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 CASE statement.

  • Keep transitions and actions apart: the CASE changes 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.

StepNameOutput onLeaves when
0Idle—Start is pressed
10FillFILL_PUMPHIGH_LEVEL: the level reaches 90 %
20HeatHEATERHeatTimer.Q: 3 s have passed
30DrainDRAIN_VALVELOW_LEVEL: the level falls to 20 %
90FaultFAULT_LAMPStop 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 CASE decides whether the active step is done, and changes STEP. Nothing else.

  • Actions: after the CASE, one assignment per output, such as FILL_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. CASE runs 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 IF comes after the CASE, 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. The ELSE branch 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 ELSE branch: 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, the CASE runs 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 Q is 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 CASE on 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.