CASE and machine sequencing in Structured Text
The Core Idea
A CASE statement selects a branch from the value of a variable. Using a stored step number makes it a clear way to express a machine sequence, with work and transition conditions for each step.
What you will learn
Run a machine one step at a time with a step number and a
CASEstatement.Keep transitions and actions apart: the
CASEchanges the step, and each output is written from the step on every scan.Give the sequence a defined reset, and send a step number no branch handles to a fault step.
Why it matters
A sequence needs a defined active step, a reason to advance, and a way to recover from interruption. Explicit branches make those requirements easier to inspect.
The problem
This is the batch tank from Sequencing in Ladder, written in Structured Text. The lesson simulator runs the tank beside your program: the fill pump and drain valve move the level, and the level switches report it.
| Step | Name | Output on | Leaves when |
| 0 | Idle | — | Start is pressed |
| 10 | Fill | FILL_PUMP | HIGH_LEVEL: the level reaches 90 % |
| 20 | Heat | HEATER | HeatTimer.Q: 3 s have passed |
| 30 | Drain | DRAIN_VALVE | LOW_LEVEL: the level falls to 20 % |
| 90 | Fault | FAULT_LAMP | Stop is pressed |
Stop returns the sequence to Idle from any step. A step number that no branch handles goes to the Fault step.
CASE STEP OF runs exactly one branch: the one whose label matches STEP. Each branch waits for its step's condition and then moves STEP on. The ELSE branch runs when no label matches. The heat timer is called before the CASE with IN := STEP = 20, so it runs only in Heat and resets when the batch leaves, as in the timers lesson. The simulator scans every 100 ms.
In this program, each step turns on the next step's output as it moves on. After you lock in your prediction, press START and watch the tank through a batch.
See it go wrong
Now press STOP. STEP goes to 0, Idle, but look at the outputs.
Fix it
Change the program so the CASE only changes STEP, and every output follows the step. The checks run a full batch, press Stop partway through one, and hold Start and Stop together. Hidden checks also run a second batch and start from a step number no branch handles. Editing pauses the program, so press Run to try your version.
Passing the checks records Structured Text progress on the shared sequences concept.
What happened and why
The original program wrote outputs only at the moment a step changed. An output then stayed at whatever the last write left it, so every step had to remember to undo what earlier steps did, and Stop, which only changes STEP, undid nothing.
The fix splits the program in two:
Transitions: the
CASEdecides whether the active step is done, and changesSTEP. Nothing else.Actions: after the
CASE, one assignment per output, such asFILL_PUMP := STEP = 10;. These run on every scan, so an output is TRUE exactly while its step is active.
With the actions written this way, no step can leave an output on behind it. Stop and the Fault step turn everything off without a line of their own.
Three more rules make the sequence dependable:
One transition per scan.
CASEruns a single branch per scan. If a step's condition is already TRUE when the step begins, the next transition still waits for the next scan, so every step is active for at least one full scan.A defined reset. The Stop
IFcomes after theCASE, so in a scan where a transition and Stop both happen, Stop wins. It changes the step, not just the outputs, so nothing resumes when Stop is released.No unknown steps. A wrong number in
STEP, from a mistyped transition or a bad write, matches no label. TheELSEbranch sends it to the Fault step, which drives no outputs and waits for a person.
Common mistakes
Turning outputs on in one step and off in another: any path that skips the "off" line, including Stop, leaves the output on. Write every output from the step.
Leaving out the
ELSEbranch: an unknown step number matches nothing, and the sequence sits with no step active and no alarm.Checking Stop before the
CASE: in a scan where Start and Stop are both TRUE, theCASEruns after Stop and starts the batch anyway.Calling the step timer inside its step: the timer never sees its input go FALSE, so on the next batch
Qis already TRUE, and Heat lasts one scan.
Platform notes
IEC 61131-3 (the international standard for programmable controller languages) also defines Sequential Function Chart (SFC), a graphical language built for step sequences. A
CASEon a step number is the common way to write the same thing in Structured Text, and it works on every platform.Many sites number steps in tens, as here, so a step can be added later without renumbering the others.
Finish the unit
This is the last lesson in Practical control. The unit capstone is a ST Studio problem. It asks for a three-state sequence with CASE, where Reset wins over every other input. The card shows Done once every check passes.
Enable JavaScript to run the embedded simulator and complete the interactive exercises.