I tried the program the TimeTaken is calculated when stop is triggered. I want the TimeTaken to be continuously calculated and counting when a BOOL variable is true.
// Main program logicIFbResetTHEN// Reset button pressed, reset the stopwatchbRunning:=FALSE;tElapsedTime:=T#0s;ELSIFbStartStopTHEN// Start/Stop button pressed, toggle the running stateIFbRunningTHEN// Stop the stopwatchbRunning:=FALSE;ELSE// Start the stopwatchbRunning:=TRUE;tStartTime:=tElapsedTime;END_IF;END_IF// Update the elapsed time when the stopwatch is runningIFbRunningTHENtElapsedTime:=tElapsedTime+T#1s;// Adjust the time increment as neededEND_IF
However counting of the seconds is not accurate. I tried changing the main task cycle time interval to 1000ms. The counting of seconds become slower. (see attached)
Is it possible to program a stopwatch to measure a system running time when it is turn on, and pause when the system is turn off. I tried your examples when stopped, the elapsed time will then be calculated, but i need the time to be continuously counting.
Warmest regards,
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I am using persistent variables to retain the timer value after machine shutdown, and main task to be as low as possible for the seconds TON to be accurate.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
how to create a stopwatch when start is pressed, timer will start counting and stop is pressed, timer will stop counting in structured text?
and then:
Hi @ph0010421,
I tried the program the TimeTaken is calculated when stop is triggered. I want the TimeTaken to be continuously calculated and counting when a BOOL variable is true.
I tried to program this way:
Declaration:
Implementation:
However counting of the seconds is not accurate. I tried changing the main task cycle time interval to 1000ms. The counting of seconds become slower. (see attached)
Please help
Do you need an 'hours-run' counter? And 1 second resolution is ok? I think you're over-thinking it.
(The task time needs to be < 1second)
Have a look at the LAD...
Then, the time in seconds can be made into hh:mm:ss with this FUNction
Declarations:
and the code:
One i wrote this to measure elepse time
When xMeasure is true is starts en when false it stops and time is messured.
Meaby this will help.
Hi @ph0010421 and @Ton,
Is it possible to program a stopwatch to measure a system running time when it is turn on, and pause when the system is turn off. I tried your examples when stopped, the elapsed time will then be calculated, but i need the time to be continuously counting.
Warmest regards,
Hi,
i guess you want to make a hour counter.
You need to store the measured value.
Try to use a retain variable, but need a graceful shutdown, or save it your self.
see https://forge.codesys.com/forge/talk/Runtime/thread/278e325579/
Succes.
Thanks everyone for your input,
I am using persistent variables to retain the timer value after machine shutdown, and main task to be as low as possible for the seconds TON to be accurate.
If you use LTIME() you will be very accurate and be able to have a normal task cycle time.