LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Conditionally Repeat Code?

I have a block of code that is one frame in a sequence that I want to conditionally repeat if conditions are correct.  Inside this code data is taken, and if this data meets certain requirements, I want to repeat the entire block of code again, but the second time it executes, needs to be the final time. 
 
Originally I just put everything inside that frame of the sequence in a while loop.  The wired the loop condition to a signal that was the AND of a boolean variable and a comparison of the while loop counter with a constant.  The boolean variable was set inside the block of code.  If the data was read and tested, and the conditions were such that I needed to run the loop again, I would set the boolean variable so that it would run again.  the comparison with the loop counter was to keep the code from running more than twice.  However the problem I am having is with the nature of how the while loop works.  It will always run at least one time.  Then when the code finishes the first time, regardles of what is fed in the loop condition terminal (either True or False) it will execute at least one more time.  So instead of the code executing 1 or 2 times (based on the data), it runs 2 or 3 times.   I know I could always add another frame after this sequence and just copy the code into the new frame, and surround it with a True/False Case Structure, but I was hoping there would be another way. 
 
Thanks. 
0 Kudos
Message 1 of 15
(3,880 Views)
It's true that a while loop will always run at least one time. It's NOT true that a while loop will always run at least two times, as you seem to suggest.
After the first execution, if you feed the proper value to the loop condition terminal, the loop will exit. If this does not happen, it means that the input to the condition is wrong.
You should be able to check this easily using debugging or wiring the condition outside the loop, enabling indexing on the  output tunnel and creating an indicator.

Paolo
Paolo
-------------------
LV 7.1, 2011, 2017, 2019, 2021
0 Kudos
Message 2 of 15
(3,874 Views)
Yes, you are correct, but the problem I have is the while loop always runs one last time after the "end" condition is met.  It have program highlighting turned on and I see a "F" go to the "run if true" loop condition terminal, and the code runs one more time then exits.  So in my case it always runs at least twice.  The first time it will always have a "T" sent to the loop condition terminal, the code executes.  While this code executes, a variable is set so that a F will be sent to the loop condition terminal, and when that happens, the code executes one more time, then it is finishes. 
0 Kudos
Message 3 of 15
(3,860 Views)
This makes absolutely no sense, and I suspect a programming error. You said you're comparing against the loop counter. Are you using zero-based indexing or one-based indexing? The loop counter is zero-based indexing. Please post your code if possible.
0 Kudos
Message 4 of 15
(3,854 Views)


@lme999 wrote:
While this code executes, a variable is set so that a F will be sent to the loop condition terminal, and when that happens, the code executes one more time, then it is finishes.

Something is wrong here! (could it be there is a feedback node somewhere? Hidden wires?)

Please attach a simplified version of your VI that demonstrates this behavior. What is your LabVIEW version?

0 Kudos
Message 5 of 15
(3,851 Views)
Everything is 0 indexing.  I have attached a simplified version of my code.  Run it with "input" = 0, then again with "input" = 1 to see both situations
(i.e. when the end is caused by exceeding the loop count value, and when it is ended by the variable "RuinDisp" being set). 
 
 
0 Kudos
Message 6 of 15
(3,842 Views)

I would think that with the "Input" at 1 (or anything above 0.5), the RunDisp variable gets set to F on the first time the code inside the outer while loop is run.  Then this causes an F to get sent to the "Run While True" loop condition terminal.  I think the code should end there.  But it goes back and runs the inner code one more time (as can be seen by the "Loop Count" beint at 2 when the code is finished". 

When the "Input" is set at something below 0.5, then RunDisp is set to T, then the loop counter is 1 which is less than or equal to 1, so the value sent to the Run While True loop condition is T.  So the code executes again.  This time however, the loop counter is 2 which is not less than or equal to 1.  So the value sent to the Run While True loop condition terminal is F.  However the code executes one more time.  For a total of 3 times. 

If I change the constant that the loop counter is compared with to "0".  Then the code always executes twice, regardless of the value of "input". 

0 Kudos
Message 7 of 15
(3,837 Views)
The problem isn't with the loop, but with your logic. If you want the outer loop to run twice at most, replace the "<=" with "<". That fixes one problem. Your other problem is that you are not thinking dataflow. The RunDisp local variable that you're using to control the loop gets read *before* you do anything inside the inner loop to set it. If you want this second way of stopping the loop this variable has to be read *after* you've finished the inner loop. Easiest way is to wire a True/False out of this loop.
0 Kudos
Message 8 of 15
(3,835 Views)

I agree with smercurio_fc .

Your code is a prime example for a race condition introduced by local variables. The local variable is read in the outer loop way before you write a different value in the inner loop. There is absolutely no need for any local variables, just use wires.

0 Kudos
Message 9 of 15
(3,828 Views)
The part about running wires outside the loop makes sense. However, I do not see what you are talking about it being read before it is set.  If I turn on the highlighting, it plainly reads the variable for the outside the loop, then inside the loop the variable is set, then it reads the variable again, then goes back inside the loop.  It plainly sends the "F" to the loop condition terminal (which I would think it would mean execution would stop instantly), then after that it starts running the inner code again. 
0 Kudos
Message 10 of 15
(3,819 Views)