Step Counter

Challenge

Fitness tracker to count steps moved during the day.

We need a variable to record the number of steps We then increase the steps each time we shake the micro:bit We can display an image to show we took a step

When button A is pressed, we show the number of steps for a short amount of time, then clear the screen

Desirable: Show number of steps to target Set target number of steps Display motivational messages

Could you record whether a step is taken more accurately

Example Solution


from microbit import *

steps = 0

while True:

  if accelerometer.was_gesture("shake"):
    steps += 1
    display.show(IMAGE.Happy)
    sleep(100)
    display.clear()
    
  # more accurate
  y = accelerometer.get_y()
  
  if y < 0:
    sleep(100)
    y = accelerometer.get_y()
    
    if y > 0:
      steps += 1
      display.show(IMAGE.Happy)
      sleep(100)
      display.clear()
      
  if button_a.was_pressed():
    display.scroll(to_s(count))