LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to start AND end a loop using a Boolean Button set to "Switch When Released" operation

I have a Boolean button set to "Switch When Released" operation. When I press this button (now it's in the On state) I want to run a loop that does something continupously (for now, let's say it displays a simulated sine wave) until I press the button again (now the button is in the Off state) in which case, I want to end the loop. I am wiring my button to the loop condition that's set to "Continue if true" and have a "Wait Until Next ms Multiple" of 200 ms (I tried up to 500 ms). When I run the VI and press the button the first time, I see the sine wave displayed. But when I press the button again, nothing happens. It looks like it does not respond to the user interface. What's happening. What am I doing wrong? I'm using an Event Handler design pattern. I've also tried the Producer/Consumer (Events) design pattern. But I don't imagine the particular design pattern should make a difference. I've attached my code. Thanx.
 
Fataneh
0 Kudos
Message 1 of 10
(3,475 Views)
Once an event is fired, it must finish before another event can be fired.  Also, no front panel controls are read until the event finishes.  So you can't use any controls inside the event case.  Instead, move your loop outside of the event structure and use a flag to run and stop the loop.  The event structure can control the flag.  See attached vi.
- tbob

Inventor of the WORM Global
0 Kudos
Message 2 of 10
(3,465 Views)
A simple solution would be to harness the timeout event, where you would put your data simulation. No need for parallel loops and local variables. 🙂
 
The event timeout is starts out as  -1 (infinite) but pressing the start/stop button toggles between a 200ms and an infinite timout whenever it is pressed. Since the evet structure always runs, all other events (e.g. stop) are also serviced.
 
Please ask if anything is not clear... 🙂 Good luck!

Message Edited by altenbach on 07-13-2005 04:20 PM

0 Kudos
Message 3 of 10
(3,460 Views)
Very good use of the Timeout event.  To take it one step further, if you exit while the loop is running, the Start/Stop button remains on Stop.  Adding a Reinit to Default will put the button back to Start when re-starting the vi.
- tbob

Inventor of the WORM Global
0 Kudos
Message 4 of 10
(3,457 Views)

Thank you altenbach and tbob. I'll try your suggestions and return with Questions, if any. Thanx again.

Fataneh

0 Kudos
Message 5 of 10
(3,450 Views)
Would you modify my VI and send it back to me. I'm trying to follow your suggestion but having a bit of trouble. For example, how to toggle between 200 ms and infinite timeout periods when start/stop button is pressed. Thanks a lot.
 
Fataneh
0 Kudos
Message 6 of 10
(3,449 Views)
My modification of your VI posted here does exactly what you need. The timeout event is stored in a shift register such that the correct timeout is used by the event structure on the next iteration. Do you know what a shift register is?
 
The timeout is set from a case structure, depending on the state of the big button. You can use execution highlighting to help you understand the code better.
0 Kudos
Message 7 of 10
(3,445 Views)
Thanx a bunch. You saved my life! Yes, I know what a shift register is. I'm sure I'll be able to understand your code. Thanx again.
 
Fataneh
0 Kudos
Message 8 of 10
(3,441 Views)
As tbob mentioned above, it might be desireable to reset the big button to false when stop is pressed to make sure it is in a defined state next time you run the VI. Easily done with e.g. a local variable in the stop event, see attached.

Message Edited by altenbach on 07-13-2005 05:40 PM

0 Kudos
Message 9 of 10
(3,437 Views)

altenbach,

since your suggestion was so helpfu the last time, I want to ask you how to do the following. I'd like to add the following to my software:

I'd like to specify a Starting Sinewave Duration (say 1 sec), an Ending Sinewave Duration (say 10 sec), and an Increment for the sinewave duration. I want the software to produce my sinewave (which is in my timeout event) beginning with the Starting Sinewave Duration value, then increment the duration by the Increment value and produce the sinewave for that amount of time, and continue this process until the Ending Sinewave Duration is reached and then it stops.

I have an idea to use the Tick Count function or Elapsed Time VI. Is that the best way to do it? I always wonder if there's a simpler way to do something, and I love simple code!

I've attached my code for you to look at. Thanks a lot.

Fataneh

0 Kudos
Message 10 of 10
(3,407 Views)