Barbara Stefanovska
Barbara Stefanovska

Reputation: 21

How to stop a While loop in LabView using a timer

I am doing a project for Uni and I need the while loop to stop after 6s. So I did the usual set up the wait(ms) to wait 6000ms and then compared it to 6000const to get a bool value of TRUE which should have stopped my loop, but it didn't. So I was wondering if you had any idea what the problem could be?

Upvotes: 2

Views: 8477

Answers (3)

poompt
poompt

Reputation: 297

The actual reason your bool value is false is that the output of the Wait (ms) VI is not the number of milliseconds the VI waited but "milliseconds timer" which is the number of milliseconds since windows booted up or something to that effect. It's the same number you'd get if you ran Tick Count (ms) VI the instant the wait VI finished.

Others are correct to point out that a 6 second wait in the loop will prevent your loop from completing execution and repeating until the 6 seconds are over.

Upvotes: 0

D.J. Klomp
D.J. Klomp

Reputation: 2669

Another option to resolve you problem can be using a timed loop. These are specifically designed for timing tasks. The benefit of a timed loop is that you have more accurate control over when it stops. The proposed while loop (from srm) will stop after 6 seconds + the time it needs to finish the code. The "downside" of a timed loop is that it behaves more like a for loop where each loop takes a specific time interval, ussualy I find that more a benefit than a downside:

while loop and timed loop VI snippet

I also put in the previous suggested solution (from srm) for comparison. On my pc the timed loop stops with ms accuracy while the while loop has a delay of several tens of miliseconds.

Upvotes: 0

srm
srm

Reputation: 3166

You don't want a "Wait" function -- that function halts execution until that node finishes, which means your While Loop will only execute 1 time, and that 1 time will last 6000 ms.

What you want is the Tick Count function.

You can drag this image out of your browser (some browsers may require you to save the PNG to disk first) onto your block diagram to recreate the code (if you have LV 2016 or later, as noted by the version number in the top-right corner). This image was created using "Edit >> Create VI Snippet From Selection".

enter image description here

Upvotes: 3

Related Questions