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 toggle between states only once when a button is pressed and stay in that state until button is pressed again?

Solved!
Go to solution

Hello,

 

I am new to LabView and I am really struggling toggling between states when a button is pressed. The state should only change once per button click. and the state should remain until the next button click. I tried it with an additional register which holds a boolean whether the state was changed and if yes set it to false if the button was released but it is not working and super complex. You find the basic setup in the VI I uploaded. How would I achieve the desired behavior? Do I really have to manage multiple variable in order to check whether the state was already changed?

Additionally, how would I wait in the inactive state for the next button click to change the state so that there are no unnecessary loops. I am really struggling for some time but nothing works + it is just messy trying to manage multiple shift registers to really only change once per button click (pressed).

 

I would appreciate any help!

0 Kudos
Message 1 of 12
(1,547 Views)

Just use a button with the Mechanical Action set to Switch When Pressed.  Then you can just wire the sensor straight to the case structure.


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 2 of 12
(1,538 Views)

Thank you for your answer. I am trying to simulate hardware interactions. There is a touch sensor which has three options: released, pressed and bumped. That is why I have to use either released or pressed in order to correctly simulate the behavior since I currently have no access to the hardware. How would I do that if the only options were really released or pressed events?

0 Kudos
Message 3 of 12
(1,534 Views)

So it is very complex when having only the options released and pressed? Do you have any approaches how to manage state change with those two options?

0 Kudos
Message 4 of 12
(1,485 Views)

If you want to switch a LED whenever a latch action button is pressed, here's what you can do. try it!

 

altenbach_0-1654530572687.png

 

0 Kudos
Message 5 of 12
(1,475 Views)

Thank you for your answer. It is not a latch action button. In fact, it is the touch sensor button of a Mindstorm Ev3 robot. I try to implement switching cases whenever the touch sensor was pressed. That is why in my setup the button had switch until released as the operation to simulate that behavior. That is why I cannot change the button behavior as you did. So is there any other solution to my problem given the above explanation of why I can't use different button actions?  The touch sensor of the robot has three options: pressed (returns the value 1 as long as the button is pressed and 0 if released), released (returns the value 1 as long as the sensor is released and 0 if the button is pressed) and bumped (returns the value one for a short period of time when the sensor was pressed and then released). How would I simulate the desired behavior and correctly switch between the cases once when the sensor was pressed? I dont have access to the robot right now but want to implement the switching between the stages by simulating the touch sensor. 

0 Kudos
Message 6 of 12
(1,458 Views)

Sorry, I was not able to look at your code (LabVIEW 2021).

 

"Switch until released" does not guarantee that the button is read by the code, for example if you release it before the terminal actually gets read.

0 Kudos
Message 7 of 12
(1,451 Views)
Solution
Accepted by topic author Glypton

See if this can give you some ideas:

 

altenbach_0-1654537532583.png

 

Message 8 of 12
(1,448 Views)

Thank you! It works fine. I append a screenshot to this post so that you can see how it should work. Could you tell me whether it would be possible to wait in the inactive state until the next change to the active state (true case) so that the loop does not unnecessarily continue?

Glypton_0-1654539723236.png

 

0 Kudos
Message 9 of 12
(1,438 Views)

Well, you need to "loop" the loop, else the terminal never gets read again.

 

You don't need a case structure, of course. Make the code more readable!

 

altenbach_0-1654540321088.png

 

0 Kudos
Message 10 of 12
(1,432 Views)