05-28-2013 07:05 AM
Hi all. I have put in a 10 seconds delay for my while loop for perform some task but I wish to stop it immediately when a stop signal is given to the loop.
I have tried many ways but I still couldn't stop the loop.
05-28-2013 07:23 AM
Where does the stop signal come from ? User interface, hardware... ?
Post your code and we will adapt it for you.
05-28-2013 07:37 AM - edited 05-28-2013 07:41 AM
You can split up the 10 seconds delay into many small waits (of 50ms ?) and use "Get Date/Time In Seconds" to see if 10 seconds have passed. You can then stop the loop if this condition has been met or if anyone has pressed 'STOP'.
So you will have the loop run many times with a short delay and check the current time and 'STOP' button each time to see if you should stop the loop.
If you only want the loop to run once you could create a second loop inside the one you have using the same mechanism in that second loop as I described above.
05-28-2013 07:47 AM
@m3rv89 wrote:
Hi all. I have put in a 10 seconds delay for my while loop for perform some task but I wish to stop it immediately when a stop signal is given to the loop.
I have tried many ways but I still couldn't stop the loop.
Check the attached example (saved in version 8.0)
05-28-2013 09:13 AM - edited 05-28-2013 09:16 AM
Thank you for all your comments above.
Here is why i am trying to do. Lets assume I have a while loop with 3 LED indicator, each delayed with 1 second in a order sequence structure framed. Whenever the stop button is pressed, the program continues to run to finish its 3 seconds delay.
I would like to stop it before the second LED lights up or stopping the while loop immediately whenever the stop button is pressed.
Attached is the screenshot. Thanks alot!
05-28-2013 09:19 AM
Do NOT use sequence structures.
This is the main source why you cannot stop inbetween.
What you should use is a state machine. This enables you to poll for a stop condition and, if it does not occur, continue to wait and switch to the appropriate action when needed.
hope this helps,
Norbert
05-28-2013 09:31 AM
Thank you for the suggestion of state machine. However, if a delay of n seconds (for instance 5 seconds) is placed in a while loop or a for loop or whichever case, it will eventually delayed for 5 seconds before doing the next task? Please correct me if I am wrong. Eventhough with a state machine, when it does poll for the next action, it must serve its delay of 5 seconds implemented before going to the next state. Am i right?
05-28-2013 09:38 AM
You wouldn't use a static wait time of several seconds. This also blocks the whole application.
You would rather poll a timer for "elapsed time: (search for this 🙂 ) and if the time is elapsed, go to the appropriate state.
The state machine will therefore include a state like "check for elapsed waiting time" which will poll the stop button and, if pressed, go to the shutdown case instead of resuming the waiting...
Norbert
05-28-2013 10:47 AM - edited 05-28-2013 10:50 AM
see if you can better understand using a state machine, polling, elapsed timer, transitions to different states, data flow....and not being locked in with flat sequences.
05-28-2013 06:54 PM
Dear Nobert and Apok, thank you guys for your helpful suggestions and different ways of tackling the delay issue.