Module 11 · Structured Text basics

IF and CASE

Decisions are where ST becomes much clearer than ladder. Learn IF, ELSIF and ELSE, the trap of values that "stick", hysteresis control, CASE for modes, and a first step sequence: a traffic light.

≈ 40 min6 lessons1 hands-on labQuiz · pass 80 %Free

By the end of this module you can

  • Write IF / ELSIF / ELSE decisions and know which branch runs
  • Tell when a variable assigned inside an IF keeps its value, and use that on purpose
  • Build on/off control with hysteresis
  • Use CASE to select between operating modes and steps
  • Write a simple step sequence with CASE and a timer
IF Level > 90.0 THEN
    Status := 3;            // high alarm
ELSIF Level > 75.0 THEN
    Status := 2;            // high warning
ELSIF Level < 10.0 THEN
    Status := 1;            // low
ELSE
    Status := 0;            // normal
END_IF;

The conditions are tested from the top; the first true one runs its statements and the rest are skipped. ELSIF and ELSE are optional. Order matters: if the test for > 75.0 came first, a level of 95 would never reach the alarm branch.

A variable that is only assigned inside an IF is not reset when the condition becomes false: it simply isn't written, so it keeps its last value. Below, Lamp_A uses a Boolean assignment and Lamp_B an IF. Switch both switches on, then one off.

Use it on purposeSometimes you want a value to stick: a latch, a remembered fault, or the heater in the next lesson. Then make it obvious with a comment. Otherwise, assign the variable in every branch, or write it as one Boolean assignment.

"Heater on below 60 °C, off above 60 °C" would switch the heater many times a second around 60 °C, wearing out the contactor. Use two thresholds instead: switch on below SP − 2, off above SP + 2, and in between leave it as it is. Here the sticky value is exactly what you want.

When a decision depends on one integer value, such as a mode selector or a step number, CASE is cleaner than a chain of ELSIFs:

CASE Mode OF
    0:      Motor := FALSE;  Lamp_Off := TRUE;      // OFF
    1:      Motor := Jog_PB;                        // MANUAL: jog
    2:      Motor := Auto_Request;                  // AUTO
ELSE
    Motor := FALSE;  Mode_Fault := TRUE;            // unexpected value
END_CASE;
  • A branch can list several values: 3, 5, 7:. Most PLCs also accept ranges: 10..19: (the Logivyx Soft-PLC accepts lists of single values, not ranges).
  • Only one branch runs. If no label matches, ELSE runs; without ELSE nothing does.
  • Always handle unexpected values with ELSE when outputs depend on the CASE.

Note the trick at the top: give each output a default value first, then let the CASE overwrite it. That avoids the sticky-value trap without repeating every output in every branch.

CASE on a step number plus a timer is the simplest way to write a sequence in text. Each step sets the outputs, waits, then moves to the next step. Steps are numbered in tens so new ones can be inserted later. (Level 4 shows the same idea graphically as SFC.)

T_Step(IN := FALSE, PT := …) resets the timer on each step change so the next step starts timing from zero. Try adding step 50, a blinking amber for night mode selected by a second switch.

Use PLC_Trainer. SW1 and SW2 form the mode selector, PB1 = jog, SW3 = auto request, Q1 = motor, N1 = mode.

Lab

L11_Modes

  1. New ST routine L11_Modes. Declare the inputs SW1, SW2, SW3, PB1, the outputs Q1, Q5, Q6, N1, and a local Mode : DINT;
  2. Derive the mode: IF SW1 THEN Mode := 2; ELSIF SW2 THEN Mode := 1; ELSE Mode := 0; END_IF; and copy it to N1.
  3. Write defaults Q5 := FALSE; (OFF lamp) and Q6 := FALSE; (fault lamp), then the CASE from this module with Q1 as the motor, PB1 as jog and SW3 as auto request.
  4. Validate, Auto-bind by Name, Save & Close. Call it from Main, save, F5, and test all three modes.
  5. Second routine L11_Traffic: the traffic light on Q2 (red), Q3 (amber), Q4 (green), run by SW4. Copy the code from this module and rename the variables. Add CALL L11_Traffic; to Main as well.
  6. Run both. Open 🧮 Logic Diag and watch Step change.