The naive controller says “on below 20 °C, otherwise off”. In practice the temperature wobbles around 20 °C and the heater switches on and off every tick. That is annoying, and some devices wear out or eat start-up power.
Two limits
A hysteresis uses two thresholds:
- below the lower limit: switch on
- above the upper limit: switch off
- in between: leave it as it is
For “leave it” the chip needs a memory – the register holding the current state.
The and/or pattern
alias temp r0
alias heat r1
alias tmp r2
# ...
slt tmp temp UPPER # still below the upper limit?
and heat heat tmp # if not: off. Otherwise keep the state
slt tmp temp LOWER # below the lower limit?
or heat heat tmp # then on in any case
sbn HEATER HASH("Heizung") On heat
Read it as: “The heater stays on as long as it is below UPPER. And it switches on as soon as it is below LOWER.” Four lines, no jump.
The same pattern works for cooling (with sgt instead of slt), pumps, generators and filters. The templates in the IC10 coder use it everywhere.
Sensor gone?
If the sensor returns NaN, both comparisons are false – heat becomes 0. So the heater switches off. That is the safe direction here: better cold than an overheated room.
Diagram
Exercise
Task
Gas sensor “Raum”, wall heater “Heizung” (both on the data network). Build a hysteresis controller:
- below 19 °C (292.15 K) heater on
- from 21 °C (294.15 K) heater off
- in between keep the state
- if the sensor fails, heater off
Hints
Hint 1
Four lines between lbn and sbn are enough: two slt, one and, one or.
Hint 2
First check whether it is still below UPPER and “and” that with the old state.
Hint 3
Then check whether it is below LOWER and “or” that in.