LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Run part of code once if user presses button

Solved!
Go to solution

Hi all,

 

I am trying to run a portion of my code only once if the user presses a button. If the button is pressed once more the code should run once more as well. Is there a simple way to accomplish this?

 

The strategy I took was encasing the relevant portion of the code in a case structure which is controlled by a boolean switch. This case structure is then enclosed in a while loop. My thinking is that the while loop continuously checks the boolean and if it is true the code within the case structure is run. However, the issue with this is the code might run more than once even though ideally I want it to run only once. Additionally, I'm not sure if it's poor arrangement to have the code continuously check the while loop. I attached a labview file demonstrating this.

 

Is there a more simple / cleaner way to accomplish this? Thanks.

0 Kudos
Message 1 of 3
(1,363 Views)

@KnicksFan wrote:

Is there a more simple / cleaner way to accomplish this? Thanks.


Yes.  Take any of the LabVIEW Introductory Tutorials that introduce the major Structures (the first section of Palettes in the Block Diagram) and explains when, how, and why you use them.  Pay attention to those that are "other than" the While Loop and Case Structure.  Look at the examples.

 

Bob Schor

0 Kudos
Message 2 of 3
(1,339 Views)
Solution
Accepted by topic author KnicksFan

@KnicksFan wrote:

Hi all,

 

I am trying to run a portion of my code only once if the user presses a button. If the button is pressed once more the code should run once more as well. Is there a simple way to accomplish this?

 

...

 

Is there a more simple / cleaner way to accomplish this? Thanks.


I think an event structure is what  is referring to (I'm also new, but I've seen it done this way). Looking at your code, it looks like you can get away with putting everything in a event structure that runs when the switch is pressed. (If any of the more experienced members correct me, listen to them)

 

Here is a basic video on Event Structures

https://www.youtube.com/watch?v=V8Yk8MdLUII&t=27s

 

Here is a page on event driven programming in LabVIEW

https://www.ni.com/en-us/support/documentation/supplemental/06/event-driven-programming-in-labview.h...

and a page on Event Structures

https://zone.ni.com/reference/en-XX/help/371361R-01/glang/event_structure/

And an important page on Caveats and Recommendations when Using Events in LabVIEW

https://zone.ni.com/reference/en-XX/help/371361R-01/lvhowto/caveatsrecmndtnsevnts/

 

I'll be reading the Caveats and Recommendation and event driven programming in LabVIEW pages today myself.


___________________________________________________________________________________________________________________________________________________
Upgraded from intern to undergrad automation engineer. Attempting to explain to my employer why I need official training…. since I’m the groups only automation engineer.

I tried. I really did.
Message 3 of 3
(1,295 Views)