So, I want to duplicate the on-delay timers you see in ladder logic. I've figured out a way to do that with an up timer but I wanted to see if anybody had a better solution. Sorry if this was repeated elsewhere. I tried searching but came up empty.
This is what it looks like in ladder. If the condition is true, the timer times up until the preset is reached. Then the timer stops and the Done bit is set. If the condition is false, the timer resets and starts over when the condition becomes true again.
---||------------| T1 |
This is what I've done in OptoScript.
if (not Condition) then
// Stop the timer if the condition is not active
elseif (T1 == 0) then
// Start the timer if the condition is active and it isn't already running (non-zero value)
T1 = Preset
elseif (HasTimerExpired(T1)) then
// Pause the timer if it's expired
Output = HasTimerExpired(T1);
I use a single If-Elseif-Endif structure to start/stop the timer. If the condition is not active, the timer is stopped. If the timer is active and it's value is 0, it's started by assigning it the preset value. If the timer has expired, I pause the timer. I do that because I'm not sure if it will just keep timing up indefinitely.
This seems pretty verbose to me and I was hoping someone had a more elegant solution.