LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I make a WhileLoop inside a case structure execute? CAn't get WHILE LOOP to execute.

Hi,

 

I am confusing myself badly with the VI I am trying to create.  If you look at the attachment, you see that I have a voltage measurement inside a WHILE loop.  Depending on the value of the voltage measurement, a case structure executes (or not.).

 

What I would like is for the ADDITION calculation (labelled in BLUE) inside the case structure to execute over and over again, increasing the value of the STRING COMMAND INPUT, labelled in RED, (prior to the STRING CONCATENATION function) until the measured voltage (VOLTAGE 3) is LESS THAN the threshold SETPOINT, labelled with ORANGE.  So, in order to get it to repeat, I placed another WHILE loop inside the CASE STRUCTURE, which means I have a WHILE LOOP that contains a CASE STRUCTURE that contains a WHILE LOOP.  Is it OK to put a WHILE LOOP inside a CASE STRUCTURE that is already inside a WHILE LOOP?

 

In the innermost WHILE LOOP, I want the voltage measurement (VOLTAGE 3, labelled in GREEN) and ADDITION FUNCTION and STRING COMMAND TO SERIAL PORT, to be executed repeatedly, as long as the measured voltage (VOLTAGE 3) is GREATER THAN the desired threshold voltage, called "SETPOINT" (ORANGE) which is in the outer WHILE LOOP.  When VOLTAGE 3 is LESS THAN the SETPOINT, I want the WHILE LOOP to stop, and to exit the CASE STRUCTURE and go back to the outer WHILE LOOP and measure VOLTAGE 2 and begin again.

 

So, basically, my question is how do I get that ADDITION function and STRING COMMAND to repeat itself so that the value inside the case structure continues to INCREASE until VOLTAGE 3 falls below the SETPOINT value? --> As the value of the string command input is INCREASED, the VOLTAGE 3 DECREASES.  I have wired a GREATER THAN to the conditional terminal of the innermost WHILE LOOP, but, I am not sure if I did that correctly.

 

For clarification, outside the first WHILE LOOP, VOLTAGE 1  initializes the shift register. Then, some time later, once inside the first WHILE LOOP, VOLTAGE 2 is taken, and there are some calculations based on the difference between these two voltage measurements, which result in the CASE STRUCTURE INPUT value being fed into the CASE STRUCTURE for conversion to a string command.  Before it is converted to a string command, it is added to the previous value from a shift register so that subsequent STRING COMMAND INPUT values are higher and higher.  I'd like to repeat the ADDITION function/CONCATENATE STRING functions and send a new string command to the serial port, with each iteration of the addition step, using this single CASE STRUCTURE INPUT value over and over.  I want the addition function to keep iterating and adding until the voltage measured in the CASE STRUCTURE falls below the SETPOINT value found in the outer WHILE LOOP.

 

Is it possible to configure the innermost WHILE LOOP to increase the STRING COMMAND INPUT value until the measured voltage falls below the SETPOINT value as I have structured it?  I cannot get the WHILE LOOP to execute.

 

Thanks,

Dave

0 Kudos
Message 1 of 5
(2,763 Views)

I think you will have a much easier time coding this if you forget about LabVIEW for a minute and "write the documentation first", that is, write down a clear description of what you want to happen.  I'm guessing that you want to periodically measure a voltage -- if it means one set of conditions, you want to do one thing, and if it meets another set of conditions, you want to do something else.  Sounds to me like a likely candidate for a State Machine (see LabVIEW examples).  You should be able to come up with a very simple VI structure to accomplish your task.

 

Incidently, it is perfectly OK to put a While loop inside a Case statement that is inside a While loop.  However, this is probably not what you really want to do ...

 

Bob Schor

0 Kudos
Message 2 of 5
(2,743 Views)

Hi Bob,

Thanks for the suggestion.  Is the description of my desired functionality not clear in my posting?  I am very clear about what I want to do.  I will look into the state machine architecture and see if I can create something simple with that?  If you look at my vi, can you tell me if I have wired the innermost WhileLoop properly?  It does not execute at all, and that confuses me.

Thanks,

Dave

0 Kudos
Message 3 of 5
(2,735 Views)

Errors I see.

 

1.  Don't continually open and set the timeout of your VISA resource inside the inner while loop.  The VISA port should only be opened once at the beginning of the program before the loop starts.

2.  Your case structure is set to use default if unwired for some of the terminals.  This is going to be a possible problem for your VISA resource wire and the DAQmx wire, if the  false case runs before the outer loop ends, you are going to get errors when the VISA close and DAQmx close run because the references will be invalid.  Uncheck  Use Default if unwired on those terminals.  Wire the purple wires through the false case of the case structure.

 

I think the main problem you have is that you say you want to continually increase a value in that inner while loop.  You should probably have a shift register on that wire of the inner while loop.  Right now, you are just adding the value of the control to the value that appears at the input tunnel of the while loop.

0 Kudos
Message 4 of 5
(2,726 Views)

@dav2010 wrote:

Hi Bob,

Thanks for the suggestion.  Is the description of my desired functionality not clear in my posting?  I am very clear about what I want to do.  I will look into the state machine architecture and see if I can create something simple with that?  If you look at my vi, can you tell me if I have wired the innermost WhileLoop properly?  It does not execute at all, and that confuses me.

Thanks,

Dave


W


@dav2010 wrote:

Hi Bob,

Thanks for the suggestion.  Is the description of my desired functionality not clear in my posting?  I am very clear about what I want to do.  I will look into the state machine architecture and see if I can create something simple with that?  If you look at my vi, can you tell me if I have wired the innermost WhileLoop properly?  It does not execute at all, and that confuses me.

Thanks,

Dave


I regret to say that from neither the description nor the code can I figure out what you are trying to do.  However, I do think I can tell you why the innermost While loop does not execute.  It is "guarded" by a Case statement, and if the input to the Case is False, the loop isn't run.  The Case, in turn, is driven by the And of three booleans.  The first one asks if SetPoint < 0, which (based on the default value of 80) is false, hence the And is false, hence the While loop does not execute.

 

I'm confused also by the multiple clock timers, including multiple "floating" Wait (ms) and Tick Count (ms).  There are also three Analog sample reads.  My guess (and it is only a guess) is that you want to sample a signal at some rate, say 50 Hz, and based on the value of this sample, do something (including possibly changing some criterion for further actions), at some point outputting values through a VISA channel.  A State Machine is a very good paradigm for this.  The outer While would run at 50 Hz (using a Wait until next N ms multiple VI for timing), would take a single analog sample, and then would dispatch through a Case statement guarded by the current State variable to do whatever is appropriate with the value.

 

Bob Schor

0 Kudos
Message 5 of 5
(2,708 Views)