GoalRead, trace, correct and plan micro:bit programs that use sensors, buttons and gestures, and name the stages of a project.
New words
threshold · the value in a condition at which the decision changes, such as 10 in temperature() < 10gesture · a movement of the micro:bit that the accelerometer can notice, such as shakesimulator · a program that imitates a device or situation so that people can practise safelysuccess criteria · the list of things the finished project must do
Explanation
A micro:bit has sensors that send numbers or True/False answers to the program. The program keeps asking its question inside while True: and uses if and else to choose the output. Indentation and the colon show which lines belong to the if. Image names such as Image.YES use capital letters, and text such as "Go" needs quotation marks. A project has five stages: plan, design, create, test and evaluate. You write the success criteria in the plan, draw the flowchart in the design and work out the expected outcomes for the test plan before you run anything. A simulator lets you try a program when the real device is not at hand.
Sensor or input
MicroPython
What comes back
temperature
temperature()
a whole number of degrees Celsius
light
display.read_light_level()
a number from 0 to 255
button A or B
button_a.is_pressed()
True or False
touch logo
pin_logo.is_touched()
True or False
movement
accelerometer.was_gesture("shake")
True or False
Worked examples
Question
Answer
goal
show whether the guinea-pig hutch is warm enough
sensor
temperature sensor
comparison and threshold
temperature() < 10
output if yes
a cross (too cold)
output if no
a tick
Before a program is typed, the problem is broken down. This is the plan for a sign on a guinea-pig hutch. Every row answers one question, and the last three rows are exactly what the flowchart and the program need.
a cross below 10 degrees; a tick at 10 degrees or more
This is the program for the plan. The threshold is 10, and the operator < means that exactly 10 degrees is not too cold. So at 10 degrees the else branch runs and the board shows a tick.
Three if statements in one loop. Press A and a heart appears. Shake the board and the smile replaces it. Press B and the screen clears. The events A, then shake, then B, then A again give the pictures: heart, smile, blank, heart.
Class activity
Paper micro:bit (no computer): draw a 5 × 5 grid and two buttons on paper. A partner reads out the program in the third example one line at a time and then calls events, for example ‘A is pressed’, ‘the board is shaken’ and ‘B is pressed’. After each event you draw what the lights show. Then swap roles and make up a new event list with at least five events that includes pressing A twice in a row. If you have a computer or tablet, you can try the program in an online micro:bit simulator with an adult.
Practice
1
What does the board show when the light level is 119, 120 and 121?
At 119 the condition >= 120 is false, so the board shows the small square. At 120 it is true, because >= includes 120, and at 121 it is true too. So it shows the big square both times.
2
The plan says: when B is pressed, scroll the word Safe, wait 2 seconds and then show a tick. Find four mistakes and write the correct program.
1 The if line needs a colon at the end (a syntax error). 2 Safe needs quotation marks: "Safe". 3 sleep(2) waits only 2 milliseconds, so it must be sleep(2000) (a logic error). 4 Image.Yes must be Image.YES, in capital letters.
3
Write the MicroPython program for this flowchart.
Program (each → is 4 spaces of indentation): 1 from microbit import * | 2 while True: | 3 → if button_a.is_pressed(): | 4 → → display.scroll("Go") | 5 → → display.show(Image.ARROW_E) | 6 → else: | 7 → → display.show(Image.SQUARE_SMALL)
4
The hutch sign shows a cross when temperature() < 10 and a tick otherwise. Write the expected outcomes by hand. Then say which test fails if the programmer wrote <= 10 by mistake.
Test
Test data
Expected outcome
1
temperature 0
?
2
temperature 9
?
3
temperature 10
?
4
temperature 25
?
Test 1: cross. Test 2: cross. Test 3: tick. Test 4: tick. With <= 10, test 3 fails: the board shows a cross at 10 degrees. The boundary test finds the bug.