LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Using global variables for loop control

I have a for loop (loop 1) and a while loop (loop 2).
I want the two loops to start their run together and
once loop 1 terminates , I want loop 2 to terminate also.

The procedure I have attempted is the following:
    Inside Loop1 the value of the loop iterator is compared to the (max number of iterations - 1)
    If they are equal a local variable is set to true.

    Loop2 should terminate once this local variable changes to true as
    the loop termination condition is stop if true.

But the value of the local variable never leaves loop1 so loop2 never starts.

Can anyone suggest what is wrong or if there is an alternative approach

0 Kudos
Message 1 of 5
(1,963 Views)
You don't need to compare anything inside the FOR loop. It will run a set number of iterations by its very nature. 😉
 
Just start both loops at once, then stop the while loop once the FOR loop finishes by setting the stop condition with a local variable..
 
If there is other code that needs to run first, place a single flat sequence around the two loops so they start together, then create a data dependency so the sequence waits..
 
Attached is a simple example (LabVIEW 7.0).
 

Message Edited by altenbach on 07-21-2006 09:38 AM

Download All
0 Kudos
Message 2 of 5
(1,953 Views)
You do not need to do the comparison inside the For loop. The loop structure does that automatically. When the loop completes, any data line coming out of the loop will indicate that the loop has completed by having valid data. This is the basic LabVIEW dataflow paradigm. When this occurs send the signal to the second loop to stop. Notifiers or queues are good for this. Local or global varibles can also be used, but they often lead to race conditions if not used carefully.

"But the value of the local variable never leaves loop1 so loop2 never starts." This makes me think you have the loops wired sequentially (or nested) rather than in parallel. For both to run in parallel, they must not have any data dependency upon each other and each should have a wait function (even set to 0 ms).

If this does not help you, post a simplified example of a VI with the problem so someone can see what you are doing.

Lynn
0 Kudos
Message 3 of 5
(1,948 Views)

This should work just fine I implemented it easily. Here is a vi in 7.1 that shows what you explaned. Post your code so that i can have a look and see why yours does not work.

 

 

Got beat to the punch

Message Edited by Jhoskins on 07-21-2006 12:45 PM




Joe.
"NOTHING IS EVER EASY"
0 Kudos
Message 4 of 5
(1,947 Views)


@Damsel wrote:
    If they are equal a local variable is set to true.

    Loop2 should terminate once this local variable changes to true as

Just a side comment to this approach:

You also have to be very careful with race conditions here. Instead of "equal", set the local variable TRUE if the iteration is "greater or equal" the set value. Otherwise, depending on the loop rate of the two loops, it is possible that the local variable is set to true only for a very short period and thus can be missed by the While loop entirely, which read the boolean only a certain intervals.

No matter what, my apprach above is preferred because the value is set once the loop has finished.

0 Kudos
Message 5 of 5
(1,945 Views)