We are all familiar with Fitbit and other smart watches even our phones counting our steps throughout the day. Let’s use a micro:bit to code our own.
How to
Think of a variable as a box that stores information that can be used throughout our program. We give variables a descriptive name so we and others can understand what is going on within our program.
while True:
block to the code area and attach it under steps = 0
block. if True:
block to the code area and attach it within the while True:
block. accelerometer.is_gesture(‘shake’)
block and attach it within the True
of the if
block. variable = 1
block to the code area and attach it within the if accelerometer.is_gesture(‘shake’)
block. Click on Variable and click steps . Click on the = and click += . display.scroll(0)
block to the code area and attach it under steps += 1
block. steps
block to the code area and attach it within the 0
of the display.scroll
block. if True():
block to the code area and attach it under the if accelerometer.is_gesture(‘shake’):
block. button_a.is_pressed():
block to the code area and attach it within the True
of the if
block. if button_.is_pressed():
block. if button_a.is_pressed():
block and click duplicate and attach it under the if button_a.is_pressed():
block. Click on a and click b . steps = 0
block to the left menu to delete it. display.scroll(steps)
block and click duplicate. Attach the duplicated block within the if button_b.is_pressed():
block. This will display the amount of steps you have done so far when you press the B button on the micro:bit.
Completed Code:
Now that we have completed the code we need to download it to the micro:bit.
You have created your own Fitbit step counter why not connect a battery pack and strap your micro:bit to your ankle and go for a walk and see how many steps you have done at the end.