Timer/Counter

Hi everyone, I was wondering what would be the best approach for making a timer/counter? basically start counting when Boolean comes on and stop counting when it goes off (how many seconds took) and I want it to work in the background, I assume the Timer in Gateway even script should be the way to do it right? never used the timer function before so not sure how to proceed, would a simple loop works to start and stop the counter? or is there a better way to do it? Thank in advance

Why not just capture the time when boolean goes on and off and then calculate the difference?

I’m already doing something like that. I was just curious to see if there is a way to set up an actual timer from bit on to bit off.

My most recent version the Simulation Aids module can help create this sort of function. It now supports a ‘state’ dictionary that can carry information about prior executions of the expression it’s in. In your case, it could carry a snapshot of the start time of a pulse, and hold the total after the end of the pulse. Try this expression on an expression tag of type ‘Long’, with a boolean memory tag in the same folder named ‘Enabled’:objectScript('shared.simtest.timeOn(state, args[0])', {[.]Enabled})And in a shared script module (named ‘simtest’ in this example), have the following code:from java.util import Date def timeOn(state, enabled): ts = Date() if 'started' in state: state['result'] = ts.time - state['started'] if not enabled: del state['started'] else: if enabled: state['started'] = ts.time if 'result' in state: del state['result'] if 'result' in state: return state['result'] return 0This will count milliseconds from the leading edge of ‘Enabled’, and hold the value after ‘Enabled’ turns back off. It will be rounded off to the update rate of the scan class.
{Needless to say, if precise timing matters, do it in PLC code.}

@pturmel
Thanks for the reply, I will definitely explore than option down the line. I convinced the AGM to stick with just a time stamp of when the bit comes off and seems to work fine for him so I’m not going push it.
I had the option to have a timer on the PLC side and would have worked great but I’m trying to learn how to do most things on my side as much as possible since I don’t deal with the PLC side yet(hopefully down the line).

1 Like