Showing results for 
Search instead for 
Did you mean: 

Timed while loop


I want the following while loop to execute every 4th second and send the iteration count as second element of an array through Ethernet UDP i.e. on 4th second it should send 1, on 8th second 2 and so on.

Currently, I am not being able to increment the iteration count every 4th second as the while loop is running continuously.

Any help would be highly appreciated.



0 Kudos
Message 1 of 3

Hi, your approach seems to be wrong.

Instead of trying to run the while loop every 4th second, use the case structure to run specific actions depending on the "Elapsed Time" function.

You will also need to create your own counter, instead of relying on the iteration counter. It is very easy to do using a shift register.

0 Kudos
Message 2 of 3

If you want to do nothing for most of 4 seconds, then send an iteration count through UDP (along with some other stuff), the easiest way to do this (which needs only a While loop, no Case, no For) is to have a Wait (ms) with 4000 wired to it (a Wait of 4 seconds) and the function building and sending data through UDP, using the iteration count as you wish.  You'll, of course, want to put a Stop control on the While loop to end the process.


On the other hand, if you have some process running (in a While Loop) at some unknown rate (presumably much faster than 0.25 Hz), you (obviously) cannot use the Loop Count.  You can, however, initialize a Shift Register to 0, then use logic to decide if you've just crossed the "4-seconds-have-elapsed-since-the-last-time" threshold and, in a Case Statement, send your Count (in the Shift Register), increment the Count (you can do this before or after, depending if you want the first count to be 0 or 1), and do whatever you need to do to reset the Timing logic.


I rarely use Express VIs, and am not that familiar with the ins and outs of the Elapsed Time Express VI, which might handle the timing chores (deciding whether or not do do the Case) for you.  Incidentally, using a Case instead of using a For loop, wiring 0 or 1 to the N input, makes it much clearer what you are doing.


Bob Schor 

0 Kudos
Message 3 of 3