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

Micro:bit inputs and jobs

Week 20 · Unit 8: Be an innovator · 3 lessons
1

Inputs and outputs

Computing 6: Unit 8, pp. 106–123
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.

KindExampleWhat it gives or does
input (event)button B pressedstarts the blocks inside it
input (event)shakestarts the blocks when the board is moved
input (sensor)temperaturea number, in degrees Celsius
input (sensor)light levela number from 0 (dark) to 255 (bright)
outputshow numberscrolls a number across the lights
outputshow iconshows a picture, such as a heart
Worked examples
changestepsby1show numberstepson shake
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.
Achangenby1show numbernon buttonApressedBchangenby-1show numbernon buttonBpressedA+Bsetnto0show numbernon buttonA+Bpressed
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.
Pressn afterBoard shows
A11
A22
B11
A22
A+B00
A11
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?
Achangescoreby2show numberscoreon buttonApressedBchangescoreby-1show numberscoreon buttonBpressed
2
What does the board show after the first, the second and the third shake?
on startsetenergyto20on starton shakechangeenergyby-4show numberenergyon shake
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.
4
Why is a counter program written with an event block, such as on button A pressed, rather than just with on start?