LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Time delay in while loops

I am using a while loop with a task in it involving the serial port. I only want the task in the loop to run for a certain amount of time (~10s). I have tried putting the 'wait' function in but this doesn't seem to work. Does anyone know what would work best.
Thanks
0 Kudos
Message 1 of 9
(22,391 Views)
Hi Sam,

use the "ms timer". Place one outside the loop, generating the "start" time. On every iteration of the loop, compare this with another instance of the ms timer inside the loop. If the difference is more than your required period (10,000 ms) then a boolean can be set to stop the loop.

See the example below. Hope this helps.

Shane.
Using LV 6.1 and 8.2.1 on W2k (SP4) and WXP (SP2)
Message 2 of 9
(22,391 Views)
Tick Count (ms) is probably the best and easiest way to perform this task, however you do need to be aware of the wrap around, whereby the function starts at 0 after the maximum number of counts has elapsed.

You can get over this by simply checking for this event and handling it by adding a constant.

You do also need to be aware that this method does rely on giving back CPU time by using the Wait until next ms function. This will aid error in the measurement of the time function as it waits for x ms.

A more complex but better implmentation of this method especially if a smaller time in ms is required would be to implement this using an a design pattern like the Producer/Consumer with Events.

Hope this also helps.
0 Kudos
Message 3 of 9
(22,392 Views)
True, there are ways to improve the resolution of this timing, but given the original question (timing for ~10 sec) I think a resolution of 1/40 second should do.

Of course, the resolution (or error as you put it) can also be improved by lowering the "wait until ms multiple" input to 5 ms....

It all depends on the requirements.

Can you post an example of your approach, I think I know what you mean with the producer/consumer approach, but I'm not quite sure. It's actually not something I would have thought of immediately, but could be useful for demanding routines.

Regards

Shane.
Using LV 6.1 and 8.2.1 on W2k (SP4) and WXP (SP2)
0 Kudos
Message 4 of 9
(22,391 Views)
Can anyone post an example of "checking for this event and handling it by adding a constant" ?
 
Is this just a matter of checking to see if the value you are watching for is higher than the max count and then adding the max count to the count itself?
 
0 Kudos
Message 5 of 9
(22,157 Views)
Is there any way to create a time delay in a for loop? I'm making some measurements but I think labview is moving too fast for the instrument to get an accurate reading.
0 Kudos
Message 6 of 9
(21,859 Views)

A delay in a for loop is no different than a delay in a while loop. You have the Wait(ms), the Wait Until Next ms Multiple, and the Delay function.

 

You also should not hijack an old, unrelated thread.

0 Kudos
Message 7 of 9
(21,847 Views)

I didn't think it was hijacking because it dealt with loops and timing.

 

I'll just post a new thread next time.  

0 Kudos
Message 8 of 9
(21,842 Views)

Same way you'd create a time delay in a while loop.  Put in a Wait function from the timing palette.

 

Never mind.  I must not have refreshed my screen in a little while since starting this message.

Message Edited by Ravens Fan on 03-16-2009 05:21 PM
0 Kudos
Message 9 of 9
(21,841 Views)