What you learn today
- Read the daylight sensor
- Light when it is dark
- Schedule with a counter
Outside the sun goes down and the path to the landing pad lies in the dark. A daylight sensor (StructureDaylightSensor) tells the chip when it is time for light.
What the sensor reports
| LogicType | Meaning |
|---|---|
Activate | 1 if sunlight falls on the sensor, otherwise 0 |
SolarIrradiance | solar power in W, 0 without sunlight |
SolarAngle | angle to the sun in degrees |
Horizontal, Vertical | direction of the sun in degrees |
For day and night Activate is enough. The value applies to the sensor itself: if it stands in the shadow of a building, it is night for it. Build it in the open, with a view of the sky.
SolarIrradiance tells you more. In bad weather the value drops without Activate jumping to 0. If you want light already under a dull sky, compare this value with a limit.
lbn sun SENSOR HASH("Sonne") Activate Average
seqz dark sun # 1 = no sun
sbn LAMP HASH("Aussenlicht") On dark
On failure, better bright
If the sensor is missing, Average returns NaN and seqz turns it into 0. The light would stay off, and for outdoor lights that is the wrong direction. A light that burns needlessly during the day costs a few watts. A dark path can cost you a suit.
snan missing sun
or dark dark missing # no sensor: light on
A schedule with a counter
Not everything follows the sun. Grow lights, for instance, should have fixed times. For that you count ticks. A pass with one yield lasts one tick, that is 0.5 s:
add time time 1
mod time time PERIOD # 0 to PERIOD-1, then from the start
slt on time ON # the first ON ticks: on
mod resets the counter to 0 after PERIOD ticks. With PERIOD 7200 and ON 4800 you get a one-hour cycle with 40 minutes of light.
A counter can also measure: how long has it been dark? Count up while it is dark and reset it to 0 in sunlight. A multiplication saves the jump:
add ticks ticks 1
mul ticks ticks dark # in sunlight: times 0
In the game the registers keep their values when you upload new code or re-insert the chip. Only sp and the program line go back to 0. So a counter starts with its old value. Set it to 0 with move before the start label. The simulator here always starts with zeros and hides this mistake.
For every device think about which state is safe on a failure. Heater: off. Outdoor light: on. Week 13 covers this in detail.
Exercise
Task
A daylight sensor “Sonne” and wall lights “Aussenlicht” sit on the network.
- If no sunlight falls on the sensor (
Activate= 0), the lights are on, otherwise off. - The LED display “Nachtzeit” shows in seconds how long it has been dark. In sunlight it shows 0.
- If the sensor is missing, the light is on.
Hints
Hint 1
seqz dark sun swaps 1 and 0.
Hint 2
snan missing sun and or dark dark missing switch the light on when the sensor is missing.
Hint 3
Counter: add ticks ticks 1, then mul ticks ticks dark – seconds are ticks times 0.5.
Quiz
5 questions. You pass from 60 % – the server does the grading.
Complete the day
- Passed the simulator exercise
- Passed the quiz with at least 60 %
Without an account progress is saved in this browser only.
Report a mistake in this lesson
Please sign in to report. Sign in