← micro:bit: talk and play

A shared code

Send a place number that stands for a word in a list both micro:bits share.

Morse code, flag signals and the numbers a fast-food counter calls out all work because both sides agree what each code means.

Last lesson, 1 meant "left" and 2 meant "right" because you decided so. A list makes that agreement clear: if both micro:bits have the same list of words, sending a place number is enough. The receiver looks up the word at that place.

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 words = ["Hi", "Yes", "No"]
radio.setGroup(1)
radio.onReceivedNumber(function (receivedNumber) {
    basic.showString(words[receivedNumber])
})
input.onButtonPressed(Button.A, function () {
    radio.sendNumber(2)
})

The receiver shows get value at receivedNumber: the word at the place that arrived.

You press A on one micro:bit, so it sends 2. What does the other micro:bit show?

RunRun it and compare the result with your prediction.

Run

Press A on one micro:bit and read the word on the other.

The program as blocks
Show as code
lesson.ts
let words = ["Hi", "Yes", "No"]
radio.setGroup(1)
radio.onReceivedNumber(function (receivedNumber) {
    basic.showString(words[receivedNumber])
})
input.onButtonPressed(Button.A, function () {
    radio.sendNumber(2)
})
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

A steps through the list; B sends the place number of the chosen word.

The program as blocks
Show as code
lesson.ts
let words = ["Hi", "Yes", "No"]
let choice = 0
radio.setGroup(1)
input.onButtonPressed(Button.A, function () {
    choice += 1
    if (choice == words.length) {
        choice = 0
    }
    basic.showString(words[choice])
})
input.onButtonPressed(Button.B, function () {
    radio.sendNumber(choice)
    basic.showIcon(IconNames.Yes)
})
radio.onReceivedNumber(function (receivedNumber) {
    basic.showString(words[receivedNumber])
})

Ella's list:

The program as blocks
Show as code
let words = ["Hi", "Yes", "No"]

Tom's list:

The program as blocks
Show as code
let words = ["Yes", "No", "Hi"]

In Python, the receiver is one line: basic.show_string(words[receivedNumber]). The square brackets mean "the item at this place", just like get value at.

This messenger lets you choose a word with A, then send it with B. Fill in choice after each press of A.

presschoice afterword shown
(start)0
A
A
A

Ella and Tom built the messenger, but Tom's list is in a different order. Ella sends Yes, and Tom's micro:bit shows No. Why?

Why send the place number instead of the whole word?

ModifyChange the code on purpose and check the result.

Modify

Add a fourth word. Did you need to change anything else? Why not?

Need a hint?

Click + on the list block. Both micro:bits run the same program, so one change updates both lists.

Make the receiver reply: when a word arrives, it sends back 99, and the sender shows a tick when 99 arrives. Real messaging apps do this to show "delivered".

Need a hint?

In on radio received: if receivedNumber = 99, show a tick; else show the word and send 99.

Another hint

Ask about 99 first, because 99 is not a place in the list.

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

Add a fourth word to the list.

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

Reply with 99 when a message arrives, and show a tick when 99 comes back.

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

The program as blocks
Show as code
lesson.ts
radio.setGroup(1)
radio.onReceivedNumber(function (receivedNumber) {
    if (receivedNumber > 3) {
        basic.showIcon(IconNames.No)
    } else if (receivedNumber > 1) {
        basic.showIcon(IconNames.Sad)
    } else {
        basic.showIcon(IconNames.Happy)
    }
})

What does this receiver show for each number that arrives?

receivedNumbershows
1
2
5

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

Make

Make a mood check-in for the start of class. Each student's micro:bit has a list of moods (at least three). A steps through them, B sends the chosen one, and the teacher's micro:bit shows it.

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?

Make a list of moods and a variable mood. A changes mood by 1 and goes back to 0 at the end of the list.

Another hint

B sends mood. on radio received shows get value at receivedNumber.

A mood check-in: A chooses a mood from a list, B sends its place, and the other micro:bit shows the mood.

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

ModifyChange the code on purpose and check the result.

Change a quiz buzzer

This buzzer lets you pick an answer, A, B or C, and send it to the quiz master. Add a fourth answer, D, then make the quiz master's micro:bit show a tick after each answer arrives.

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 send a place number that stands for a word in a list both micro:bits share, and see that a message is a code both sides must agree on. About 45 minutes.

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

Students will be able to

Before class

This lesson joins earlier ideas: lists (places from 0), variables (the chosen place) and radio. Both micro:bits hold the same list of words. Instead of sending a word, the sender sends its place number; the receiver shows the word at that place (get value at receivedNumber). That shared list is a code, and both sides must agree on it exactly: same words, same order. Stepping through a list uses a variable that goes up by 1 and goes back to 0 when it reaches the length. A reply (such as 99 for "got it") is just another agreed number that is not a place in the list.

Running the lesson

  1. Predict: many say 2. Ask what the receiver does with the number (looks it up).
  2. Run: it shows No. Say it plainly: the number is a code for a word both sides know.
  3. Investigate: the trace shows the loop back to 0. Ella and Tom is this lesson's misconception: a code only works if both sides share it.
  4. Modify: adding a word needs nothing else changed, because of the length. The reply task is how real apps show 'delivered'.
  5. Mix it up: else if over receivedNumber, where the order of the questions matters.
  6. Make: a mood check-in. With real micro:bits, the teacher's micro:bit can sit at the front.

The hard part (on purpose)

Both sides must share the same list in the same order. When the wrong word shows, ask them to compare the two lists item by item.

When students get stuck

Stretch and support

Stretch: Send two numbers in one message: the sender's player number times 10, plus the word's place (so 23 means player 2 said word 3). Decode it on the receiver.

Support: Give students the mood list and the A stepping blocks, and ask them to add the sending and receiving.

Where it leads

Next unit: time. The micro:bit can measure how long something takes. Class ideas: a class emoji vote; a secret code where groups invent their own word lists and swap them.

What each step is for

Now you can
Next lesson5. Time betweenStore the running time at one moment and take it away from a later one, to measure how long something took.