LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

force while loop to execute every 1 sec

Solved!
Go to solution

Is there anyway I could force the while loop to execute every 1 second.

Let us say I have a while loop with a couple of case structures. I want to

force execution every 1 or 2 seconds. How could it be done in labview

 

0 Kudos
Message 1 of 11
(17,047 Views)

You can make the while loop exicute every 1-2 seconds by placing a wait into the structure. See below

 

Example.png

Tim
GHSP
0 Kudos
Message 2 of 11
(17,044 Views)

Do you have code that we can look at to help you. It would be much easier.

 

Tim
GHSP
0 Kudos
Message 3 of 11
(17,043 Views)

If the code withing the case structure doesn't take up too much time (more than your desired loop rate) then there are a few ways that you could do this. There is a "timed loop" structure under the "Timed Structures and VIs", where you can define the time between executions, and it has "internal" nodes that will tell you how long it has been since the last iteration and adjust the loop time to compensate for delays with the loop.

 

A cruder way would be to keep track of how long the case conditions took, and add or subtract the appropriate time to a timer function within the loop.

Putnam
Certified LabVIEW Developer

Senior Test Engineer North Shore Technology, Inc.
Currently using LV 2012-LabVIEW 2018, RT8.5


LabVIEW Champion



Message 4 of 11
(17,034 Views)

aeastet wrote:

You can make the while loop exicute every 1-2 seconds by placing a wait into the structure. See below

 

Example.png


 

I would actually use a metronome (Wait Until Next ms Multiple) here.

 

As LV_Pro suggested, you can use a Timed Loop. Whichever way you do it, I'd set the time interval to be small and use a time check to see if the required time has elapsed. This will allow the loop to respond quickly if being asked to abort instead of waiting a full 2 seconds before actually stopping because it's busy waiting for the time interval to end.

0 Kudos
Message 5 of 11
(17,019 Views)
I was only suggesting something until I better understand what is need for this application. As I stated it would be better to have the code and some explination of the end result to give the best possible answer.
Tim
GHSP
0 Kudos
Message 6 of 11
(17,004 Views)

aeastet, I was posting my comments at the same time you were, and neither it nor the following ones were particularly a critique of your suggestion, just alternatives.  Keep helping others, it helps them and you (gets us thinking about LabVIEW problems other than the ones we might be running into in our own work).

 

Putnam
Certified LabVIEW Developer

Senior Test Engineer North Shore Technology, Inc.
Currently using LV 2012-LabVIEW 2018, RT8.5


LabVIEW Champion



0 Kudos
Message 7 of 11
(16,998 Views)

aeastet wrote:
I was only suggesting something until I better understand what is need for this application.

As LV_Pro indicated, I wasn't criticizing your suggestion - I was suggesting that perhaps the metronome may be a better fit to make sure something happens at a specific multiple, although I think using a small wait in the loop and checking the elapsed time is a better solution as it doesn't make it seem like something is "stuck" after you press the Stop button.

 


As I stated it would be better to have the code and some explination of the end result to give the best possible answer.

100% agree. Smiley Wink

0 Kudos
Message 8 of 11
(16,994 Views)

I am attaching a snapshot of my code. I designed it like

a state machine. if you look at the code you can see that

I have used a elapsed time vi to stop the loop as soon as

given time elapses. My loop without any elapsed time usually

acquires data from a hardware every 4 -5 sec. I am able to

extract data now for every 1 second after I use elapsed time.

there are a couple problems for this

 

--> I cannot stop the while loop in this if I run it continuously. I have

to use abort button.

 

--> I want to make it iterate continuously until I press stop, which does

not happen when I just run and not run continuously.

 

All I want is to execute the loop continuously when I press Run and

execute every 1 second.

0 Kudos
Message 9 of 11
(16,987 Views)
Solution
Accepted by topic author Yesh

The loop will stop everytime it has run the timed loop's expected time. When you run it the "run continuous" mode what you are really doing is causing LabVIEW to retart the vi over and over again, which is why you can't stop it with your stop boolean. Why have you connected the output of the timed loops boolean to the or?   Oops, sorry, not a timed loop, didn't really look at the image long enough. What are you trying to do with the elapsed time vi? Remember, LabVIEW is a data flow language. What this means is firstly, you don't know, unless it is forced by data flow, what will execute first in your loop. So it is possible that the elapse time vi may execute first, causing uncertainty in your loop times. With it connected to the stop it will always stop your loop after the time has elapsed (except when "run continuously" which is more of a debugging tool, not the way vi's are normally run). You could put the elapsed time into one or the other of your state machine cases so that it goes through, acquires data, then sits in the other case (with a "select" from the comparision pallette with the don't acquire wired to one input, the acquire to the other) until the time has elapsed, then acquires data again, repeat...  I don't know what is in your "stop" case of the state machine, do you stop the loop in that case or just not acquire data?

 

 

Message Edited by LV_Pro on 01-11-2010 03:39 PM
Message Edited by LV_Pro on 01-11-2010 03:41 PM
Putnam
Certified LabVIEW Developer

Senior Test Engineer North Shore Technology, Inc.
Currently using LV 2012-LabVIEW 2018, RT8.5


LabVIEW Champion



Message 10 of 11
(16,981 Views)