LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

aborting for loop without stopping VI

hello

I have a for-loop inside of a case strukture. Normally the for-loop could finish its "job" but if the condition of the case strukture changes I'd like to switch immediately to the corresponding case.

Now I'm wondering if there is a way to switch
to another case without waiting for the loop to complete?

I know the stop-function but I think it won't help me because it aborts the whole VI.
What I'm searching for is an equivalent to the break statement in C.


thanks in advance
Simon
0 Kudos
Message 1 of 10
(3,670 Views)
No. The FOR loop commits to doing N loops when it starts ( N = smaller of the N input(if wired) and all the auto-indexing inputs). You can't stop it short of aborting the VI that contains it.

But your logic is wrong if you need to do that. It sounds to me like you need something like


i := 0;
repeat
case Selector of
1: begin
DoSomething (i);
inc(i);
Stopped := i >= N;
end;

2: DoSomethingElse();
end;
until Stopped;

Which is trivial in LabVIEW.
The counter will increase, just like a FOR loop.
It will stop after N iterations like a FOR loop.
But you can do other things if needed.
Steve Bird
Culverson Software - Elegant software that is a pleasure to use.
Culverson.com


LinkedIn

Blog for (mostly LabVIEW) programmers: Tips And Tricks

0 Kudos
Message 2 of 10
(3,670 Views)
hello

many thanks for your answer CoastalMaineBird but it seems that I have to go a little more in detail.
Attached is a little piece of code showing my problem.

I use a SubVI named "serial 8out" which controls the state of 8 external LEDs via the
RS232 port.
The user should be able to choose between 3 main cases by selecting the corresponding control
on the frontpanel:

1. All LEDs off (just sends array 00000000 to the subvi)
2. All LEDs on (11111111 -> subvi)
3. manual - Define state of each LED.
(the 2D array allows to select the states user-defined and additonaly allows
to define several states for each LED witch are set consecutively (blinking))

This normally works pretty well except the case when you change the c
ontrol from
"manual" to one of the other cases. It dosn't react imediately couse the programm is completing
the for-loop before.

Now I'm wondering about a way of aborting the loop in order to change cases quickly when the
condition changes.
Or is there a better way of doing this. Maybe completely different.


Thank you for your suggestions
Simon
0 Kudos
Message 3 of 10
(3,670 Views)
You should replace the for loop with a while loop with the conditions for loop termination being that the loop has iterated n times (i = n - 1) or the value of the condition of the case structure has changed. This makes it so that the loop will only execute one time after the value of condition has changed.

Hope this helps.

John
Message 4 of 10
(3,670 Views)
I think the easiest way, looking at your jpeg, is by changing your for loop to a while loop. A while loop can also index your array, and you can use a VI to get the length of the array, feed that into the while loop, and put in some logic to check the iteration value against the array length, as well as if the button changed. Either case could stop the while loop.

Mark
0 Kudos
Message 5 of 10
(3,670 Views)
As I said, you need to change your logic.

It looks like your code is doing this:

repeat
Case CONTROL of
All OFF: SubVI(0,0,0,0,0,0,0,0);
All On: SubVI(1,1,1,1,1,1,1,1);
Manual:
"For i = 0 to NRows in Array-1 do
SubVI(Row[i])"
until Stopped.

What you need is something like:

repeat
if Control <> Previous Control
then ManualIndex = 0
Case Control of
AllOff: Pattern = (0,0,0,0,0,0,0,0);
AllOn: Pattern = (1,1,1,1,1,1,1,1);
Manual:
Pattern = ManualArray[ManualIndex];
ManualIndex = (ManualIndex + 1) mod ManualArray.NRows;

if Pattern <> PreviousPattern
then
SubVI(Pattern);
PreviousPattern = Pattern;

PreviousControl = Control;
until Stopped;

Use
shift registers for the "Previous" variables.

You're hogging the CPU with the 2nd WHILE loop in your picture - put a WAIT function in there to wait 100 mSec or so.

Better yet, why have a separate loop at all? Just stop the first one if there's an error OR the stop button is pressed.
Steve Bird
Culverson Software - Elegant software that is a pleasure to use.
Culverson.com


LinkedIn

Blog for (mostly LabVIEW) programmers: Tips And Tricks

0 Kudos
Message 6 of 10
(3,670 Views)
hello

many thanks to all of you who answered to my question.

special thanks to CoastalMaineBird - you wrote: "what you need is something like..."
- you could have left out the "something like" - your suggestion was close to the exact programm I needed.

I think you've recognized: I'm still a labview newbie (however this senseless extra-while-loop
even made me feel ashamed) - hoping to be able to provide this forum with answers like yours too one day.

Thanks
Simon
0 Kudos
Message 7 of 10
(3,670 Views)
please - take a look at last posting
Simon
0 Kudos
Message 8 of 10
(3,670 Views)
please - take a look at last posting
Simon
0 Kudos
Message 9 of 10
(3,670 Views)
Please take a look at the last posting? I did! I don't understand why you typed this.

I have read through our answers, and I thought the other people were right on. If we haven't helped you, you have to give more detail than this.

Mark
0 Kudos
Message 10 of 10
(3,670 Views)