From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to pause and resume a while loop with a button

Solved!
Go to solution

Hello everyone!

 

I'm getting back into LabVIEW after a few years and I can't figure out, what on the face of it seems like a simple problem!

 

All I want to do is to have a while loop in which for the purposes of illustration I have the "random number 0-1" function inside, and I want to be able to pause and resume this using a button. There are some other posts on the forum which are very similar and the make use of "event structures". The problem I run into is that I can get the random number genarator to start generating however the pause will not stop this!

 

I have a feeling I am making a silly mistake - but I can't figure or google my way out of the problem! I have attached my solution so far!

 

Cheers! 

0 Kudos
Message 1 of 8
(6,212 Views)
Solution
Accepted by topic author QuantumPenguin

i made an example (you can drag and drop the picture into a labview block diagram >=2014

hope it helps (i just made 2 snippets, its the same program)

exmple-event-structure--1.png

exmple-event-structure--2.png


If Tetris has taught me anything, it's errors pile up and accomplishments disappear.
0 Kudos
Message 2 of 8
(6,185 Views)

That's a great solution thanks! Also I had no idea you can drag and drop images in - amazing!!

 

I have a seperate question but related to my problem. Do you know if there is a way to programaticaly create switch functionality from a latch button. Basically I want to have something that constantly outputs eiter true or false or is in a constant state of true or false but when it recives a true/false "pulse" from the latch on relase button it will flip its state - is there a function already in labview to do this? 

0 Kudos
Message 3 of 8
(6,177 Views)

if you want to do it yourself:

while in block-diagram, mark everything, and EDIT -> Create Snippet

 

:cheers:


If Tetris has taught me anything, it's errors pile up and accomplishments disappear.
0 Kudos
Message 4 of 8
(6,171 Views)

The first problem with this solution is that you don't need local variables at all here.  The stop button should be a "latching" button and the terminal should be inside of its value change event case.  This will allow it to unlatch.  The Run and Pause buttons should also be inside the event case where they are used.  Heck, all of those terminals should be used instead of the local variables.

 

Secondly, to go to anything more than a basic program, this really should be done in a state machine where the random number generation should be its own state that then calls the idle state (where and event structure is).  So when the pause is done, you just make sure to come back to the idle state instead of going on to the random number generation state.


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 5 of 8
(6,150 Views)

@crossrulz i know for this little example local-vars are not needed,

but i found when getting a little larger with my vis,

i always use them.

any specific reason, that might not be a good idea?

<edit> except for race conditions</edit>


If Tetris has taught me anything, it's errors pile up and accomplishments disappear.
0 Kudos
Message 6 of 8
(6,126 Views)
Solution
Accepted by topic author QuantumPenguin

@jwscs wrote:

@crossrulz i know for this little example local-vars are not needed,

but i found when getting a little larger with my vis,

i always use them.

any specific reason, that might not be a good idea?

<edit> except for race conditions</edit>


Memory (each local is a copy of the data).  Speed (the terminal is a little faster than the local).  They also make it hard to read a diagram.  If you find yourself using a lot of locals, then you are likely not using a good architecture and you would likely benefit from a State Machine, Producer/Consumer, Queues, User Events, and/or (Queued) Message Handlers.


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 7 of 8
(6,111 Views)

Thanks for the input everyone. I will lookup state machines and have a play!

 

0 Kudos
Message 8 of 8
(6,092 Views)