LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Conditionally Repeat Code?

I guess I am just confused on how the While loop works.   It always seems to check the condition terminal, then regardless of what it is, it finishes running everything in the loop. 

I just do not see how I can wire a t/f out of this loop when the loop is the code the obtains the data that determines if it needs to be run a second time or to finish.  This is not in the example I posted, but in the real code I am using, the data is read and processed.  If it is above a certain threshold, then I want the data to be read again.  If it is not, then I do not want to read and process another data point.  I want to move on to the next frame in the sequence. 

 

Thanks for you help. 

0 Kudos
Message 11 of 15
(797 Views)
That's not what's happening. Your problem isn't with while loops but with understanding how dataflow works.

This is what's happening:

1. At the start of the second frame of the sequence the value of True gets written to the "RunDisp" indicator.
2. Outer while loop starts
3. The value of the "RunDisp" indicator gets read via the local variable
4. The "<=" comparison against the outer loop index and the constand of "1" gets evaluated
  NOTE: Because of dataflow (3) and (4) can happen in reverse.
5. The "AND" function at the bottom gets evaluated, and the result fed to the loop control.

At this point you think that the loop has been told whether or not to continue. That's true, but what it's really been told is whether or not to perform another iteration once it's done finishing its current iteration. That's because the rest of the code inside the outer loop is independent (there's no wire connection) of what just happened. LabVIEW performed the above because they were simple operations compared with the rest of the code, which requires entering a case structure. Because of dataflow the loop control condition gets evaluated before the contents of the loop actually get processed. This is not a bug. This is how dataflow works.

See attached VI for a simplified example of controlling the loop based on max runs as well as the value of some measurement.


0 Kudos
Message 12 of 15
(791 Views)
You just need to ensure that the outer local variable does not get read until the inner loop has finished. This can be patched (ugly!) by forcing dataflow with a simple sequence frame. (See attached image). Now reading of the local is delayed until the inner loop has a chance to write the local variable. Remember, the sequence frame cannot start until all inputs receive data (even if one of the inputs is not even used inside it). 🙂
 
 
Of course you could improve your overall code by doing a few changes:
  1. The inner loop runs a constant amount of iterations and cannot be iterrupted, thus it should be a FOR loop.
  2. You should replace all these local variable "read..increment...write" constructs with shift registers.
  3. I don't see a reason for the sequence structure.
  4. The "enable" button should be either inside the while loop or its case around the while loop. Right now it will never get read after the program starts.

Message Edited by altenbach on 04-04-2006 12:31 PM

0 Kudos
Message 13 of 15
(785 Views)

Thanks. 

The code I posted had lots of things removed from the original code I have.  I am not even incrementing data in the original code.  It was just trying to quickly get something that was similar (as far as loops and sequences) to what I had.  You are correct that the inner loop should be a For loop.  I inherited this code and I have never changed that. 

I have always had problems understanding dataflow in labview (as evidenced by this problem).  Thanks for the help.  I think I understand a little better now. 

0 Kudos
Message 14 of 15
(777 Views)
smercurio, adding the sequence like altenbach worked, but like he said...a little ugly.   I changed the inner While loop to a For and tried something like your example.  But the problem was that there were problems with tunnel missing assignments.  It needed something wired to that terminal for iteration 0, 1, 2 of the case statement.  Then I realized the For with a Case inside should just be replaced with a stacked sequence.  Now in the final frame of that stacked sequence, the T/F decision is made, and then wired out to the AND gate that is waiting for this value so the value can be sent to the loop condition terminal.  This works like I wanted and is much cleaner than what I had.   Thanks to both of you for your help. I not only fixed my problem, but I understand dataflow in LabView much more than I did. 
0 Kudos
Message 15 of 15
(771 Views)