GoalUse an if block in a receiving micro:bit to choose what to do with a number that arrives.
New words
condition · a test that is true or false, such as receivedNumber > 2if … then … else · runs one set of blocks when the condition is true and another set when it is falsecompare · to check how two values are related, for example equal to, greater than or less than
Explanation
A receiving micro:bit does not have to show every message. It can decide. Inside on radio received, an if block tests a condition about the value that arrived. For numbers you can compare with = , < or >. If the condition is true, the blocks inside then run. If it is false, the blocks in else run. Only one of the two sets runs for each message. This works like a decision in a flowchart or in pseudocode. In the examples the receiver uses its own rule. The sender simply sends the number. Notice that = is true only for exactly that number, while > is true for every larger number. Trace the messages one at a time, and write down what the receiver shows after each.
Worked examples
A class points game. Each press of A adds 1 to score and sends it. The receiver shows the number while it is small, but when the number is greater than 2 it shows a yes icon instead.
Event
score sent
Is receivedNumber > 2?
Receiver shows
press A (1)
1
no
number 1
press A (2)
2
no
number 2
press A (3)
3
yes
yes icon
press A (4)
4
yes
yes icon
Tracing four presses of A. score starts at 0. The messages are 1, 2, 3 and 4. For 1 and 2 the condition is false, so the number is shown. For 3 and 4 it is true.
A small change makes a different program. With = (3) the yes icon appears only when exactly 3 arrives. A message of 4 would just show the number 4.
Class activity
Pretend receiver (no computer). Write the rule on a card: if the number is greater than 2 say “yes”, otherwise say the number. A partner holds number cards 1 to 6, turns them over one at a time and reads each number. You apply the rule and answer aloud. Write your answers in a table. Then change the rule to “less than 4” and repeat with the same cards in a different order. Draw the blocks for one of your rules. If you have a computer or tablet, ask an adult to help you look for the if block in the block editor.
Practice
1
The receiver uses the rule: if receivedNumber > 5 then show the yes icon, otherwise show the number. The messages are 4, 6, 5 and 9. What does it show each time?
4 shows the number 4. 6 shows the yes icon. 5 shows the number 5, because 5 is not greater than 5. 9 shows the yes icon.
2
A pupil wants the no icon to appear when the number is less than 3, and the number to be shown otherwise. Write the blocks for the receiver’s on radio received.
on radio received (receivedNumber): if <(receivedNumber) < (3)> then show icon [no v] else show number (receivedNumber).
3
The receiver uses if receivedNumber = 4 then show yes icon, else show number. The sender sends 5. What is shown, and what mistake might the pupil have made?
It shows the number 5. The pupil probably wanted every number of 4 or more to show the icon, but = is true only for exactly 4. The condition should use > (3) instead.
4
Draw a flowchart for the rule in the first question.
Start, then a box “a number arrives”, then a diamond “receivedNumber > 5?”. The yes branch goes to “show the yes icon”, the no branch goes to “show the number”, and both end.