AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Choosing what arrives

Week 16 · 1.8 Physical computing · 3 lessons
1

Decide on what arrives

Computing 8: Topic 1.8, pp. 173–196
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.

Starta number arrives (receivedNumber)receivedNumber > 2?yesnoshow the yes iconshow the numberEnd
Worked examples
Senderradio set group4setscoreto0on startchangescoreby1radio send numberscoreon buttonApressedReceiverradio set group4on startshow iconyesshow numberreceivedNumberifreceivedNumber>2thenelseon radio receivedreceivedNumber
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.
Eventscore sentIs receivedNumber > 2?Receiver shows
press A (1)1nonumber 1
press A (2)2nonumber 2
press A (3)3yesyes icon
press A (4)4yesyes 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.
Receiverradio set group4on startshow iconyesshow numberreceivedNumberifreceivedNumber=3thenelseon radio receivedreceivedNumber
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?
Receiverradio set group1on startshow iconyesshow numberreceivedNumberifreceivedNumber>5thenelseon radio receivedreceivedNumber
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.
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?
Receiverradio set group1on startshow iconyesshow numberreceivedNumberifreceivedNumber=4thenelseon radio receivedreceivedNumber
4
Draw a flowchart for the rule in the first question.