LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How To Exit While Loop with Timing Delay?

Solved!
Go to solution

I have a fairly cumbersome program that iteratively takes a series of measurements from a number of channels using various NI-Switch and NI-DMM VIs that I assembled following various examples and many help pages. Each channel is to be sampled once during each iteration with data to be logged in a text file for later analysis after it is imported into Excel. I added "real-time" on-screen indicators and graphs to see what the data being collected looks like as it runs.

 

Each iteration is performed using a While Loop with a delay timer, although for a previous version I used a Timed Loop. Measurements for the series of channels are collected with a For Loop inside the While Loop. The For Loop includes VIs that:

1. Switch to the proper channel and wait for debounce.

2. Initializes the DMM and passes the setup information.

3. Wait for a time to ensure that the DMM has settled.

4. Take a measurement.

5. Close the DMM.

6. Disconnect all switches.

 

The polling of the channels completes in 15-20 seconds, and I am currently collecting data at two-minute intervals, so it spends more time "waiting" than "collecting." Each iteration of the For Loop initializes and closes the DMM to avoid hot switching.

 

Other components of the program are setup parameter controls and the like (in front of the While Loop), a "current measurement" indicator and a write-text-file operation inside the For Loop, several iteration indicators (inside the While Loop, but outside the For Loop), and a bunch of bundling and array operations that provide graphs of groups of measurements vs time.

 

I am sure there are more elegant ways to perform many of the functions I am doing and I am not posting the VI here because I want to focus on what I know is a big problem with my program (for now!)- I can't figure out how to exit the While Loop. I have tried combinations of the Boolean control, its latch conditions, and Stop/Continue if True to no avail.

 

I want a "Stop" button that terminates the While Loop at the next opportunity, but it's not working properly, I suspect, because the Stop button is not being read with proper timing because the program is either executing the For Loop or the time delay at the start of the While Loop. "Next opportunity" can mean "Immediately", "At the completion of the next iteration of the For Loop", or "At the completion of the next Timing Delay." I suspect if I get one of these scenarios to work, I can get any of them to work!

 

Please help!

Jeffrey Zola
0 Kudos
Message 1 of 15
(8,073 Views)

Jeffrey,

 

Dataflow. Dataflow. Dataflow!

 

The stop switch is probably read very early in each iteration of the while loop (within nanoseconds or microseconds of the start of the iteration) because it probably does not depend on anything else in the loop. However, the decision to terminate the loop is the last thing the loop does in each iteration.  The loop cannot terminate things inside it until they all execute.

 

Similarly, the for loop will run all of its iterations before it stops.  If you are using a recent version of LV, you can use the conditional for loop which has a termination terminal like a while loop.

 

Most likely the best fix would be to convert to a state machine architecture with the states carefully designed so that the system never remains in any state for longer than the maximum acceptable stop response time before checking for urgent requests. It might look something like this:

 

Send switch channel set command.  Check for stop.

Wait short time. Check for debounce. Check for stop. If no debounce or stop, then repeat this state.

Initialize DMM.  Check for stop.

Send DMM Setup information.  Check for stop.

Wait short time. Check for DMM settling.  Check for stop. If not settled and no stop, then repeat this state.

Read measurement.  Check for stop.

Close DMM.  Check for stop.

Disconnect switches.  Check for stop.

 

You get the idea.  For example suppose the debouncing takes 5-6 seconds and you want the response to the stop button to occur within 1 second. Then the debounce state would execute about 6 times, waiting about 1 second each.  If the stop button is pressed after 2 seconds, the state machine jumps to the shutdown state at the end of the second 1 second wait.

 

One way to speed up the response of your existing program without converting it to the state machine would be to put the stop button in an event structure in a parallel loop. When the stop button is pressed, send a Notification to the loop with the other code.  Put Wait on Notification functions with short timeouts any place in the existing code where you can interrupt the process. You can put case structures around repeated time-consuming code so that when the Stop button is false the code executes and when it has been pressed, the empty case executes. The for loop will zip through all the empty cases in nanoseconds and end so that the while loop can continue.  Depending on how complex your code it, this could get very messy and there probably are some parts which will still take several seconds before the Notification can bypass them.  Getting this working might be almost as much effort as redoing the program but with much less versatility. No way to tell withuot seeing the code.  Since you seem to have some flexibility in defining "next opportunity" to stop, this may be a reasonable approach.

 

Lynn

 

 

Message 2 of 15
(8,067 Views)

when i want to INSTANTLY EXIT a loop process, i use the "notifiers" and the "time out?" node inverse or'd connected to the stop, this will fire once the notification "ABORT" is sent and received....ABORT NOTIFIER.png

0 Kudos
Message 3 of 15
(8,041 Views)

Weird anomaly… on the “code capture tool” in the before snapshot?  i thought i mis-wired and again look at the "error" bundle out ....Smiley Indifferentabort%20notifier%20ver8.5[1]_BD.png

0 Kudos
Message 4 of 15
(8,030 Views)

What specific delay timer are you using ?  Wait (msec) or Time Delay express vi ? It sounds like it may be a blocking delay which will not continue with further program execution until its time has expired.  What you could do is to use multiple shorter delays (say 100 msec) that in total would add up to your desired final delay duration.  Use a For or While Loop to reach the desired total time delay.  After each of the short delays, check the status of your Stop button.

 

There is also the Elapsed Time express VI which has a Time has Elapsed output which can be used in a loop to indicate the remaining delay time and allows the delay time to be changed while the delay is running.

 

 

Steve

0 Kudos
Message 5 of 15
(8,019 Views)

Each delay is using the "Wait (ms)" VI. I have set up the amount of time to wait as inputs.

 

The DMM settle time is wired from the input outside the While Loop to the delay inside the For Loop.

 

The iteration period is input in minutes outside the While Loop. I bring the input into the While Loop to calculate the actual delay time for each iteration to start at the zero second mark of the appropriate minute based on the current time. I "borrowed" the setup for this from a sequence I found in a help file. 🙂

 

I like apok's idea of using Notifiers I played with them once and will see if I can incorporate a notifier to terminate the While Loop at the completion of the data manipulations after the For Loop upon pressing a button.

Jeffrey Zola
0 Kudos
Message 6 of 15
(7,996 Views)

@Jeffrey_Zola wrote:

 

I like apok's idea of using Notifiers I played with them once and will see if I can incorporate a notifier to terminate the While Loop at the completion of the data manipulations after the For Loop upon pressing a button.


make it a conditional for loop and wire it to the notifier...Smiley Wink

 

and i would not use the "wait ms" to time your process, use the "time delay".CLD architecture style par.7.b.3: Avoid using any of the Wait functions to time a software operation. CLD Preparation guide

0 Kudos
Message 7 of 15
(7,990 Views)

Hi, apok.  Why do you use a queue in this example?

 

Jeff

Jeffrey Zola
0 Kudos
Message 8 of 15
(7,979 Views)

@Jeffrey_Zola wrote:

Hi, apok.  Why do you use a queue in this example?

 

Jeff



well, you can use any number of different types of architectures..depending on your application. recommend getting "labview style book" by bloomy controls. it explains in detail the many different architectures and rules when and what to use.....programming is like an art (style) to solving a problem, and there are many different ways in "turning a wrench".Smiley Wink

0 Kudos
Message 9 of 15
(7,968 Views)

As I run the example code you attached, it gets "confused" if the "start test" button is pressed more than once and does not stop the random number generator/chart when "STOP" is pressed. The confusion lingers until the execution is aborted and "STOP" is pressed. Is this your intended behavior?

 

Jeff

Jeffrey Zola
0 Kudos
Message 10 of 15
(7,951 Views)