From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, 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: 

How to use counter inside a for loop as a variable outside the loop?

I am trying to write a program consisting of a for loop, with code also outside the for loop. As the for loop iterates, I need the counter to be used outside the loop to increase the output frequency of an external frequency generator. This needs to occur simultaneously with the main for loop, but I can only seem to pass the final counter value outside the loop; ie: the value is only passed after the for loop has completed all iterations. 

 

Is there a way to continually pass the counter value outside of the loop in 'real time'?

 

Thanks in advance. 

0 Kudos
Message 1 of 6
(2,809 Views)

Hi niamhw,

 

yes!

Spoiler
local variables, global variables, notifier, queues, channels, …
Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 2 of 6
(2,807 Views)

Hi GerdW,

 

Could you elaborate a little more on how I would use the functions you mentioned? I am pretty new to LabVIEW and have no experience with local/global variables etc.

 

Thanks

0 Kudos
Message 3 of 6
(2,767 Views)

Hi niamhw,

 

when you are so new to LabVIEW you even don't know about local/global variables then I STRONGLY advise to go to that Training section in the header of this LabVIEW board!

Learn LabVIEW first, before starting to code…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 4 of 6
(2,748 Views)

When you start learning LabVIEW, be sure that you learn the Principle of Data Flow (PDF), the key organizing principle that gives LabVIEW a lot of its power and makes it ideal for being an Engineering Workbench (look at the last two letters of "LabVIEW").

 

One corollary of PDF is that variables inside Loops (such as the Loop Index) "stay" inside the Loop until it exits, unless you export it using some mechanism.  This "breaks" Data Flow, and can often cause difficult-to-find problems, so is not recommended for beginners.

 

A "safe" thing to do is to use the variable inside the loop.  Of course, we don't really know what you want to do, but there may be a "better" (i.e. "safer") way to do it.

 

I agree with GerdW (of course!) that Learning LabVIEW and Learning How It Works is a good idea.  Here's a Pop Quiz -- can you imagine doing whatever it is you want to do in C++ (or VBA, or MatLab) when you just started learning the language?  It's challenging enough to write "Hello, World!" ...

 

Bob Schor

0 Kudos
Message 5 of 6
(2,741 Views)

@niamhw wrote:

As the for loop iterates, I need the counter to be used outside the loop to increase the output frequency of an external frequency generator. This needs to occur simultaneously with the main for loop...


Then put the updating of the frequency inside of the FOR loop.  It will make things A LOT easier on you.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 6 of 6
(2,722 Views)