LabVIEW

cancel
Showing results for
Did you mean:

Solved!
Go to solution

synchronise loop with computer time

I would like to run a while loop exactly every 1 minute, with the minute occuring as per the computer time at 0 seconds.

a timed loop runs a minute after the loop is started and is not syncronised with the 0 seconds of the computer time.

how do I achieve this? I realise the first loop will be less than 1 minute due to the synchonisation.

Message 1 of 6
(1,422 Views)

Re: synchronise loop with computer time

Use the current computer time and compare the number of seconds to zero.  If it is, do whatever it is you want to do in the While Loop.  If it isn't, then execute a false case of a case structure that does nothing.

Message 2 of 6
(1,414 Views)

Re: synchronise loop with computer time

I thought of doing this, but it then the main loop needs to run  at 1 second, when I only need it to be 1 minute.

is there another way?

Message 3 of 6
(1,411 Views)

Re: synchronise loop with computer time

Why can't the main loop run at 1 second?

Just let it do nothing the other 59 seconds.  You really don't want slow running while loops because if the user hits stop, it will take 1-2 minutes for the loop to realize this and stop the while loop.

Message 4 of 6
(1,406 Views)
Solution
Accepted by topic author hydzik

Re: synchronise loop with computer time

thanks, that helped me.

i was initially thinking the 1 second loop would consume additional resources, but realised this is negligible.

I ended up using quotient/remainder on the computer time in seconds and checking when the remainder was 0, as this allowed for second interval.

Message 5 of 6
(1,348 Views)

Re: synchronise loop with computer time

Technically there's a miniscule change you can miss a minute by this approach. As the windows timer isn't perfect and due to the multitasking the 1000ms wait can be a couple ms longer in some cases (not all the time). If this happens you could end up with the ms timer reading just before and after the '0'-second.

(It's basically the Nyquist theorem applied on these timings)

The solution is simple, check at a time that cant miss, e.g. 750ms. 🙂

If checking <500ms you'll have to handle the case of 2-3 hits during the same second.

A common solution is to store and compare to last used time, and when >=1min has passed, do action, set new last time.

/Y