13Control flow
The CASE statement and your first state machine
CASE OF with integer labels, ranges, comma lists and ELSE, then a real four-state sequence: idle, fill, hold, drain.
When CASE beats IF
When you test one variable several times. An ELSIF ladder repeats the variable name in every branch; CASE names it once:
CASE State OF
0: Pump := FALSE;
1: Pump := TRUE;
2: Pump := FALSE;
ELSE
State := 0;
END_CASEThe selector must be an integer. Not a real, not a boolean.
Label forms
Form | Meaning |
|---|---|
| one value |
| a range, both ends included |
| a list of values |
| a range and a value together |
| everything the labels did not cover |
CASE Step OF
0: Lamp := FALSE;
1..3: Lamp := TRUE; (* all the filling steps *)
10, 20: Lamp := Blink;
ELSE
Step := 0; (* unexpected value? go to a known point *)
END_CASEAlways write the ELSE. If memory is corrupted or an HMI writes a value you did not plan for, ELSE is what puts the machine back into a safe state instead of leaving every branch dead.
The state machine
A state machine is only three things:
- An integer variable holding the current state.
- A
CASEwith one branch per state: what the outputs do here. - Inside each branch, a transition condition that writes the next state number.
Our tank sequence:
State | Name | Outputs | Transition |
|---|---|---|---|
0 | idle | everything off | |
1 | fill | pump running | |
2 | hold | everything off | timer done to 3 |
3 | drain | valve open | |
Rules that save you
- Write the outputs in every branch, not only where they are needed. Outputs hold their value, so a pump started in state 1 keeps running in state 2 unless you switch it off explicitly.
- Put the stop above the
CASE, not inside it. One condition before the construct returns the machine to zero from any state. - Do not cross two states in one scan unless you mean to. Here you cannot, because the selector is read once per scan.
- Numbers or names? On a real PLC you declare an enumerated type (
TYPE ... END_TYPE) and writeFillinstead of1. The lab does not supportTYPE, so we use numbers and document them in a comment.
A timer inside the sequence
The hold state needs time. Call the timer outside the CASE and feed it from a state comparison:
Hold(IN := State = 2, PT := T#3s);That way the timer is called every scan as it must be, starts by itself when state 2 is entered, and resets when it is left.
Try it in the lab
The lab runs in your browser: an ST editor, a virtual process that suits the lesson, the same program as a live ladder diagram, a watch table, and export to Siemens SCL.
What to try
- Run the program and follow State in the watch table: 0, 1, 2, 3, then back.
- Notice pedestrians walk only in state 2, while the light is red.
- Change state 0 to T#10s and download again.
- In the ladder notice how each CASE arm became a State = n comparison.
Terms in this lesson
People find this lesson by searching
CASE statement structured textplc state machine structured textCASE OF END_CASEcase range 1..3 sttank fill sequence plcsequence programming plcstate machine example st
Course outline
- 01What is Structured Text, and when to use it instead of ladder
- 02How a PLC runs your code: the scan cycle and the process image
- 03Program structure: POUs, PROGRAM, FUNCTION and FUNCTION_BLOCK
- 04Your first program: starting a pump with seal-in logic
- 05Data types: BOOL, INT, REAL, TIME and the rest
- 06Declaring variables: VAR, VAR_INPUT and RETAIN
- 07Literals and constants: 16#FF, 2#1010 and T#5s
- 08Type conversion and scaling a 4-20 mA signal
- 09Assignment and arithmetic in ST: := and operator precedence
- 10Boolean logic, comparisons and bit operations
- 11Standard functions: ABS, SQRT, MIN, MAX, LIMIT, SEL, MUX
- 12IF, ELSIF and ELSE: the rules and when to avoid them
- 13The CASE statement and your first state machine
- 14The FOR loop and the watchdog
- 15WHILE, REPEAT, EXIT, CONTINUE and RETURN
- 16Edge detection: R_TRIG, F_TRIG and the "it fired every scan" bug
- 17Timers: TON, TOF and TP
- 18Counters: CTU, CTD and CTUD
- 19SR and RS flip-flops: which one wins
- 20Writing your own function
- 21Writing a function block
- 22Arrays in Structured Text
- 23Structs and enumerations
- 24References and pointers
- 25The state machine pattern: a batch sequence that cannot get stuck
- 26Alarm management to ISA-18.2: delay, deadband, latch, acknowledge, first-out
- 27Analogue signal processing: counts to engineering units, NE43 and filtering
- 28Closed-loop control in ST: hysteresis first, then a practical PID
- 29Motors and valves in practice: permissives, feedback and travel time
- 30The same ST on every platform: SCL, Studio 5000, CODESYS, TwinCAT
- 31Testing and debugging ST: the watch table, forcing and simulation
- 32An ST style guide: naming, comments, constants and version control
- 33Final project: a complete tank station with a state machine and alarms
- 3420 interview questions on ST and PLC programming, with answers