LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

stop while loop that contains a case statement

Hello.  I can not figure out how to stop my state machine that consists of a while loop and case statement.  I've broken it down (attached) to a very simple state machine and still can't find my problem.  I've moved the STOP button inside and outside the while loop to no avail.  Also, I don't see a property node that might help me out. 

 

Basically, I'd like to be able to stop in the middle of a state (case).  The state machine attached consists of 4 cases.  Each case only contains a time delay of 2 seconds and a constant for the next case in addition to indicating which state you're in.  I believe when you see the code you'll easily follow.  Any help is appreciated. 

 

Thanks. 

Message 1 of 9
(4,344 Views)

I don't have LV 10, so I can't open your vi.  But if you are using the wait (ms) function that is part of your problem.  The wait will basically freeze your program while it is waiting.  I consider the wait function as evil most of the time, especially if you want long waits.  One way around it is to use the get time/date in seconds before you go to the next case and compare it to the current time/date and when it goes above X seconds go to the next case.  That way the program still runs while it waits.

 

You will also need a way to check your stop button in each case.  I will add an example in a few minutes.

 

  The stop button also needs to be inside your while loop to work.

0 Kudos
Message 2 of 9
(4,336 Views)

Bryan is right.  If you want to be able to stop a state machine within a specified time, say 200 ms, then no state can take more than 200 ms to execute.  Actaully it can be worse than that.  Run your VI with execution highlighting (the Light Bulb on the block diagram tool bar) turned on.  Notice that the Stop button is read before or about the same time as the Time Delay starts executing.  Suppose you push the button 1 millisecond after it is read in state 0. State 0 will take 2 seconds to execute. Then state 1 will start and the stop button will be read again (True this time). After 2 seconds the case will complete and the loop will stop.  Total time from pushing the button until the loop stops: ~3999 ms.  This is basic dataflow.

 

Determine the longest time you can wait for a response. Make sure no state lasts longer than that without checking for the termination logic. Verify that the stop button is read and the termination logic is tested after the state executes.  In other words carefully design your state machine to meet the requirements. If a state must be maintained for an extended time, leave it to check for a termination command and then return to it until the totatl time has elapsed.

 

Lynn

 

 

0 Kudos
Message 3 of 9
(4,327 Views)

Here is a quick and very dirty example of what I was talking about.  For a better way to control the case structure go to File > New > Templates > Design Patterns > Standard State Machine.  Using a type-def for the states helps a lot with reducing errors and makes things easier than the way I did it.  I did it with a text box to keep the example to 1 file.

 

Somebody might have a better example, but mine should get you started.

0 Kudos
Message 4 of 9
(4,320 Views)

Here is an example of what you need to do.

Tim
GHSP
0 Kudos
Message 5 of 9
(4,308 Views)

Thanks, both examples helped.  Again, what I originally attached is a stripped down version of my real state machine, in which each case is a different subvi that can last between 10 seconds and 5 minutes.  I'm guessing I will find the max time each case should execute and go from there using these examples. 

 

I was also thinking, couldn't another thread be used to constantly monitor the button?  This might be too much to deal with though.  I'll stick with the examples I have. 

 

Thanks again! 

0 Kudos
Message 6 of 9
(4,296 Views)

You can use another thread. Instead of the Wait function, you can use a Wait on Occurance or Wait On Notification. Then in another loop (perhaps with an Event Structure), you can have a Set Occurance or Send Notification.

0 Kudos
Message 7 of 9
(4,288 Views)

Hi, I have a similar problem. I need to execute the state machine and then stop the while loop either if a Boolean button is pressed or if the sequence of events that must transpire are all done. In other words, I am iterating through a number of states to input different numbers and this iteration must stop on its own after a known amount of numbers have been input.

 

The problem that I'm having is that with the push-button, the while loop stops but when I try to stop it programatically by using the 'TRUE' constant, the program does not stop but keeps running without doing anything. In both cases, either the sequence of events is finished or the Stop button is pressed, the program goes to the same state "End".

 

Thanks.

0 Kudos
Message 8 of 9
(3,498 Views)

You will need to share your VI if you want any help.  We have no idea what your state machine looks like and it will be a complete shot in the dark as to what your problem is.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 9 of 9
(3,488 Views)