Skip to content

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.

Lesson 13 of 3412 minIEC 61131-3

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_CASE

The selector must be an integer. Not a real, not a boolean.

Label forms

Form

Meaning

3:

one value

1..3:

a range, both ends included

2, 5, 9:

a list of values

10..19, 30:

a range and a value together

ELSE

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_CASE

Always 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:

  1. An integer variable holding the current state.
  2. A CASE with one branch per state: what the outputs do here.
  3. Inside each branch, a transition condition that writes the next state number.

Our tank sequence:

State

Name

Outputs

Transition

0

idle

everything off

Start to 1

1

fill

pump running

LSH to 2

2

hold

everything off

timer done to 3

3

drain

valve open

LSL to 0

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 write Fill instead of 1. The lab does not support TYPE, 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

  1. Run the program and follow State in the watch table: 0, 1, 2, 3, then back.
  2. Notice pedestrians walk only in state 2, while the light is red.
  3. Change state 0 to T#10s and download again.
  4. 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

  1. 01What is Structured Text, and when to use it instead of ladder
  2. 02How a PLC runs your code: the scan cycle and the process image
  3. 03Program structure: POUs, PROGRAM, FUNCTION and FUNCTION_BLOCK
  4. 04Your first program: starting a pump with seal-in logic
  5. 05Data types: BOOL, INT, REAL, TIME and the rest
  6. 06Declaring variables: VAR, VAR_INPUT and RETAIN
  7. 07Literals and constants: 16#FF, 2#1010 and T#5s
  8. 08Type conversion and scaling a 4-20 mA signal
  9. 09Assignment and arithmetic in ST: := and operator precedence
  10. 10Boolean logic, comparisons and bit operations
  11. 11Standard functions: ABS, SQRT, MIN, MAX, LIMIT, SEL, MUX
  12. 12IF, ELSIF and ELSE: the rules and when to avoid them
  13. 13The CASE statement and your first state machine
  14. 14The FOR loop and the watchdog
  15. 15WHILE, REPEAT, EXIT, CONTINUE and RETURN
  16. 16Edge detection: R_TRIG, F_TRIG and the "it fired every scan" bug
  17. 17Timers: TON, TOF and TP
  18. 18Counters: CTU, CTD and CTUD
  19. 19SR and RS flip-flops: which one wins
  20. 20Writing your own function
  21. 21Writing a function block
  22. 22Arrays in Structured Text
  23. 23Structs and enumerations
  24. 24References and pointers
  25. 25The state machine pattern: a batch sequence that cannot get stuck
  26. 26Alarm management to ISA-18.2: delay, deadband, latch, acknowledge, first-out
  27. 27Analogue signal processing: counts to engineering units, NE43 and filtering
  28. 28Closed-loop control in ST: hysteresis first, then a practical PID
  29. 29Motors and valves in practice: permissives, feedback and travel time
  30. 30The same ST on every platform: SCL, Studio 5000, CODESYS, TwinCAT
  31. 31Testing and debugging ST: the watch table, forcing and simulation
  32. 32An ST style guide: naming, comments, constants and version control
  33. 33Final project: a complete tank station with a state machine and alarms
  34. 3420 interview questions on ST and PLC programming, with answers