'PLC SCL keeping variable true

So I am writing a PLC code in SCL language and I am wondering about this thing:

I have a tank with an inlet and a pump for flow out. The pump should only run after the level in the tank goes beyond a level, lets say 10 meters. And it should keep going until the tank level goes down to 4 meters.

Not sure if I should use more IF statements or maybe a WHILE statement?. Problem is, how to keep the pump going after the level has gone under 10 meters but not yet reached 4 meters?

This code would probably not be useful:

IF (TankLevel > 10) THEN
      StartPump := TRUE;
END_IF;

Any help is appreciated.



Solution 1:[1]

Just add the following:

IF (TankLevel < 4) THEN
      StartPump := FALSE;
END_IF;

Solution 2:[2]

Another option, although this is more suited to ladder logic:

StartPump := ( (TankLevel > 10) OR StartPump ) AND NOT (TankLevel < 4);

The StartPump bit will be "latched" at 10m until it is released at 4m.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 pboedker
Solution 2 kolyur