Use a variable that holds true or false to remember what is happening now, so each event can check it.
A lift only opens its doors when it has stopped, and a ticket gate only lets you through once per ticket. Each one remembers a single yes-or-no fact.
Last lesson's reaction tester has a cheat: press A before the heart and you still get a time. To fix it, the micro:bit must remember whether the heart is showing. A variable can hold true or false (in Logic), and an if can ask about it.
Make your prediction first. Help opens once you've locked it in.

let start = 0
input.onButtonPressed(Button.B, function () {
basic.clearScreen()
basic.pause(randint(1000, 4000))
basic.showIcon(IconNames.Heart)
start = input.runningTime()
})
input.onButtonPressed(Button.A, function () {
basic.showNumber(input.runningTime() - start)
})
This is last lesson's reaction tester.
Press B, then press A straight away, before the heart.

let start = 0
input.onButtonPressed(Button.B, function () {
basic.clearScreen()
basic.pause(randint(1000, 4000))
basic.showIcon(IconNames.Heart)
start = input.runningTime()
})
input.onButtonPressed(Button.A, function () {
basic.showNumber(input.runningTime() - start)
})

let waiting = false
let start = 0
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
basic.showNumber(input.runningTime() - start)
} else {
basic.showIcon(IconNames.No)
}
})
def on_button_pressed_a():
global waiting
if waiting:
waiting = False
basic.show_number(input.running_time() - start)
else:
basic.show_icon(IconNames.NO)
input.on_button_pressed(Button.A, on_button_pressed_a)
global waiting lets the function change the waiting made outside it. Blocks do this for you.
This version remembers whether the heart is showing in waiting. Fill in waiting after each event, and what A shows.
| event | waiting after | shows |
|---|---|---|
| B pressed | false | nothing |
| A pressed too early | ||
| heart appears | heart | |
| A pressed | ||
| A pressed again |
The same program in Python. Match each line with what it does.
The variable on is made for you. Make A switch the light on when it is off, and off when it is on.
In on button A pressed: if on, clear the screen and set on to false.
else: show the lights and set on to true. Then delete the B event.
Make a shake show whether the light is on.
In on shake: if on, show a tick; else show a cross.
Make A alone switch the light on and off, using on.
This ticks by itself when your blocks do what is asked.
Make a shake show a tick if the light is on, and a cross if it is off.
This ticks by itself when your blocks do what is asked.
A stopwatch from last lesson.

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 start; B shows running time − start. Fill in what B shows.
| press | running time | B shows |
|---|---|---|
| A | 1000 | (start) |
| B | 1600 | |
| B | 2500 |
Fix the cheat. waiting is made for you.
waiting to false.waiting to true.At the start of B, set waiting to false. When the heart appears, set waiting to true.
In A: if waiting, set it to false and show the time; else show a cross.
No cheating: only a press while the heart is showing counts; an early press shows a cross.
This ticks by itself when your blocks do what is asked.
Each micro:bit can vote yes once with A; after that, it shows a cross. Make B vote no, also only once (one vote each, yes or no).
Students use a variable that holds true or false to remember what is happening now, and fix a cheat in a reaction tester. About 40 minutes.
Open the teacher guide: answers, solutions, slides and a worksheet.
A variable can hold true or false (Logic). It remembers one yes-or-no fact, such as is the heart showing? Each event either sets it (the heart appears: true) or asks about it with if. The usual bug is forgetting to set it back, so it keeps saying true and every later press counts. if waiting asks "is waiting true?"; there is no need to write = true.
Knowing where to set it and where to ask. Ask: when does this fact become true, and when does it stop being true?
Stretch: Make the light switch remember how many times it was switched on, and show it on A+B.
Support: Give students the if … else in A already built, and ask them only to set waiting in the right places.
Next: the final challenge. Build the reaction timer from scratch, then race a friend over radio.