RTO Timer Code for Arduino OPTA PLC

Since Allen Bradley PLC have a Retentive Timer and other PLC systems do as well it appears that Arduino Opta/Finder PLCs does not I was able to successfully create a Retentive Timer and thought I'd share how to do it so other people can Recreate it in their Projects as well if they need one of these timer types.

Create a New Function Block in ST Text and use the following Code:

Local Variables to be added into the Function Block

Variable Class        Pin        Name          Type          Init Value
VAR_Input               0           Start            Bool
VAR_Input               2           Reset           Bool
VAR_Input               1           PresetTime Int
VAR_Output            1          AccumTime Int
VAR_Output            0          TimerDone  Bool
VAR                                       PrevStart     Bool
VAR                                       TimerEnable Bool     
VAR                                       CycleTime     Int             10
VAR                                       TimerTON     TON
(* Timer function to measure elapsed time in small increments *)
TimerTON(IN := TimerEnable, PT := CycleTime);

(* Reset logic *)
IF Reset THEN
    AccumTime := 0;
    TimerDone := FALSE;
ELSIF Start AND NOT TimerDone THEN
    (* Enable timer accumulation when Start is pressed *)
    TimerEnable := TRUE;
    
    (* When the timer reaches cycle time, add the elapsed time *)
    IF TimerTON.Q THEN
        AccumTime := AccumTime + CycleTime;
        TimerTON(IN := FALSE); (* Restart the timer *)
    END_IF;
ELSE
    (* Stop the timer but retain AccumTime *)
    TimerEnable := FALSE;
END_IF;

(* Check if accumulated time reaches preset *)
IF AccumTime >= PresetTime THEN
    TimerDone := TRUE;
    TimerEnable := False;
END_IF;

(* Store previous Start state *)
PrevStart := Start;

the Above Code will count up to the time that you feed into the Preset Time Variable and then stop counting. If the input to the RTO Goes Low it will stop counting and when it goes high again it we continue counting where it stopped out. Once the Count has finished the TimerDone bit will go true.

to reset the timer to allow it to count again you need to pulse the reset input of the RTO Function Block

1 Like