LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

While Loop Stop Button Does Not Work

Solved!
Go to solution

Hi everyone,

 

I realise there are a lot of posts with similar titles already, but none of them address the problem I'm having.

 

The "stop" function at the bottom of any while loop fundamentally does not work for me, whether its' connected to a toggle switch or any kind of function.

This is a problem for me because It's the only way I can find to regulate how often things happen.

 

I've attached a simple example VI to show what I mean.

Any help would be much appreciated!

(I'm very new to labview)

0 Kudos
Message 1 of 8
(218 Views)

(You didn't attach anything ;))

 

So, what you have to understand is "dataflow". Any given "node" (be it a subVI, loop, etc) will only "return" and move on after everything inside has finished executing. A While loop has a terminal that says "If this is False, run the loop again. If it's True, move on."

 

The terminal doesn't stop it now, it prevents it from running again. To stop what you're doing now depends on what it is you're doing. If you have an event structure (for example), then that structure won't move on until one (and ONLY one) of its events trigger. For a Stop function, you probably want to configure one event case to trigger on the Stop button being pressed. That will exit the Event structure. Then, if you have the Stop button connected to the Stop terminal, the While loop won't execute again, and (assuming there isn't something after the While loop) the program will stop.

 

By the way, most people don't have the latest version of LabVIEW, so use File- Save for Previous to version 2020 or so.

Message 2 of 8
(207 Views)
Solution
Accepted by WillGard

Code looks fine (but see below!)

 

altenbach_0-1741890624230.png

 

 

My suspicion is that you are using the "continuous run" mode, so it will restart as soon as the VI completes. Don't!

 

Notes:

  • The stop button should probably be latch action (else it is true the next time you run and it will stop immediately).
  • I would also place a short wait inside the loop. I seriously doubt that you need to poll all controls millions of times per second, saturating at least one CPU core.
  • . start with some basic tutorials.
Message 3 of 8
(206 Views)

Thank you!

It looks like an event structure was what I really needed. I wanted to prevent the random number generator from running continuously using the while loop's stop terminal, but I see that it isn't meant for this.

I'll save my VIs as previous versions in the future too.

0 Kudos
Message 4 of 8
(142 Views)

@WillGard wrote:

Thank you!

It looks like an event structure was what I really needed. I wanted to prevent the random number generator from running continuously using the while loop's stop terminal, but I see that it isn't meant for this.


In your case, everything in the loop is running as fast as the processor allows, polling the controls and updating the indicators, (not just the random generator!) and once you press stop, the entire VI goes back to edit mode. Edit mode has no meaning to the end user of your code.

 

You did not list any of the code requirements (when should a random number be generated, what else should happen, what is the purpose of all this?)

 

I have the feeling that you should not start using an event structure until you are more familiar with the concepts of dataflow. If you only want to generate a single random number on demand, I would recommend to implement a very simple state machine that can easily be expanded once more functionality is needed. You also need to decide what the indicator should show at program start and before the first random number is generated.

 


@WillGard wrote:

I'll save my VIs as previous versions in the future too.


Start by attaching your "event" solution so we can offer more specific guidance.

0 Kudos
Message 5 of 8
(136 Views)

Thank you!

The wait function does a great job here (though it's less helpful when it only waits once at the start of the simulation). I thought the stop terminal had to recieve a constant signal so thanks for clearing that up.

0 Kudos
Message 6 of 8
(129 Views)

@WillGard wrote:

The wait function does a great job here (though it's less helpful when it only waits once at the start of the simulation). I thought the stop terminal had to recieve a constant signal so thanks for clearing that up.


None of what you say makes any sense!

 

(What is "receive"/ What is a "constant signal"? "Helpful" for what? What "simulation"?)

0 Kudos
Message 7 of 8
(121 Views)

@WillGard wrote:

I thought the stop terminal had to recieve a constant signal so thanks for clearing that up.


You clearly don't understand the idea of Data Flow.  Suppose you have a While Loop and a Push Button (which you label "Stop") wired to the While Loop.  You precede it and follow it with a Wait of 5000 ms (= 5 seconds), and use a Frame Sequence to do the first Wait, read the Stop button, and a second Wait.

 

Start this running.  How do you stop it, and when does it stop?  Can you explain why it behaves as it does?  [Think about the three principles of Data Flow].

 

The Stop Pushbutton is a "Push button", which means it is True when being pushed and False when not being pushed.  Is this a good "mechanical action" for a "Stop Button"?  How would you want it to behave?  if you don't know about "mechanical actions", look it up!  Did you notice that some Boolean Controls on the Boolean palette are rectangular?   They all have a different "mechanical action".  Also, notice that the control that (on the Front Panel Boolean palette is called "Push Button" does not have the "Push Button" mechanical action.

 

 

DEMO Data Flow Example.png

 

This is simple enough that you should be able to recreate it (if you don't know about Snippets or can't handle LabVIEW 2021), but I'll also attach the VI.

 

Bob Schor

0 Kudos
Message 8 of 8
(83 Views)