Step Counter
Last updated
Last updated
In this lesson, kids will learn how to assign a variable to micro:bit and make code for performing step calculation while the kids walk
Assign a variable on the micro:bit
On walking, micro:bit recognizes a shake and add steps
When button A or B is pressed, micro:bit will display the number of steps recorded
When A+B is pressed, the variable will be reset to 0
Click on "New Project". The MakeCode editor will appear
Delete the forever blocks by dragging it to the toolbar. Click on Variables option from the toolbar
Click Make a Variable and give it a name "step"
Click Ok. The variable step will now be created
Now you will have to initialize variables on start. Drag set variable to 0 option to the on start block
Now we will have to calculate number of steps based on walking pattern. When we walk with the micro:bit it shakes. We will use the on shake option. Drag on shake option from Input section
Now open Variables section and drag change step by 1 option to the on shake block
Now micro:bit will start calculating number of steps when you walk. We will have to display the number of steps calculated when you press button A or B. Drag on button A is pressed block from the Input section.
Now we will have to display the number of steps calculated. For this drag show string option from Basic section to the block. Drag the step from Variables section to the block. Perform the same action for button B
We now need to be able to reset the steps counted. To perform this action drag on button A+B pressed option from Input section
Drag the set steps to 0 option from Variables section
Save you code as Step_Counter and press save to download.
Drag your code file named microbit-1-2-3.hex to the MICROBIT(D:) folder to upload your code to your micro:bit.
Test your micro:bit by pressing button A or B. micro:bit will display zero. Walk around for a while and then press press Button A or B once, see if micro:bit displays number of steps that you have walked. Now press A+B together and then press button B to check if the steps have been initialized again.