Skip to main content

Case 9: Compare whose hand speed is faster

Introduction:

Two players press their button sensors at the same time to see who can complete 10 presses first and win the game.


Case purpose:

  1. Understand the variable blocks in MakeCode

  2. Understand the digital operation blocks in MakeCode


Materials Needed:

Item图片Quantity
micro:bit V21
Jacdac Expand board1
Jacdac 10cmConnection Line2
Button Sensor2
USB Cable1

Sensor principle description:

Principle description

Button sensor: A sensor used to detect the status of a button, which outputs signals carrying "0" and "1".


Connection diagram

As shown in the figure below, insert the micro:bit board into the Jacdac expansion board, and use the connecting wire to connect the two button sensors in a star connection to the gold finger interface of the Jacdac expansion board.

Programming software

Microsoft Makecode


Makecode programming

Step 1: How to add Jacdac extension

  1. Enter Microsoft Makecode and click "New Project".

  1. In the pop-up window, enter a project name and click Create.


  1. Click "Extend" in the code drawer.

  1. In the pop-up interface, enter "Jacdac" and click the search icon, select the Jacdac software library, as shown in the figure.

![](https://wiki-media-ef.oss-cn-hongkong.aliyuncs.com/docs/microbit/getting-started/microbit-jacdac-smartexploration-kit/images/Step%20Diagram/jacdac-smart-exploration-kit-3.png)


Step 2:

  1. Connect the micro:bit mainboard to the computer with a data cable.

Note: If the micro:bit mainboard is running the Jacdac program for the first time, please pre-install a blank Jacdac program on the mainboard, otherwise skip this step.

  1. Connect the sensor, MakeCode will automatically identify and simulate it, and the sensor will provide real-time feedback of the sensor status in the simulation area.

  1. Click on "ADD BLOCKS" sensor expansion module.

    Note: To connect a new sensor, repeat the "Click on "ADD BLOCKS" sensor expansion module" operation process.


Write the program as shown


https://makecode.microbit.org/S04563-87350-86165-83144


You can also download the program directly from the following webpage and start running the program after the download is complete.


Result

When the A button sensor is detected to be pressed, one LED light will be lit in the two columns of LED lights on the left side of the micro:bit motherboard for counting. Similarly, when the B button sensor is detected to be pressed, one LED light will be lit in the two columns of LED lights on the right side of the motherboard for counting. The goal of the game is to see who can quickly press the corresponding button sensor 10 times, and that player will be the winner. If you need to restart the game, press the reset button of the micro:bit motherboard.

Thinking

What is the difference between variables and constants.