Quote:
Originally Posted by Fauge7
Code:
boolean buttonToggle = false;
if (button.isPressed()) {
buttonToggle = !buttonToggle;
}
//code for using it:
if (buttonToggle) {
motor.setSpeed(someSpeed);
}
|
I do not believe this will work. What happens when your buttonToggle variable is false? You will need to add more to truly achieve toggle functionality.
Code:
boolean buttonToggle = false;
if (button.isPressed()) {
buttonToggle = !buttonToggle;
}
//code for using it:
if (buttonToggle) {
motor.setSpeed(someSpeed);
} else {
motor.setSpeed(0);
}
Another reason this may experience an issue is that your code will start from the top of TeleopPeriodic every 20 ms or so. So if you hold down the button for 100 ms, your buttonToggle command (and therefore your motor output) will rapidly switch back and forth between true and false 5 times before letting go of the button. If you hold down the button for 80 ms? Switching back and forth 4 times and now you end up right where you started before you hit the button.
Edit: Follow BL0X3R's suggestion to achieve true toggling functionality.