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

Sensors make decisions

Week 8 · Unit 7.2: Decomposing problems · 3 lessons
3

Does the code match the plan?

Computing 7: Unit 7.2, pp. 44–81
GoalTrace a sensor flowchart, check the program against it and add a sound output.
New words
import · a line that loads extra commands, such as import musictrace · to follow a flowchart or program step by step using a chosen valueoutput · what a program shows, says or does, such as a picture or a tuneruntime error · a mistake that stops a program while it is running
Explanation

To make a sound, a program must first load the music library with the line import music. Then music.play(music.POWER_DOWN) plays a short tune. A program can have more than one output, such as a picture and a tune. To check a program, trace its flowchart with some sensor values. Then compare the program with the flowchart line by line: the same sensor, the same operator and the same threshold. Common bugs are a wrong operator, a wrong sensor, a missing colon or a missing import. A missing import is a runtime error, because the program starts but stops when it reaches the music line. In the program the whole flowchart sits inside while True, so it is followed again and again.

Starttemperature < 12?yesnoshow a crossplay a tuneEnd
Worked examples
Python
1from microbit import *
2import music
3while True:
4    if temperature() < 12:
5        display.show(Image.NO)
6        music.play(music.POWER_DOWN)
Output
when the temperature is below 12: a cross and a tune
This is the program for the flowchart. A plant-room frost warning shows a cross and plays a tune when it is cold. Notice the import line at the top.
TemperatureQuestion: temperature < 12?Output
5yescross and tune
12nonothing new
20nonothing new
0yescross and tune
Tracing means choosing values and following the flowchart. Only a value below 12 takes the yes path.
BugExampleKind
wrong operator> instead of <logic error
wrong sensordisplay.read_light_level() instead of temperature()logic error
missing colonif temperature() < 12syntax error
missing importmusic.play(...) with no import musicruntime error
When a program does not match its flowchart, look for one of these bugs.
Class activity

Trace race (no computer). Your partner writes eight numbers from 0 to 40 in a row. You trace the frost-warning flowchart for each number and write “cross and tune” or “nothing new” under it. Your partner checks your answers. Then change the threshold to 20 and the operator to <=, redraw the flowchart and trace the same numbers. Which answers changed? Why?

Practice
1
What does this program do when the light level is 99, 100 and 101?
Python
1from microbit import *
2while True:
3    if display.read_light_level() <= 100:
4        display.scroll("Dim")
2
The program starts and works until the temperature goes above 30. Then it stops. Find the bug and say what kind of error it is.
Python
1from microbit import *
2while True:
3    if temperature() > 30:
4        music.play(music.JUMP_UP)
3
The flowchart asks “temperature >= 30?” but the program uses > 30. When do they give different results? Write the correct operator.
Starttemperature >= 30?yesnoshow a crossEnd
4
Write the MicroPython program for this flowchart.
Startboard shaken?yesnoshow a heartplay JUMP_UPEnd