LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How can I count loops? (beginner)

Hi,
I am used to program in languages like C and Java, so I have a few problems in thinking in the LabView way.

My problem:

I have a case structure with three cases.
Now I want to place this structure inside a loop and want to make sure that in each loop the next case is used. (1->2->3->1->2->...)

In Java I would use something like

int i = 1;
while(true)
{
//do something before the switch
switch(i)
{
case 1: /*do x*/; i++; break;
case 2: /*do y*/; i++; break;
case 3: /*do z*/; i=1; break;
}
//do something after the switch
}

In my understanding I would need a variable which I can read and write from. How can this be done in LabView?
0 Kudos
Message 1 of 4
(2,925 Views)
If you wire the Quotient & Remainder function to the iteration terminal of the while loop (x input) and a 3 to the y input, the x-y*floor(x/y) output will cycle 0,1,2,0,1,2... This is the same as the modulo function in other languages. Wire that value to the case selector. See the attached picture.
0 Kudos
Message 2 of 4
(2,925 Views)
Actually, this one is really easy in LV. You can just wire the iteration counter terminal (the boxed "i" inside the while loop) to the case selector terminal (on the edge of the case structure). This will automatically change data types to I32 and act just like your switch statement with a break; (there is no way to do flow through) Just don't forget about case 0: and default:
0 Kudos
Message 3 of 4
(2,925 Views)
Create a case structure with cases 0, 1, and 2.

Calculate the reminder of "loop counter"/3 and wire it to the case selector. See attached image.

Cases will be executed as 0,1,2,0,1,2,0,1,2,0...
0 Kudos
Message 4 of 4
(2,925 Views)