Menu
CoddyTech

Functions: MIN, MAX, LIMIT

Part of the Structured Text section of Coddy's PLC Programming journey. Lesson 13 of 17.

A function takes values and gives one back, right inside an expression. Unlike a block it keeps nothing between scans, so it needs no VAR line:

  • MAX(a, b): the larger value. MIN(a, b): the smaller.
  • LIMIT(MN, IN, MX): IN, but never below MN or above MX.
  • ABS(x): x without its minus sign.
Speed := LIMIT(0, Setpoint, 100);
Gap := ABS(Level - Target);

MAX with 0 is a floor: MAX(x, 0) is x, but never below 0.

challenge icon

Challenge

Easy

The display shows how many boxes are still needed for a batch of 6, but after the sixth box it goes below zero. Fix it with a function so it stops at 0.

Press Run, try it on the machine, then Check.

Try it yourself

VAR
    C1 : CTU;
END_VAR

Belt := (Start OR Belt) AND Stop;
C1(CU := Entry, PV := 6);

// Boxes still needed for a batch of 6
Display := 6 - C1.CV;

All lessons in Structured Text

Practice on your own: PLC playground