What you learn today
- One subroutine for all rooms
- Pass names via registers
- Save lines
Lab, kitchen, store: three rooms, three times the same hysteresis. Copied, that makes three times eight lines and three chances to copy a limit wrongly. With a subroutine the rule exists once, and the rooms call it one after another.
The setup
| Room | gas sensor | wall heater | stack slot |
|---|---|---|---|
| lab | Labor | Labor | 0 |
| kitchen | Kueche | Kueche | 1 |
| store | Lager | Lager | 2 |
Sensor and heater of a room carry the same name, the type tells them apart. In addition there is a flashing light “Heizstoerung” as a collective alarm.
Passing names via registers
lbn and sbn also take the name hash from a register. So the subroutine gets the room in name:
move name LAB
move nr 0
jal room
Inside the subroutine it then says lbn temp SENSOR name Temperature Average and sbn HEATER name On heat. Which room is meant is decided solely by the content of name.
Remembering the state per room
A hysteresis needs a memory. One register for all three rooms is not enough, so every room gets a slot in the stack, its number is in nr:
room:
get heat db nr # fetch this room's state
# ... hysteresis ...
put db nr heat # and write it back
sbn HEATER name On heat
j ra
Milestones
- Lab heats with hysteresis: on below 19 °C, off from 21 °C, keep the state in between.
- Three rooms independent: every room controls by its own sensor without disturbing the others.
- One sensor fails: only its heater goes off, the collective alarm lights up.
- Everything fine: if all sensors are there, the collective alarm is off.
A fourth room only costs three lines in the main loop and one define. That is the whole point: saving lines and characters without losing clarity.
In the game the stack survives an upload. An old heating state at slots 0 to 2 is harmless, because the hysteresis corrects it in the next tick. But if you use the slots for something else, tidy them up with poke before the start label.
Diagram
Project task
Task
Build the multi-room controller with one subroutine room that gets the name in name and the stack slot in nr. Rooms, names and slots are in the table. The collective alarm “Heizstoerung” lights up as soon as a sensor is missing. The milestones are listed above the editor.
Milestones
- Milestone 1: lab heats with hysteresis
- Milestone 2: three rooms independent
- Milestone 3: one sensor fails
- Milestone 4: everything fine, no alarm
Hints
Hint 1
Per room three lines in the main loop: move name …, move nr …, jal room.
Hint 2
In the subroutine: fetch the state with get heat db nr, hysteresis, back with put db nr heat.
Hint 3
Count missing sensors in the subroutine with snan into faults, switch the lamp afterwards with sgtz.
Quiz
2 questions. You pass from 60 % – the server does the grading.
Complete the day
- Passed all milestones of the project
- 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