LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Repeat an action (based on different trigger) efficiently without repeating the code

Hello,

 

I'm trying to find an efficient and clean way to do an action tha can be triggered by different things without having to repeat the piece of code itself at various places in my VI (after the said trigger things).

 

An example (because my explanation is pretty clumsy):

- The "action" (piece of code) is reading a file displaying the results (tiny more complicated than that but that shows the idea)

- I want that action to happen when for example I save data in the file, open an existing file or press a button

- Right now Im using the same piece of code at each places (when I write in the file, open existing file and press the button) using local variables

 

I'm trying to find a more efficient way to do that, so I can write the code only once.

 

FYI the whole thing is in a big while loop. The VI Im working on is pretty huge and complex so It doesnt really make sense to share it.

 

Hope my question is clear. Thanks !

0 Kudos
Message 1 of 9
(2,842 Views)

Hey, it's always best to simply create a subvi when you use the same code multiple places in your program.  You can actually just highlight the piece of code and click edit-create subvi.  This is the best way to go.  Here is a link that shows screenshots.

 

http://www.ni.com/white-paper/7593/en/

 

Good Luck.

0 Kudos
Message 2 of 9
(2,830 Views)

 I was actually thinking about subVI but was wondering if it was the best way. One little problem is that I have multiple indicators I need to update so I still have to connect the subvi to local variable of the said indicators.

0 Kudos
Message 3 of 9
(2,828 Views)

Currently this is the temporary way I found to do what I want

 

Repeat code.png

 

Now that I see it I could just put the "refresh" button as a non-latch /non-release and change it to true when needed in my code.

0 Kudos
Message 4 of 9
(2,821 Views)

A good state machine could handle this easily.  And it might even get you to not use local variables.


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 9
(2,816 Views)

Definitely without a doubt use a state machine.  You most-likely won't have to use local variables.  Labview has good templates and examples of the state machine architecture. 

0 Kudos
Message 6 of 9
(2,781 Views)

Ok thanks. I know nothing about state machine, I'll do some googling.

0 Kudos
Message 7 of 9
(2,771 Views)

Application Design Patterns: State Machines


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
Message 8 of 9
(2,768 Views)

Good Luck

0 Kudos
Message 9 of 9
(2,762 Views)