01-11-2010 01:49 PM
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
Solved! Go to Solution.
01-11-2010 01:53 PM
You can make the while loop exicute every 1-2 seconds by placing a wait into the structure. See below
01-11-2010 01:54 PM
Do you have code that we can look at to help you. It would be much easier.
01-11-2010 01:57 PM
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.
01-11-2010 02:06 PM
aeastet wrote:You can make the while loop exicute every 1-2 seconds by placing a wait into the structure. See below
![]()
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.
01-11-2010 02:15 PM
01-11-2010 02:19 PM
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).
01-11-2010 02:23 PM
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.
01-11-2010 02:26 PM
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.
01-11-2010 02:32 PM - edited 01-11-2010 02:41 PM
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?