GoalUse a sensor reading to control an output, and work out the result of a formula.
New words
light level · a number from the light sensor: 0 is dark and 255 is very brightbrightness · how brightly the LEDs shine: 0 is off and 255 is the brighteston start · a block whose program runs once when the board startsforever · a block that repeats the blocks inside it again and again
Explanation
A sensor gives the program a number, and the program can use that number to control an output. A night light uses the light level, which goes from 0 (dark) to 255 (very bright). The darker the room, the brighter the LEDs should shine. So the program works out brightness = 255 − light level. This calculation is the process. In a dark room the light level is 0, so the brightness is 255. In a bright room the light level is 255, so the brightness is 0 and the moon disappears. The block on start draws a moon once. The forever block keeps measuring the light and changing the brightness.
Worked examples
Room
Light level
Brightness
dark bedroom
0
255
dim hall
100
155
room with a lamp
200
55
sunny kitchen
255
0
Work out the brightness for different rooms with brightness = 255 − light level.
Why subtract from 255? Program A makes the LEDs brightest in a bright room, which is no use for a night light. Program B turns the number round, so the LEDs are brightest in the dark.
light sensor→255 − light level→LED brightness
The night light uses an input, a process and an output. The sensor measures the light, the program subtracts, and the LED display shows the result.
Class activity
Human night light (with family): one person is the light sensor and secretly picks a number from 0 to 255 on a card for “how bright the room is”. The other person is the program and works out 255 minus the number. Then they show the brightness by holding a torch or their hands apart: very close together for a low brightness and wide apart for a high one. Do five rounds and write the numbers in a table. Which light levels give the brightest LEDs?
Practice
1
The light level is 40. What brightness does the night light program set?
255 − 40 = 215.
2
What do the LEDs do when the light level is 255? Why?
They turn off. The brightness is 255 − 255 = 0, which means no light.
3
Complete the table for the night light. Which room gets the brightest moon?
Light level
Brightness (255 − light level)
60
?
120
?
210
?
60 → 195, 120 → 135, 210 → 45. The room with light level 60 is darkest, so it gets the brightest moon.
4
In the night light, which block runs only once at the start and which block keeps repeating? What would go wrong if the set brightness block were inside on start?
on start runs once and forever keeps repeating. Inside on start the brightness would be set only once, so the light would not change when the room gets darker.