What you learn today
- State in a register
- Transitions with conditions
- Display the state
A tank is to be filled at the push of a button. The pump runs until 3000 kPa are reached. After that it stays off – even if someone takes gas out and the pressure drops to 2500 kPa. Only when the tank is almost empty is the plant ready again, waiting for the next push.
“Pressure too low → pump on” does not get you far here. At 2500 kPa the pump should run once (right after the push) and once not (after filling). The reading alone is not enough – the chip has to remember what it is doing right now.
The state is a number
You give every stage of the process a number and keep it in a register:
| State | Number | Pump |
|---|---|---|
| Ready | 0 | off |
| Filling | 1 | on |
| Full | 2 | off |
define READY 0 # waiting for start
define FILLING 1 # pump running
define FULL 2 # tank full
alias state r0
The numbers as define make the code readable: everyone understands seq tmp state FILLING, nobody understands seq tmp state 1. State 0 is the safe starting state on purpose, because every register starts at 0.
Transitions: state and condition
A transition always has two parts: which state you are in and what has to happen. Only when both are true does the state change.
| from | condition | to |
|---|---|---|
| Ready | button “Start” pressed | Filling |
| Filling | pressure ≥ 3000 kPa | Full |
| Full | pressure < 1000 kPa | Ready |
Every row of the table turns into a few lines of code:
seq tmp state READY # am I in Ready?
and tmp tmp pressed # ... and is Start pressed?
select next tmp FILLING next # then go to Filling
That is the core idea: every tick reads the state and decides on the transitions. What the pump does then follows from the state alone:
seq pumpOn state FILLING # on only in state Filling
sbn PUMP HASH("Fuellpumpe") On pumpOn
Work out all transitions from the old state and take over the new one only at the end: move next state at the start, move state next at the end. If you change state directly, the next line already checks the new value. If the line for Ready → Filling sits below the one for Full → Ready, a held button turns Full straight back into Filling within a single tick.
Showing the state
An LED display with the state number is the cheapest debugging there is. If it shows 1 and the pump is not running, the fault is the pump or its name – not your logic.
sbn DISPLAY HASH("Status") Setting state
If the analyzer fails, the pressure is NaN. Then the machine never leaves Filling and the pump runs forever. Send it back to Ready in that case – snanz ok pressure gives you a clean 0 or 1 for it.
Exercise
Task
Build the tank filler from the text. Devices on the data network:
- Button “Start” (
Activate) - Pipe analyzer “Tank” (
Pressure) - Volume pump “Fuellpumpe” (
On) - LED display “Status” (
Setting= state number)
Transitions: Ready → Filling on a button press, Filling → Full from 3000 kPa, Full → Ready below 1000 kPa. Without the analyzer back to Ready. The pump only runs in state Filling.
Inputs and display are done. The transitions and the pump are missing.
Hints
Hint 1
Every transition: seq tmp state FROM, condition into cond, and tmp tmp cond, then select next tmp TO next.
Hint 2
For Ready → Filling pressed already is the condition: and tmp tmp pressed.
Hint 3
Without the analyzer: select next ok next READY as the last transition line.
Hint 4
Pump: seq pumpOn state FILLING and write it to “Fuellpumpe” with sbn.
Quiz
4 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