From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

How to control a While Loop Inside a case Structure ??

Hello,

I have a very simple problem but I am struggling a little bit and have already spent some time. I have a big while loop and a case structure with two cases inside that while loop. Case A and Case B. and My VI is currently working great except this extra feature I need to add. (Extra feature - generate a 5V DC in my line0.p0 when case B is running if not OV DC)

I realized that, in order to do that I have to use a Daqmx DO in my Case B inside a while loop. Now I have a two while loop and two STOP button.

The main Problem - How can I control the entire VI with one STOP button? For example when I stop my Main VI, how can I make 5V drop to 0V in my case B.?

Right now even if I stop my main VI I still see 5V coming out of my DO line in my Daq ( USB 6353) if I am running Case B, and if I press Stop button of my 5V vi nothing happens.

I need to write a code in such a way that

While VI is running

If Case A is selected – line0.p0 = OV DC

If Case B is selected - line0.p0 = 5V DC

and if I press stop button ( Main VI stop button) or Stop tab in front panel, line0.p0 = 0VDC and stop entire program. (Right now even if I stop main VI, the Digital output while loop is running and line0.p0 = 5VDC)

I hope I am clear with my problem here.

Thank you for help in advance.

Best,

0 Kudos
Message 1 of 4
(4,853 Views)

Nested while loops are usually a sign that you should consider a different architecture for the program.  Your original program can probably be converted to a state machine fairly easily. 

 

Rather than embedding a difficult-to-stop loop inside Case B, Case B will be split into two (or more) states. The state machine only stays in any state for a few tens of milliseconds before checking for the stop command or other data which will cause it to change states. If not change is required, it simply repeats the same state. When the stop button is pressed, it will go to a Shutdown state where a write 0 V to DO occurs before stopping the loop.

 

Most DAQ devices support static DO. You can write the value one time and the output remains at that value until a different value is written. So both Case A and Case B only need to write to the DO one time - when the case changes. With the state machine you could have states like Initialize, Idle, Write, and Shutdown.  Initialize sets up the DAQ task. Idle waits for a command from the user, probably with an event structure. The possible commands appear to be Set DO to 0, Set DO to 5, and Stop. The Write state is called with data (on a shift register) for any change of command. The Stop command first performs a Set DO to 0 command and then goes to Shutdown where the DAQ task is stoppped and cleared and the loop is stopped.

 

Lynn 

 

0 Kudos
Message 2 of 4
(4,852 Views)

Lynn,

 

Thank you for your reply. I gave it a try to your recommendations. I am not completely done and I think I need some more guidance. 

 

I created a shift register inside while loop and inside case B (excluding my actual code). I did not include my actual code because I wanted to make it easier t0 test and debug. Please look at the picture below 

 

 

At this moment if I run my VI with Case B, my DO gives 5V and it never stops. And if I am running my case A, stop button does not stop my actual program and I still have 5V in my DO. 

 

CaseA.JPGCaseB.JPGCaseB_Idle.JPGCaseB_Write.JPGCaseB_Shutdown.JPG

0 Kudos
Message 3 of 4
(4,821 Views)

pkunwor, 

 

A few things to point out in your current program: 

 

1). I would recommend using a case structure with a timeout case in the "Idle" state containing the stop button. The event structure should respond to the stop change event, or the timeout should execute in an appropriate amount of time as Lynn mentioned: "The state machine only stays in any state for a few tens of milliseconds before checking for the stop command or other data which will cause it to change states"

 

Depending on how you want your program to execute the timeout case can send the state machine directly into the write case, or you can add a control on the front panel that the user selects to send the state machine into the "Write" case. You can use another event case in your event strucuture to respond to this "Write" control. 

 

2). Stopping and clearing the task should occur when your while loop exits or in the shutdown case, not the same case as the write. Take a look at one of the Measurement I/O examples in the LabVIEW shipping examples for a reference on the open --> read/write --> close structure for DAQmx tasks. 

Will M.
Applications Engineer
National Instruments
0 Kudos
Message 4 of 4
(4,787 Views)