How to program a button this way?

Rather than program “do [this] when the button is pressed down, do [that] when it’s not pressed down”, I want to program “do [this] when it’s pressed, then do [that] when the same button’s pressed again” and alternate between them. It would be really easy in a text based language but I’m a LabVIEW noob and don’t know how to do it :confused:

I’m working with pneumatics. Basically I just need to give one Vi a value of 0 and another Vi a value of 1 when a button is pressed, and leave it that way even when I let go of the button. Then, next time I press the button, switch the two values. Then next time, switch them back, etc.

Pneumatics start retracted. Push button x to push the pneumatic arms out. Push button x again to pull the pneumatic arms back in

Do a search for “toggle” and you will find about a dozen ways to toggle between two states.

http://www.chiefdelphi.com/forums/showthread.php?t=113409&highlight=toggle

When you encounter a problem like this in LabVIEW, and it seems like “Hmm. I can’t be the first person to want to do this. I’m sure somebody has already figured this out”… then go to:

http://team358.org/files/programming/ControlSystem2009-/LabVIEW/

… and scroll down to “Common Robot Operations”

358’s solution works, but our team’s method is a bit simpler.

EDIT: It’s remarkably similar to the solution in the thread Omar posted above. Take your pick!

Its called a feedback loop. It looks like this:

Feedback Loop.PNG


Feedback Loop.PNG

Focussing on the original problem, I’m curious what solution you would have done in a text language? I ask this because this sort of thing comes up often as folks are just learning to program or are learning a new language, and I’m wanting to make sure that I understand the things that kept you from finding the solution.

Greg McKaskle

In a text language, I’d just make a boolean PneumaticsRetracted or something like that. Then when I press the right button, if the bool is true, do one thing, if it’s false, do the other. And in both, change the bool’s value to the opposite of course

In the text language, the Boolean variable would need to be static or global in order to keep its value from call to call. That serves the same purpose as the feedback node. It remembers state data in a scope that is longer lived than the function call.

In a text language, the local variables, and in LV, the wires, have a short-lived scope that is cleared each time the function is called. They are temporaries.

FYI, you can actually make global variables in LV too, so if you don’t get the hang of the feedback node, try with globals. But the feedback node or shift registers are a somewhat safer and more controlled language feature, like static local variables are in C, etc. So it is good to learn how to use them.

Greg McKaskle