GoalTrace a receiver that uses a variable and works with messages from two micro:bits.
New words
message · the number or text sent by one micro:bit and received by anotherreceivedNumber · the name that holds the number that has just arrivedtrace table · a table that records what each device sends, stores and shows after each event
Explanation
More than two micro:bits can share one group. Every micro:bit in the group hears every message. A receiver runs its on radio received block once for each message. The value that arrived is in receivedNumber, but only until the next message comes. To keep a running total, the receiver can store it in a variable. For example, change total by receivedNumber adds the new number to total, and then show number total displays the total. The order of events matters. Make a trace table with one row for each event and a column for each micro:bit’s variable or display. A micro:bit in another group is not part of the conversation and shows nothing.
A message arrivesthen→change total by receivedNumberthen→show number (total)
Worked examples
Two senders and one receiver in group 3. X sends 2 when A is pressed. Y sends 4 when B is pressed. The receiver adds each number to total and shows total. Each message is an event.
Event
Number sent
total after the message
Receiver shows
X: press A
2
2
2
Y: press B
4
6
6
Y: press B
4
10
10
X: press A
2
12
12
Tracing the events in order: X presses A, then Y presses B, then Y presses B again, then X presses A. total starts at 0.
A fourth micro:bit uses group 4. It does not hear any of these messages, so it shows nothing. Changing it to group 3 would let it join the conversation.
Class activity
Message relay (no computer). Three players. Two are senders, and each has a card with a number between 1 and 9. One is a receiver who keeps a total on a piece of paper. The senders call out their numbers in a chosen order, and the receiver adds each new number to the total and says the total aloud. A fourth player holds a different group card and must stay silent. Write the trace table for your game: event, number sent, total. Then change one sender’s number and predict the new final total before playing again. If you have a computer or tablet, ask an adult to help you open the block editor and drag in the blocks.
Practice
1
In the program from the lesson, X presses A three times in a row and nothing else happens. What does the receiver show after each press?
It shows 2, then 4, then 6. Each message from X adds 2 to total.
2
Complete the trace table. The sender sends 3 when A is pressed and 5 when B is pressed. The events are A, B, B. The receiver adds each number to total.
Event
Sent
total
Receiver shows
press A
3
?
?
press B
5
?
?
press B
5
?
?
total becomes 3, then 8, then 13. The receiver shows 3, then 8, then 13.
3
A receiver contains set [total] to (0) inside the on radio received block, followed by change total by receivedNumber and show number total. What goes wrong, and how do you fix it?
total is set back to 0 every time a message arrives, so it always shows only the latest number and never a running total. The setting to 0 belongs in on start, so it happens once.
4
Why is it important to write the events in order in a trace table?
The values depend on what happened before. Totals and displays change after every message, so the order decides the results.