Store the running time at one moment and take it away from a later one, to measure how long something took.
A stopwatch, a lap timer and a game's reaction score all do one subtraction: the time now, take away the time it started.
The micro:bit keeps count of time. running time (in Input › more) gives the number of milliseconds since the micro:bit switched on. 1000 ms is one second. On its own it is a very big number; the trick is to take one reading away from another.
Make your prediction first. Help opens once you've locked it in.

let start = 0
input.onButtonPressed(Button.A, function () {
start = input.runningTime()
basic.showIcon(IconNames.Yes)
})
input.onButtonPressed(Button.B, function () {
basic.showNumber(input.runningTime() - start)
})
A stores the running time in start. B shows the running time take away start.
Press A, count to three in your head, then press B. Is the number close to 3000?

let start = 0
input.onButtonPressed(Button.A, function () {
start = input.runningTime()
basic.showIcon(IconNames.Yes)
})
input.onButtonPressed(Button.B, function () {
basic.showNumber(input.runningTime() - start)
})

let start = 0
input.onButtonPressed(Button.A, function () {
start = input.runningTime()
basic.showIcon(IconNames.Yes)
})
input.onButtonPressed(Button.B, function () {
basic.showNumber(input.runningTime() - start)
})
Fill in the number B shows.
| start (A pressed) | running time when B is pressed | B shows |
|---|---|---|
| 5200 | 5450 | |
| 8000 | 11000 | |
| 12000 | 12180 |
B shows 2500. How many seconds is that?
Make B show seconds, not milliseconds.
Drag the division block from Math into show number.
Put running time − start on the left and 1000 on the right.
Make a shake start the stopwatch as well as A.
Drag in on shake, with set start to running time inside.
Make B show the time in seconds, by dividing by 1000.
This ticks by itself when your blocks do what is asked.
Make a shake start the stopwatch too.
This ticks by itself when your blocks do what is asked.

input.onButtonPressed(Button.B, function () {
basic.clearScreen()
basic.pause(randint(1000, 4000))
basic.showIcon(IconNames.Heart)
})
B clears the screen, pauses for pick random 1000 to 4000, then shows the heart. What is the longest the heart can take to appear, in milliseconds?
Press B and a heart appears after a random wait. Make A show how many milliseconds you took to press it.
start to running time.When the heart appears, set start to running time.
In on button A pressed: show number running time − start.
Time a reaction: store the time when the heart appears, and show how long until A is pressed.
This ticks by itself when your blocks do what is asked.
This timer starts when the micro:bit switches on, and again whenever A is pressed. It shows a small square until 5 seconds have passed, then an alarm face. Make it a 3-minute timer (180000 ms), and choose your own pictures.
Students store the running time at one moment and take it away from a later reading, to measure how long something took. About 40 minutes.
Open the teacher guide: answers, solutions, slides and a worksheet.
running time (Input › more) is the number of milliseconds since the micro:bit switched on. It never restarts at a press, so on its own it is a big number. Timing is a subtraction: store running time in a variable at the start (start), then later show running time − start. 1000 ms is one second; dividing by 1000 gives seconds with a decimal. In the simulator, timings are a little longer than on a real micro:bit.
Seeing why the subtraction is needed. Ask what running time was when the micro:bit started, and what it is now.
Stretch: Make a lap timer: B shows the time since the last B, not since A (set start again after showing).
Support: Give students the make with start already set when the heart appears, and ask them only to build the subtraction in A.
Next lesson: a true/false variable, so an early press does not count. Class idea: time how long the class takes to go silent after the quiet signal.