Skip to content

活动

计步器

初学者 | MakeCode, Python | LED显示器, 加速计 | 乘法, 人体, 传感器, 变量, 性能工具, 测量

第 1 步:制作

它是什么?

Turn your BBC micro:bit into a step counter (or pedometer) to help you track how active you are - and learn some coding at the same time!

这两个视频介绍了你将制作什么以及如何编程:

介绍

编程指南

工作原理

  • Download the code onto a micro:bit and attach a battery pack.
  • Attach the micro:bit and battery pack to your shoe or ankle, place it inside your sock, or just hold it in your hand and shake it as you walk.
  • The code uses the micro:bit’s accelerometer sensor input to sense when your leg is moving.
  • The code counts how many times the micro:bit has been shaken. It 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.

所需材料

  • micro:bit或者是MakeCode模拟器
  • MakeCode 或者 Python 编辑器
  • 电池盒
  • something to attach the micro:bit to your shoe or leg – elastic band, a pipe cleaner, tape or Velcro.

第 2 步:编程

1from microbit import *
2steps=0
3
4while True:
5    if accelerometer.was_gesture('shake'):
6        steps += 1
7        display.show(steps)

第3步:完善

  • Modify the code so it shows your current step count when you press a button.
  • 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.
  • 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.
女孩指着另一个女孩鞋子上的 micro:bit 计步器