← micro:bit: talk and play

Reaction timer

Build a reaction game from scratch, using everything so far. Wait for the heart, press A as fast as you can, and beat your best.

Sprinters, racing drivers and goalkeepers train their reactions with timers like this. You already know every idea it needs.

Press B to start. After a random wait, a heart appears: press A as fast as you can, and the micro:bit shows how many milliseconds you took. You have met every part of this game. Now you build it from almost nothing, in three milestones, with less help each time.

PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.

Predict

The program as blocks
Show as code
lesson.ts
input.onButtonPressed(Button.B, function () {
    basic.clearScreen()
    basic.pause(randint(1000, 4000))
    basic.showIcon(IconNames.Heart)
})
You press B three times, waiting for the heart each time. Does the heart take the same time to appear each time?

RunRun it and compare the result with your prediction.

Run

Press B a few times. Can you guess when the heart will appear?

The program as blocks
Show as code
lesson.ts
input.onButtonPressed(Button.B, function () {
    basic.clearScreen()
    basic.pause(randint(1000, 4000))
    basic.showIcon(IconNames.Heart)
})
Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.

InvestigateTrace how each line of code connects to what you see.

Investigate

Read the finished game before you build your own.

The program as blocks
Show as code
lesson.ts
let reaction = 0
let waiting = false
let start = 0
let best = 99999
input.onButtonPressed(Button.B, function () {
    waiting = false
    basic.clearScreen()
    basic.pause(randint(1000, 4000))
    basic.showIcon(IconNames.Heart)
    start = input.runningTime()
    waiting = true
})
input.onButtonPressed(Button.A, function () {
    if (waiting) {
        waiting = false
        reaction = input.runningTime() - start
        basic.showNumber(reaction)
        if (reaction < best) {
            best = reaction
            basic.showIcon(IconNames.Happy)
        }
    } else {
        basic.showIcon(IconNames.No)
    }
})
input.onButtonPressed(Button.AB, function () {
    basic.showString("Best")
    basic.showNumber(best)
})

This is the finished game. Match each job with the blocks that do it.

wait an unguessable time
measure the reaction
catch an early press
keep the fastest time

start is set when the heart appears. Fill in the number shown when A is pressed.

start (heart appears)running time when A is pressednumber shown
52005450
80008320

best starts at 99999. Your first reaction is 400. What is best after it?

ModifyChange the code on purpose and check the result.

Milestone 1: time the press

The variable start is made for you, and B clears the screen. Build the rest:

  1. B: wait a random time, show the heart, and store the time in start.
  2. A: show how long it took.
Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.
Need a hint?

In B: pause pick random 1000 to 4000, show the heart, then set start to running time.

Another hint

In A: show number running time − start.

B waits a random time and shows the heart; A shows the reaction time.

This ticks by itself when your blocks do what is asked.

ModifyChange the code on purpose and check the result.

Milestone 2: no cheating

Make an early press show a cross, using the variable waiting. Set it, ask about it, and set it back.

Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.

Only count a press while the heart is showing; an early press shows a cross.

This ticks by itself when your blocks do what is asked.

MakeBuild something new using what you have learned. Expect a few tries; that is normal.

Milestone 3: beat your best

Keep a best time:

Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.

Keep your best time, celebrate a new best, and show it on A+B.

This ticks by itself when your blocks do what is asked.

ModifyChange the code on purpose and check the result.

Why random?

Play this version a few times. What do you notice?

The program as blocks
Show as code
lesson.ts
let waiting = false
let start = 0
input.onButtonPressed(Button.B, function () {
    waiting = false
    basic.clearScreen()
    basic.pause(2000)
    basic.showIcon(IconNames.Heart)
    start = input.runningTime()
    waiting = true
})
input.onButtonPressed(Button.A, function () {
    if (waiting) {
        waiting = false
        basic.showNumber(input.runningTime() - start)
    } else {
        basic.showIcon(IconNames.No)
    }
})
Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.
In this version the wait is always 2 seconds. Why does that spoil the game?

For teachers

Students build a reaction game from an almost empty starter in three milestones, using a random wait, running time, a true/false variable, decisions and a best score. About 55 minutes.

Open the teacher guide: answers, solutions, slides and a worksheet.

Students will be able to

Before class

This challenge adds nothing new: every part was taught earlier in the course. Time between gave the timing (store running time in start, then running time − start); True or false gave waiting, the flag that catches early presses. The milestones fade the help: Milestone 1 lists the steps, Milestone 2 gives only the goal, Milestone 3 only goals. Play it yourself first; good times are 200 to 400 ms, and the simulator adds a little delay.

Running the lesson

  1. Predict and Run: the random wait is what makes it a reaction game. Let students try to guess and fail.
  2. Investigate: matching jobs to blocks is the plan for the milestones.
  3. Milestone 1: if times look huge, start was never set.
  4. Milestone 2: waiting is true only while the heart shows. Read the A event aloud: 'if we are waiting for a press, time it; else, too early'.
  5. Milestone 3: starting best at 99999 means any first time beats it.
  6. Why random: a fixed wait can be counted, so it stops measuring reactions.

The hard part (on purpose)

Putting the parts in the right events. Ask which event each job belongs in: B or A.

When students get stuck

Stretch and support

Stretch: Show a rating after each time: under 250 a happy face, under 400 a meh face, otherwise a sad face (if, else if, else).

Support: Give students last lesson's no-cheating program as their Milestone 1 starter, and focus on Milestone 3.

Where it leads

Next: Radio duel, the same game for two players over radio. Class ideas: a class reaction leaderboard on the board; does reaction time change after exercise? (a quick science investigation with real micro:bits).

What each step is for

Now you can
Next lesson8. Radio duelTurn the reaction timer into a two-player race over radio. First to press after the heart wins.