GoalExplain that a micro:bit program can listen for several different inputs and give a different output for each one, and write the algorithm in parts.
New words
physical computing device · a device that senses the world with inputs and acts on it with outputsinput · something sent into a device, such as a button press, a touch or a shakeoutput · something a device sends out, such as lights or a sound
Explanation
The micro:bit is a physical computing device. It can sense many different inputs: button A, button B, both buttons together, a touch on the gold logo and a shake. It can give outputs too: lights on the 5 × 5 display and sounds from its speaker. One program can hold several input blocks at once. Each input block waits for its own input, and it holds the blocks for its own output. When we plan such a program, we write one part of the algorithm for each input. The parts do not depend on each other. The board does the part that matches whatever input happens.
Input
Output
button A pressed
a picture on the lights
button B pressed
a different picture
logo touched
a sound
board shaken
anything we choose
Worked examples
Part
Starts when
Output
1
button A is pressed
show a plus shape
2
button B is pressed
show an X shape
3
the logo is touched
play the yawn sound
An algorithm for a shape gadget, written in three parts. Each part starts with its own input and gives its own output.
Part 1: when button A is pressed, the lights make a plus shape. The plus has 5 lights in the middle row and 5 in the middle column. The middle light is in both, so the plus uses 5 + 5 − 1 = 9 lights.
Part 2: when button B is pressed, the lights make an X. Its two diagonal lines cross in the middle, so the X also uses 5 + 5 − 1 = 9 lights.
Class activity
Coin display (with family): put 25 coins, buttons or beans in a 5 × 5 square on a tray. Face up means a light is on and face down means it is off. One person is the micro:bit and the others call inputs: “A”, “B”, “logo” or “shake”. The micro:bit follows the algorithm above and makes the shape: a plus for A, an X for B and a yawn sound for the logo. Can you change the shape quickly? Then make a new algorithm that uses the shake input and play it again.
Practice
1
A board must do three things: button A shows the giraffe icon, buttons A and B pressed together show the small diamond icon, and shaking the board plays the slide sound. Write the algorithm in three parts.
Part
Starts when
Output
1
?
?
2
?
?
3
?
?
Part 1: start when button A is pressed; show the giraffe icon. Part 2: start when buttons A+B are pressed; show the small diamond icon. Part 3: start when the board is shaken; play the slide sound.
2
Name the input for each: (a) pressing both buttons at once, (b) touching the gold logo, (c) moving the board quickly.
(a) button A+B pressed, (b) the logo touched, (c) the board shaken.
3
What does this program do when button A is pressed? Say the outputs in order.
It first shows the umbrella icon and then plays the mysterious sound until it is done. One input can have more than one output.
4
What happens on the board if no button is pressed and the board is not touched or shaken?
Nothing happens. The board waits until an input arrives.