Toggle on/off with button press

I am a high school student that is in the PLTW program. I am trying to figure out how to make a button press toggle my program to enabled and disabled. I do not know if it is possible, or if we have to use a different method to do this.

currently using an if statement to check for the button press, then depending on the state variable initialized before, it turns it on or off, however it never seems to start the motor. Any good toggle methods that work?


There are several example toggle codes around the forums, if you just search “toggle” you could find one that works.

But, for the logic behind it. For a toggle you cant simply say that when a certain button is pressed on, switch to the opposite toggle action. This is because every time the program runs through the loop (probably about every 10ms or 20ms) it will sense that the button is on and switch the toggle state, and will constantly do so until you release the button. What you are missing in this scenario is a way to check the previous state of the button and prevent the toggle state from changing unless the button state had changed since the last run of the loop.


I’m not sure if it’s a problem with v5 but back with the cortex system I found that the buttons would very infrequently send back ‘false’ when held down, so you may need to have the program wait for it to return false several times in a row to determine when the button is actually being released.