In this activity you will be creating a step counter, (pedometer) to measure the number of steps taken over a period of time. You will learn how to use mulitple inputs to control different aspects of the pedometer whilst developing your knowledge of varibales further.
- To understand how multiple inputs can be used to control different processes and outputs.
- Gain a further knowledge of variables.
- Use the micro:bit as a wearable device.
- MicroUSB Cable
- 2 x AAA Batteries
Time to Code
As you go through these steps please note that there is an image of the completed code to the right.
- Begin by selecting the ‘on start’ block from the ‘basic’ tab.
- Create a new variable called ‘step’ in the ‘variables’ tab. Select the ‘set step’ variable and insert it into the ‘on start’ block.
- Now select the ‘on shake’ input block from the ‘input’ tab. Insert the ‘change step’ block from the ‘variables’ tab into the ‘on shake’ input. Then change the number in the ‘change step’ block to 1. This is the trigger for counting each step.
- The next step is to create a reset. Do this by selecting the ‘on button pressed’ input from the ‘input’ tab. Change the button to ‘B’ using the dropdown. Again select the ‘set step’ block from the ‘variables’ tab. Insert this into the ‘on button pressed’ block.
- At this stage you have a starting command, a trigger command and a reset. The final step is to create a command to show the number of steps that you have taken.
- This will be done using a button A press. Begin by selecting the ‘on button pressed’ block from the ‘input’ tab. The default is set to button ‘A’.
- Next select the ‘repeat x times’ block from the ‘loop tab and change it to 2 times. Insert this into the ’on button pressed’ block.
- Choose the ‘show number’ block and the ‘pause’ block from the ‘basic’ tab. Insert them into the ‘repeat x times’ block. Select your ‘step’ variable from the ‘variables’ tab and insert it into the ‘show number’ block. Change the pause time to 2 seconds.
- Finally add a ‘clear screen’ block underneath the ‘repeat x block.
- The code is now finished! Download your code and upload it onto the micro:bit.
When the micro:bit first turns on the number of steps is set to 0. Everytime that the ‘on shake’ function is activated by a movement the step counter will increase by 1. To view the number of steps taken, press button A. This will display the number on the LED matrix twice before clearing the screen. To reset the step count press button B.
Taking it Further
- Include step targets into your pedometer that alert you once you have reached your target.
Keep in Touch
We would love to keep in touch with you and as a result aid you along your digital making journey.
Becoming a digital maker subscriber means you will receive exclusive offers and we will keep you up to date with new resources and kits, as well as upcoming events and workshops.
Let us know how your digital education journey is progressing. Send us pictures and videos of your awesome creations and use the hashtag below.