Week 9 · Unit 7.2: Decomposing problems · 3 lessons
2
Break the problem down
Computing 7: Unit 7.2, pp. 44–81
GoalDecompose a smart-device problem into sensor, question, outputs and a variable, and plan it as a flowchart.
New words
decompose · to break a problem into smaller parts that are easier to solvethreshold · the value that a sensor number is compared withsnake_case · a variable name written in lower case, with underscores in place of spaces, such as light_levelcamelCase · a variable name with no spaces, where each word after the first starts with a capital letter, such as lightLevel
Explanation
Planning a smart device starts by decomposing the problem into small questions. What should the device do? Which sensor gives the data? What question compares the data with a threshold? What is the output if the answer is yes, and what is the output if it is no? Then you draw the flowchart. The sensor reading is stored in a variable, so that the program can use it. A variable name cannot have spaces, and it should tell the reader what is stored. Choose one style and keep to it: snake_case, such as light_level, or camelCase, such as lightLevel. The light level runs from 0 to 255, so a number near the middle is a sensible first guess for a threshold, and testing may change it.
Question to ask
Why
What should the device do?
this is the goal
Which sensor gives the data?
the input
What is the comparison and threshold?
the decision
What is the output if yes?
one path
What is the output if no?
the other path
Worked examples
Question
Ravi’s answer
goal
show whether the plant has enough light
sensor
light sensor
comparison and threshold
light_level < 80
output if yes
a cross (not enough light)
output if no
a tick (enough light)
Ravi wants a smart plant shelf that tells him whether a plant has enough light. He breaks the problem down like this.
Now Ravi draws the flowchart. The first process box stores the sensor reading in the variable light_level. The decision uses the variable.
a cross when the level is below 80; a tick when it is 80 or more
Here is the plan as a MicroPython program. The variable name matches the flowchart, and the loop checks the light again and again.
Class activity
Decompose a device (no computer). Choose one idea: a bread box that warns when it is too warm, a bedroom sign that tells you when the room is too cold, or a classroom sign that shows when the room is too bright. Answer the five questions of the plan on paper and choose a sensible threshold. Draw the flowchart with a variable name in your own style. Then give your plan to a partner. They check that it has one sensor, one question with a threshold and two outputs.
Practice
1
A classroom sign should show a cross when the room is colder than 18 degrees, and a tick if not. Complete the plan.
Question
Answer
goal
?
sensor
?
comparison and threshold
?
output if yes
?
output if no
?
Goal: show whether the room is warm enough. Sensor: temperature sensor. Comparison and threshold: temperature < 18. Output if yes: a cross. Output if no: a tick.
2
Which of these are good variable names for the light sensor reading? Give a reason for each.
Name
Good?
light level
?
light_level
?
lightLevel
?
q
?
light level: not allowed, because it has a space. light_level: good (snake_case). lightLevel: good (camelCase). q: allowed but poor, because it does not describe the data.
3
Write the MicroPython program for this flowchart for a cool bag.
Program (each → is 4 spaces of indentation): 1 from microbit import * | 2 while True: | 3 → bag_temp = temperature() | 4 → if bag_temp > 8: | 5 → → display.show(Image.NO) | 6 → else: | 7 → → display.show(Image.YES)
4
Explain why a variable name such as light_level is better than x in a smart-device program.
The name tells anyone who reads the program what the variable stores. With x you have to work it out, and mistakes are easier to make when you change the program later.