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

Plan and test a smart idea

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 askWhy
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
QuestionRavi’s answer
goalshow whether the plant has enough light
sensorlight sensor
comparison and thresholdlight_level < 80
output if yesa cross (not enough light)
output if noa 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.
Startlight_level = light sensor readinglight_level < 80?yesnoshow a crossshow a tickEnd
Now Ravi draws the flowchart. The first process box stores the sensor reading in the variable light_level. The decision uses the variable.
Python
1from microbit import *
2while True:
3    light_level = display.read_light_level()
4    if light_level < 80:
5        display.show(Image.NO)
6    else:
7        display.show(Image.YES)
Output
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.
QuestionAnswer
goal?
sensor?
comparison and threshold?
output if yes?
output if no?
2
Which of these are good variable names for the light sensor reading? Give a reason for each.
NameGood?
light level?
light_level?
lightLevel?
q?
3
Write the MicroPython program for this flowchart for a cool bag.
Startbag_temp = temperature readingbag_temp > 8?yesnoshow a crossshow a tickEnd
4
Explain why a variable name such as light_level is better than x in a smart-device program.