LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Using the for loop iteration number as variable outside loop

Hello! I've tried this suggestion but I can't get acces to each number for every iteration, The loop is just sending out the final one. I need the numbers as they increase inside another loop. Any help would be of help!

0 Kudos
Message 11 of 20
(2,270 Views)

To (badly) paraphrase a well-known saying, "What Changes Inside a For (or While) Loop, Stays Inside the For (or While) Loop".  [As with every rule, there are exceptions ...]

 

The Principle of Data Flow (absolutely central to LabVIEW) says that functions (such as graphs, charts, and indicators) placed outside the Loop cannot show you changes that take place inside the loop (but see [statement] above) -- values only are exported from the loop when it exits.  If you want to use/watch values that vary inside the loop, you need to put your indicator ... inside the loop!

 

Look at these three examples, all of which end with Rand being the final Random number and Count being the final Count (99 -- do you know why?).

Loops.png

The first example will display only one value, the final one.  The second example will, in fact, display all 100 values, but they'll zip by so fast you'll only notice the last one.  The final loop adds a 50 millisecond wait to let you see the numbers count up, and after 5 seconds, the loop will exit leaving the final values in the indicators.

 

[If you try this yourself, you'll notice that when you code the first example, the wire coming out of the For loop is not a "Last Value" tunnel, but an "Indexing" tunnel, looking like a square-within-a-square, building an array instead of the last value.  You can right-click the tunnel and change it to a Last Value tunnel, which is what I did.]

 

Bob Schor

0 Kudos
Message 12 of 20
(2,262 Views)

Many thanks for your fast reply.

 

The reason for needing access to the loop, was that we are doing some frequency analysis on an hydraulic actuator, and therefor we nedded to change the frequency at given time intervalls. The code attached seems to respond like we want. If you have suggestion how to improve the code, pleas feel free to comment, sample rate at 0,5ms is required.

 

Regard Jan

0 Kudos
Message 13 of 20
(2,234 Views)

10 second analysis:

 

  • way too many local variables and structure stacks (e.g. eliminate the inner loop in the lower part and place the shift regsiter on the outer loop).
  • constantly converting between "scalars" and "arrays with one element" is just silly.
  • use consistent representations.
  • There is a "+1" perimitive
  • ...
0 Kudos
Message 14 of 20
(2,222 Views)

Many thanks! It seems like my Labview code has room for improvements.

 

Regards Jan

0 Kudos
Message 15 of 20
(2,210 Views)

Also note that your lower loop run at a near infinite rate if the selector boolean is false, spinning the loop as fast as the computer allows, trying consume all available CPU and starving all other processes (e.g. the upper loop!).

 

Here's how the code of the lower loop could look like instead. (Personally, I would probably implement that logic into the upper loop and eliminate all locals.)

 

0 Kudos
Message 16 of 20
(2,198 Views)

I don't usually recommend this (it is far better to learn to write "neat" code from the beginning), but something you might try (do it with a copy of your VI, just in case) is to use the "Clean up diagram" tool (on the Block Diagram toolbar, it's off to the right and looks like a broom).  This tries to straighten your wires, make sure they don't "run backwards" (as some of yours do), and tries to get rid of unnecessary blank space (because it is easier to see/understand code if you don't have to "search" for where wires go).

 

 

Bob Schor

0 Kudos
Message 17 of 20
(2,196 Views)

Many thanks for your feedback. I did try to manage to increase the frequency inside the main loop but I cound not figure out how. The lower loop seems to be a quick fix, but obviously not that clever. Why is local variable not a such a good idea?

 

Regards Jan

0 Kudos
Message 18 of 20
(2,184 Views)

If you use local variables without thinking, you create situatios where you can't control when they're being read or written from/to.  Your logic breaks down if the two operations happen in reverse or arent' consistent.  It's better to pass a wire, when possible, or use a queue to block computation until a new values appears.

0 Kudos
Message 19 of 20
(2,167 Views)

Thanks!

0 Kudos
Message 20 of 20
(2,153 Views)