I have a for loop and some other loops running in the same time. The for loop is used to read values from a PMT connected to a DAQ. I need to export the value of the loop's counter in order to use it in a case statement which runs outside of the loop. I tried tunelling it out of the loop, either by using automatic indexing (which exports an array with all the values of the counter stacked from what I understand) or without automatic indexing, which from what I understand only exports the current value of the loop. But both of these did not work, as I tried for example to print the value of the counter on an indication on the board and it was not printing anything. On the contrary, when I put an indicator inside the loop it was printing the value of the counter normally. Would you have any advice on how I could do that or why is it not working? I also checked shift registers but it doesn't look like their use is appropriate for my problem.
Solved! Go to Solution.
If you want to use the internal value of a loop at another place you have a few options:
Pick one. 🙂
You cant 'export' the variable. If you use Autoindex you'll get an array as you mention (that's the idea), and if you dont use autoindex you'll get the last value update when the loop finish (i.e. the number of loops performed). Usually if you want to use the loop counter you'll perform the action/operation inside the loop itself. If it's a schematic thing ("it's getting too big to perform inside the loop") make a sub-vi.
With that said, are you running two loops in parallell? Dont you want to perform this case inside the loop so it checks each iteration?
Thanks! I will try to use a global variable. Yes, in this vi additionally to reading from the DAQ I'm also controlling a couple of motors simultaneously. The general problem is that the PMTs take some time to heat up and I want to start writing their output voltages to a file after they have heated up, so I am using the value of the counter in a case statement inside the for loop to do that. But then I also need to command the motors to start moving at the same time when I start writing to the file, so I also need to be checking the loop's counter outside of its loop.
@George M wrote:
Thanks! I will try to use a global variable.
Be VERY careful about how you do this. There is nothing evil about global variables, but newbies to programming, especially LabVIEW fail to understand the dataflow concept and assume LabVIEW programs like a text-based language, somehow expecting the code at the top of the page to execute before the code at the bottom of the page, or the code on the left to execute before the code on the right. Dataflow doesn't work this way, and consequently it is VERY easy to create race conditions, and local variables and global variables are by far the #1 cause of this "malady".
If you do a search (at least in LabVIEW 8.6.1) under "Help", "Search the LabVIEW Help" for "producer" you will find discussions of what you are trying to do. Under the "File","New" pulldowns there should also be, under "Frameworks" "Design Patterns" there are examples called "Producer/Consumer" will open a template of different ways to do what you are attempting. As said be the previous poster, while globals aren't inherently bad, their use comes with some risk. There have been a number of threads devoted to that (and locals which can provide a similar if, "local" risk). Without going into it too deeply, the primary concern is knowing when the data read from them is valid/updated vs. when it is written.
Thank you all.
After all I solved my problem using queues (something like the producer/consumer that was suggested). Indeed initially I tried using global and then local variables, but soon I realized that they were not updated simultaneously, although in my mind I had that they would be updated automatically whenever their value changed by some sort of polling (although I'm not a noob in programming, I am in labview). The same was true for the queue until I put both the enqueue and dequeue VIs in loops. Thanks again!