What you learn today
- Build a reusable template
- Settings block
- Documentation
This year you have written dozens of two-point controllers: a heater here, cooling there, almost the same code every time. This week you build the template you will copy from now on. A good template has three parts: a settings block holding everything you need to adjust, logic you never have to touch again, and documentation that says how to use it.
The idea
A subroutine regeln handles one control loop. What it needs, it gets in registers:
| Register | Content |
|---|---|
name | name of sensor and device, e.g. HASH("Wohnraum") |
typ | prefab of the device: heater or cooler |
low, high | limits in Kelvin |
direction | 1 = device heats, 0 = device cools |
When heating, “too cold” switches on and “too warm” off, when cooling it is exactly the other way round. For that the subroutine swaps the roles of ein and aus with two select – after that the actual hysteresis is the same line for both directions.
The state lives in the device
Every control loop needs its own memory: was the device on? Instead of spending registers or the stack on it, the subroutine simply reads the state back from the device:
lbn on typ name On Average # the state lives in the device itself
snan tmp on
select on tmp 0 on
That way every further loop costs only six lines in the main part, and the subroutine stays unchanged.
Safe state per direction
If a loop's sensor is missing, a heater switches off and a cooler on. The display “Regler” shows how many loops are currently running without a sensor.
The setup
| Device | Prefab | Name |
|---|---|---|
| gas sensor and wall heater | StructureGasSensor, StructureWallHeater | “Wohnraum” |
| gas sensor and wall cooler | StructureGasSensor, StructureWallCooler | “Kuehlraum” |
| LED display | StructureConsoleLED5 | “Regler” |
Milestones
- The living room heats with hysteresis (19/21 °C).
- The cold room cools with hysteresis (6/2 °C).
- Sensor failure: heater off, cooler on, the display counts the failed loops.
- The loops are independent: if one fails, the other keeps controlling.
Write into the template's header how to add a third loop: six move lines and one jal regeln. Whoever takes over the template then does not need to read the subroutine at all.
Diagram
Project task
Task
Build the template: settings block, devices and registers are already there. Missing are the two calls in the loop, the display of failed loops and the subroutine regeln, which uses the same hysteresis for heating and cooling.
Milestones
- Milestone 1: living room heats with hysteresis
- Milestone 2: cold room cools with hysteresis
- Milestone 3: sensor failure per direction
- Milestone 4: loops are independent
Hints
Hint 1
Per loop: five move for name, typ, low, high, direction, then jal regeln.
Hint 2
In the subroutine: slt ein t low, sgt aus t high, then swap by direction with select.
Hint 3
Hysteresis: seqz aus aus, and on on aus, or on on ein. On NaN: select on ok on tmp with tmp = seqz direction.
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