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.
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.
"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.
L11_Modes
- New ST routine
L11_Modes. Declare the inputsSW1,SW2,SW3,PB1, the outputsQ1,Q5,Q6,N1, and a localMode : DINT; - Derive the mode:
IF SW1 THEN Mode := 2; ELSIF SW2 THEN Mode := 1; ELSE Mode := 0; END_IF;and copy it toN1. - Write defaults
Q5 := FALSE;(OFF lamp) andQ6 := FALSE;(fault lamp), then the CASE from this module withQ1as the motor,PB1as jog andSW3as auto request. - Validate, Auto-bind by Name, Save & Close. Call it from
Main, save, F5, and test all three modes. - Second routine
L11_Traffic: the traffic light onQ2(red),Q3(amber),Q4(green), run bySW4. Copy the code from this module and rename the variables. AddCALL L11_Traffic;to Main as well. - Run both. Open 🧮 Logic Diag and watch
Stepchange.