Search code examples
timerplcstcodesysstructured-text

Pausing Timers In Structured Text


I need to be able to pause a timer and retain it's ET value when the timer is no longer being asked to run. The timer times when the input from a proximity switch is not present, but I only want it to time when the pump that forwards on the material is running. The pump may only run for 30 seconds, but the prox switch may be 120 seconds worth of pumping away, so it would take 4 runs of the pump before any material would be detected.

I'm using Codesys v2.3 if that helps

So far I have:

IF Motor AND NOT Proxy.P1 THEN (*If the motor is running and the proxy doesn't energise, then start that proxy's timer*)
    Proxy.P1_Timer.IN:= TRUE;
ELSE
    Proxy.P1_Timer.IN:=FALSE;
END_IF

But the above scenario will cause the ET value to reset when Motor goes off also, not only when Proxy.P1 becomes TRUE. The ET should only reset when Proxy.P1 is set to TRUE

Any advice on this? I'm surprised that there isn't just a retain option on the FB.


Solution

  • to pause the timer I would use the following code

    rEdgeTrig(CLK:=startTimer);
    
    IF rEdgeTrig.Q THEN
        actualTime := actualTime - eTime;
    END_IF;
    
    (* TON timer *)
    timer(IN:=startTimer,PT:=actualTime);
    
    IF startTimer THEN
        eTime := timer.ET;
    END_IF;
    
    if timer.Q then
        acutalTime:=initialTime;
    end_if;
    

    when your timer is no longer being asked to run then load the elapsed time to a variable. Then subtract from the preset time when you want to start again.