GoalName the inputs and outputs of a micro:bit program and trace what the board shows after a series of presses and shakes.
New words
input · a way of getting data into a program, such as a button press or a sensor readingoutput · what a program sends out, such as a number or picture on the lightsevent · something that happens and starts a block, such as a shake or a presssensor · a part that measures the world, such as temperature or light
Explanation
A micro:bit program takes data in, processes it and sends data out. The inputs can be events. The blocks on button A pressed, on button B pressed, on button A+B pressed, on shake and on logo pressed each wait for something to happen. When it happens, the blocks inside them run once. The inputs can also be sensor readings. The board can measure the temperature and the light level, and these readings are numbers that a program can use in a calculation or a condition. The outputs are what we can see. The board can show a number, an icon or a word on its 25 lights. A variable keeps its value between events, so a program can count presses.
Kind
Example
What it gives or does
input (event)
button B pressed
starts the blocks inside it
input (event)
shake
starts the blocks when the board is moved
input (sensor)
temperature
a number, in degrees Celsius
input (sensor)
light level
a number from 0 (dark) to 255 (bright)
output
show number
scrolls a number across the lights
output
show icon
shows a picture, such as a heart
Worked examples
This program is a step counter. Each shake adds 1 to steps, and then the new value of steps is shown. After four shakes the board has shown 1, 2, 3 and 4.
Three buttons control the same variable, n. Button A adds 1, button B takes away 1 and A+B sets n back to 0. Suppose that the buttons are pressed in this order: A, A, B, A, A+B, A. The board shows the new value of n each time.
Press
n after
Board shows
A
1
1
A
2
2
B
1
1
A
2
2
A+B
0
0
A
1
1
This is the trace of the six presses. Check each row against the blocks.
Class activity
Be the micro:bit (no computer): draw a 5 × 5 grid on paper for the lights. Write a program on a card, such as ‘on button A: change n by 2 and show n’. One player gives inputs by calling out ‘A’, ‘B’ or ‘shake’. The micro:bit player keeps the value of n on a small whiteboard or in a notebook, and writes the output on the grid. Swap roles after eight inputs. Then add a new rule to the program, for example ‘on shake: set n to 0’, and run the same eight inputs again. What changes? If you have a computer or tablet, build one of the programs in a micro:bit simulator.
Practice
1
The buttons are pressed in the order A, A, B, A. What numbers does the board show, and in which order?
score goes 0, then 2 (A), 4 (A), 3 (B) and 5 (A). The board shows 2, 4, 3 and 5.
2
What does the board show after the first, the second and the third shake?
energy starts at 20. The shakes give 16, 12 and 8.
3
Say whether each of these is an input or an output. (a) pressing button B, (b) show icon heart, (c) shaking the board, (d) show number 5, (e) the temperature reading.
Why is a counter program written with an event block, such as on button A pressed, rather than just with on start?
The blocks in on start run only once, when the program starts. An event block runs again every time that the event happens, so the program can count each press.