Reaction Timer

Step 2 – how to measure a length of time

We don't always want the time from when program was sent to the micro:bit. Sometimes we need to be able to measure how long something takes to happen.

To do this, we'll measure the length of a piece of music.

Micro:bits have a very limited set of music that they can play. They're very short clips. The one we'll use lasts about three and a half seconds.

The code has some key ideas in it:

  1. line 3 is needed to allow the micro:bit to use music
  2. we'll start the music when button a is pressed
  3. line 7 stores the time at the start of the music
  4. line 9 stores the time at the end of the music
  5. by taking them away from each other (line 10) we can get the amount of time that passed whilst the music was playing

Once you have the program written, send it to the micro:bit and press button a a few times. The time should be about the same each time.

Code image

You could add another piece of music on button b.