Lesson 1: Step Counter
To Know - Create a Step Counter
To Be Able To - Develop a Step Counter Program
Step 1: Make It
Turn your BBC micro:bit into a step counter (or pedometer) to help you track how active you are.
Step counters are built into most mobile and fitness devices.
They are built into:
Mobile Phones
Smart watches (e.g. fitbits, apple watches)
Health trackers (e.g. whoop, garmin)
This program makes use of the micro:bit’s accelerometer sensor input to sense when your leg is moving.
Each time you take a step, the code counts detects that the micro:bit has been shaken. It then stores this number in a variable called ‘steps’.
Variables are containers for storing data, which can be accessed and updated while a program is running.
Every time the micro:bit accelerometer input senses a shake, the program increases the number held in the variable by 1, and shows the new number on the LED display output.
Brainstorm in pairs, using the description above to breakdown what the program needs to do.
___________________________________________
___________________________________________
___________________________________________
___________________________________________
___________________________________________
Step 2: Code It
Function - This line imports all the micro:bit libraries, which contain tools for using its buttons, screen, and sensors.
Python Code - from microbit import *
Function - This line sets up a variable called steps, starting at 0. This variable will keep track of how many times you shake the Microbit, or how many steps you have taken.
Python Code - steps = 0
Function - This starts a loop that will keep running as long as the micro:bit is on. It lets us continuously check if a shake gesture is detected.
Python Code - while True:
Function - This line checks if the micro:bit was shaken.
Python Code - if accelerometer.was_gesture('shake'):
Function - When a shake is detected, the steps variable goes up by 1.
Python Code - steps += 1
Function - This line displays the current step count on the micro:bit's LED screen.
Python Code - display.show(steps)
Step 3: Improve It
There are errors in this program, by testing you will be able to identify these issues.
Take the time to use your step counter outside to help identify these issue.
Once you find an error make the improvement to your program then test it again to make sure it works.
Reset the count
Modify the code so it shows your current step count when you press a button.
Ensure it is accurate
If you find that the code only counts every other step, modify the code to multiply the ‘steps’ variable by two when it’s displayed.
Calculate the distance walked
Measure the length of your average stride and get your micro:bit to multiply this by the number of steps to calculate the distance you’ve walked.