← micro:bit: talk and play

Which message?

Use the number that arrives by radio to do something different for each message, and send words as well as numbers.

A garage door opener has one receiver but two buttons: open and close. The receiver reads which code arrived and acts on it.

Last lesson every message did the same thing: a heart. But on radio received also tells you what arrived. receivedNumber holds the number that was sent, so the receiver can ask about it with if.

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
radio.setGroup(1)
input.onButtonPressed(Button.A, function () {
    radio.sendNumber(1)
})
input.onButtonPressed(Button.B, function () {
    radio.sendNumber(2)
})
radio.onReceivedNumber(function (receivedNumber) {
    if (receivedNumber == 1) {
        basic.showIcon(IconNames.Heart)
    } else if (receivedNumber == 2) {
        basic.showIcon(IconNames.Sad)
    }
})

A sends 1 and B sends 2. One on radio received block checks which arrived.

You press B on the first micro:bit. What does the second micro:bit show?

RunRun it and compare the result with your prediction.

Run

Press A, then B, on one micro:bit. Watch the other.

The program as blocks
Show as code
lesson.ts
radio.setGroup(1)
input.onButtonPressed(Button.A, function () {
    radio.sendNumber(1)
})
input.onButtonPressed(Button.B, function () {
    radio.sendNumber(2)
})
radio.onReceivedNumber(function (receivedNumber) {
    if (receivedNumber == 1) {
        basic.showIcon(IconNames.Heart)
    } else if (receivedNumber == 2) {
        basic.showIcon(IconNames.Sad)
    }
})
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
radio.setGroup(1)
input.onButtonPressed(Button.A, function () {
    radio.sendNumber(1)
})
input.onButtonPressed(Button.B, function () {
    radio.sendNumber(2)
})
radio.onReceivedNumber(function (receivedNumber) {
    if (receivedNumber == 1) {
        basic.showIcon(IconNames.Heart)
    } else if (receivedNumber == 2) {
        basic.showIcon(IconNames.Sad)
    }
})
def on_received_number(receivedNumber):
    if receivedNumber == 1:
        basic.show_icon(IconNames.HEART)
    elif receivedNumber == 2:
        basic.show_icon(IconNames.SAD)
radio.on_received_number(on_received_number)

elif is Python's short way to write else if.

What does the receiver show for each number that arrives?

receivedNumbershows
1
2
5

Max wants 1 to show a heart and 2 a sad face. He drags in a second on radio received block for 2, and it greys out. What should he do?

The receiver in Python. Match each line with what it does.

def on_received_number(receivedNumber):
if receivedNumber == 1:
elif receivedNumber == 2:

ModifyChange the code on purpose and check the result.

Modify

Make a shake send 3, and the receiver show a surprised face when 3 arrives.

Need a hint?

Drag in on shake with radio send number 3.

Another hint

Click + on the receiver's if to add another else if: receivedNumber = 3.

send string sends a word, and on radio received receivedString holds the word that arrived. Make A+B send hi, and the receiver show it.

Need a hint?

radio send string is in Radio. Put it in on button A+B pressed, with hi.

Another hint

Drag in on radio received receivedString, with show string receivedString.

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

A shake sends 3, and the receiver shows a surprised face for 3.

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

A+B sends the word hi, and the receiver shows the word that arrived.

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

Last lesson's program, on three micro:bits.

The program as blocks
Show as code
lesson.ts
radio.setGroup(1)
input.onButtonPressed(Button.A, function () {
    radio.sendNumber(1)
})
radio.onReceivedNumber(function (receivedNumber) {
    basic.showIcon(IconNames.Heart)
    basic.pause(500)
    basic.clearScreen()
})
Three micro:bits run this program. The first and second are in group 1; the third is in group 2. You press A on the first. Which show the heart?

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

Make

Make bike signals by radio: the rider presses buttons on one micro:bit, and the one on the back of the bike shows the signal.

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?

Three senders: A sends 1, B sends 2, shake sends 3.

Another hint

One on radio received, with if receivedNumber = 1 … else if = 2 … else if = 3.

Bike signals by radio: A, B and shake send three different numbers, and the other micro:bit shows a different picture for each.

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

ModifyChange the code on purpose and check the result.

Change a doorbell

This doorbell sends a word instead of a number. Make it send your name, then make B send a different message, such as "come in".

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 receivedNumber to do something different for each radio message, and send words with send string. About 40 minutes.

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

Students will be able to

Before class

on radio received runs for every number that arrives, and receivedNumber (drag it out of the block) holds which number it was. One receiver block with if … else if handles every message; a second on radio received block greys out. send string and on radio received receivedString do the same for words. Each number is a code both micro:bits agree on: 1 means left, 2 means right.

Running the lesson

  1. Predict: some students expect the number 2 to show. Ask what the receiver does with receivedNumber.
  2. Run: A and B on one, watch the other.
  3. Investigate: the 5 in the trace shows that a number with no question does nothing. Max's second block is this lesson's misconception.
  4. Modify: a third number, then a word with send string.
  5. Mix it up: groups again, from last lesson.
  6. Make: three agreed numbers, three pictures.

The hard part (on purpose)

One receiver for every message. Ask what receivedNumber holds when B is pressed.

When students get stuck

Stretch and support

Stretch: Make a two-way chat: each micro:bit sends 1 for 'yes' and 2 for 'no', and shows a tick or cross when the other answers.

Support: Give students the receiver with if … else if already built, and ask them to add the senders.

Where it leads

Next lesson: a list both micro:bits share turns the numbers into words. Class idea: a quiz buzzer where A and B are two answers.

What each step is for

Now you can
Next lesson4. A shared codeSend a place number that stands for a word in a list both micro:bits share.