← micro:bit: talk and play

True or false

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.

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
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.

You press B, then press A before the heart appears. What does A show?

RunRun it and compare the result with your prediction.

Run

Press B, then press A straight away, before the heart.

The program as blocks
Show as code
lesson.ts
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)
})
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

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(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.

eventwaiting aftershows
B pressedfalsenothing
A pressed too early
heart appearsheart
A pressed
A pressed again

Raj sets waiting to true when the heart appears, but never sets it back to false. He presses A once and gets a time. What does a second press of A show?

The same program in Python. Match each line with what it does.

waiting = True
if waiting:
else:

ModifyChange the code on purpose and check the result.

Modify

The variable on is made for you. Make A switch the light on when it is off, and off when it is on.

Need a hint?

In on button A pressed: if on, clear the screen and set on to false.

Another hint

else: show the lights and set on to true. Then delete the B event.

Make a shake show whether the light is on.

Need a hint?

In on shake: if on, show a tick; else show a cross.

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

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.

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

Mix it up

A stopwatch from last lesson.

The program as blocks
Show as code
lesson.ts
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.

pressrunning timeB shows
A1000(start)
B1600
B2500

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

Make

Fix the cheat. waiting is made for you.

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?

At the start of B, set waiting to false. When the heart appears, set waiting to true.

Another hint

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.

ModifyChange the code on purpose and check the result.

Change a class vote

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).

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

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.

Students will be able to

Before class

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.

Running the lesson

  1. Predict: an early press still gets a time. Let students find the cheat.
  2. Investigate: the trace follows waiting through five events. Raj's bug is this lesson's misconception.
  3. Modify: a light switch with one button: the same idea, a different fact.
  4. Mix it up: the stopwatch from last lesson, with two B presses.
  5. Make: this fixes the cheat; it is the core of the final challenge.

The hard part (on purpose)

Knowing where to set it and where to ask. Ask: when does this fact become true, and when does it stop being true?

When students get stuck

Stretch and support

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.

Where it leads

Next: the final challenge. Build the reaction timer from scratch, then race a friend over radio.

What each step is for

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