From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

For loop inside a while loop

Solved!
Go to solution

I have for loop inside a while loop...the input to the for loop N is comming from the select VI.....the while loop I have has condition statement it basically stops right after the for loop finishes all the iteration...

 

The input to the for loop N is bascially driven by a local variable...now here is the problem Iam having:

 

When I press the start button to run the program...what ever the output of the select VI is gives the N to the for loop and then the loop starts running and then finishes up....and when the output of the select statement changes to different value( the N of the for loop)  the for loop does not run until I restart the program again...what can I do so that the for loop runs again for a different value to N while the program's RUN button is ON. 

 

0 Kudos
Message 1 of 28
(12,785 Views)

Hi there

 

Any chance that you could post your code, makes it so much easier to determine what is going on 🙂

 

/Sletten

0 Kudos
Message 2 of 28
(12,776 Views)

What is the stop condition of the while loop? If you want the FOR loop to repeat, the while loop should maybe not stop.

 

Please attach your code. Your words are too ambiguous to really tell what's is going on. Why is there a local variable? Is the actual terminal elsewhere?

0 Kudos
Message 3 of 28
(12,774 Views)

Hello SK0480,

 

Its not very clear to me, what exactly are you trying to do. You said that you have a condition which stops the main while loop when all the itrations of the for loop gets over. So your program will stop after your while loop completes its first itration. I think your concept about the for loop inside the while loop is not very correct.

 

Let me explain this according to my understanding. If you place a for loop with 5 wired to N terminal of it inside a while loop, for each itration of the while loop, the for loop will run 5 times. During each itration, the while loop will wait for the for loops 5 itrations to get completed to proceed to its next itration. So in your case, even if the output of the Select VI is supposed to change the value of N of the for loop, that will wait for the next itration of the while loop, but it doesn't happen because your while loop stops with its first itration itself due to the condition in it (stop when all the itration of for loop completes).

 

It will be better if you could post your code so that your problem can be solved in a better way.

Warm Regards,

NitzZ

(Give kudos to good Answers, Mark it as a solution if your problem is Solved;)) 

0 Kudos
Message 4 of 28
(12,772 Views)
Ok well here is the thing iam basically running a stepper motor. So the N of the For loop is basically the number of the steps right after the motor finishes up steps it has to stop. If i dont put a condition on a while loop the motor will keep running forever. So what iam trying to do is rerrun the motor if the output of the select changes after the motor finishes all the steps without having to restart the program and it was doing the same thing when the for loop was not inside a while loop.
0 Kudos
Message 5 of 28
(12,762 Views)

Hi SK0480,

 

Through the use of a State Machine architecture, you can execute a high level of control over the sequentiality of your software.

 

Idle.png

 

Through the use of a Shift Registers to pass around an Enumerated Control, you can control the execution through the use of a  Case Structure.  

 

Move Motor.png

 

This design will allow the For Loop to operate only when the 'Move Motor' state is entered, otherwise the program will be idle.

 

Is this what you had in mind?


Alex Thomas, University of Manchester School of EEE LabVIEW Ambassador (CLAD)

Message 6 of 28
(12,744 Views)

ok so as far as cases goes inside it...how can choose it externally instead of having the user press button...lets say through a local variabe

 

lets say If I add one more state then how can I choose more than two states because in the Vi you gave me it has a select Vi with two inputs only...IDLE or MOVE...but lets say If I want to have more than one variable what would be the procedure for that.

 

Thanks

0 Kudos
Message 7 of 28
(12,697 Views)

Let's NOT say a local variable. I would recommend you use a queue to feed actions into your state machine. (Technically once you have outside control of your state machine it is no longer a pure state machine but that is a whole other topic.) Use the queue within the While loop to determine the next action to take. You can post actions to the queue either within the loop or from outside in another task. Take a look at the producer/consumer examples to see how this is accomplished.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
Message 8 of 28
(12,696 Views)

Ok Attached is the program I created to control stepper, now I already have a producer consumer loop but do not know how to include a state machines..

 

I tried using Case statement and shift register with the while loop, but the motor runs only once and then to run for another case I have restart the program... it would not run while during the program.

0 Kudos
Message 9 of 28
(12,656 Views)
Solution
Accepted by topic author SK0480

1. Clean up your diagram.  The style guides suggest keeping the diagram to one screen.  With a little effort I was able to get your BD to less than 1600 x 1000 pixels.

2. Then I can see (some of) what is going on, all at once. Does this thing actually do anything?

3. Local variables can lead to race conditions.  Output Position in Angle may have a race condition, although it probably is not doing what you intended.  What does the Time local wired to the Time indicator terminal do? (Ooops! Two controls with the same name - that can be quite confusing as well!)  If you need or want two front panel components to have the same text, use captions.  Make the labels different so the BD is more readable.

4. Having more than one Dequeue function on the same queue will lead to unpredictable results.  When an element is dequeued, it is removed from the queue and is not accessible to any other Dequeue function.  In parallel loops you have no way of guessing which Dequeue will grab any particular element.

5. Use Boolean Invert rather than Select with False wired to the True input and True wired to the False input. Better, just make the false case in the case structure inside the for loop the true case. No inversion needed.

6. I did not try to determine the logic of the code inside that case structure. It seems I may have posted a much simpler way of doing that several weeks ago.

7. Use Multiply from the Numeric Palette rather than a formula node to multiply by 4 or 1.8. Uses less BD space and is much easier to read.

8. As has been suggested, learn how the Producer/Consumer architecture and state machines work.  They can make your life much easier.  Do not try to immediately convert this to those patterns.   That is too much to bite of at one time.  Learn how they work first.  Do a couple of simple examples.  Then rewrite this program in that format.  Probably faster than fixing what you have now.

 

Lynn

Message 10 of 28
(12,650 Views)