Skip to content

활동

Distance calculator

중급 | MakeCode, Python | LED, 버튼, 스피커, 터치 로고 | 곱셈, 변수, 측정

1단계: 만들어 보세요.

프로젝트 소개

Use your BBC micro:bit to measure distances as you walk.

학습 내용

This practical project shows how to use multiplication and variables to measure distances.

설명

  • This program calculates distances by multiplying step length by the number of steps you take. It uses an average child's step length of 0.6 metres.
  • Carefully walk the distance you want to measure, pressing button A each time you take a step.
  • The program works by increasing the 'step count' variable by one each time button A is pressed.
  • Press button B to see the distance you have walked in metres. The micro:bit calculates this by multiplying the 'step count' variable by the step length.
  • If you want to know how many steps you took, press the touch logo on the front of your micro:bit.
  • Reset the micro:bit by pressing the reset button on the back.

준비물

  • BBC micro:bit and battery pack

Step 2: code it

1# Imports go at the top
2from microbit import *
3import music
4
5stepcount = 0
6steplength = 0.6
7display.show(0)
8music.play(['c4:4'])
9
10while True:
11    if button_a.is_pressed():
12        music.play(['c3:2'])
13        display.show(Image.HEART)
14        sleep(400)
15        stepcount += 1
16        display.clear()
17    if button_b.is_pressed():
18        display.scroll(stepcount*steplength)
19    if pin_logo.is_touched():
20        display.scroll(stepcount)

Step 3: improve it

  • Calculate your average step length in metres and use it to replace the 0.6 number in the code.
  • Calculate areas of rectangles, such as parts of a school playground, by measuring each side and multiplying the distances together.
  • Add cardboard to make the buttons easier to press, as seen in the Improve it section of the Emotion badge project.
  • Use this wheelchair distance calculator code that can automatically detect when your wheel rotates. Replace the 0.6 number with the circumference of your wheel measured in metres.