View Single Post
  #4   Spotlight this post!  
Unread 01-03-2016, 16:11
KrazyCarl92's Avatar
KrazyCarl92 KrazyCarl92 is offline
Registered User
AKA: Carl Springli
FRC #5811 (The BONDS)(EWCP)
Team Role: Mentor
 
Join Date: Jan 2010
Rookie Year: 2010
Location: Dayton, OH
Posts: 519
KrazyCarl92 has a reputation beyond reputeKrazyCarl92 has a reputation beyond reputeKrazyCarl92 has a reputation beyond reputeKrazyCarl92 has a reputation beyond reputeKrazyCarl92 has a reputation beyond reputeKrazyCarl92 has a reputation beyond reputeKrazyCarl92 has a reputation beyond reputeKrazyCarl92 has a reputation beyond reputeKrazyCarl92 has a reputation beyond reputeKrazyCarl92 has a reputation beyond reputeKrazyCarl92 has a reputation beyond repute
Re: Java Iterative-Robot: Toggle Buttons?

Quote:
Originally Posted by Fauge7 View Post
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.
__________________
[2016-present] FRC 5811 - BONDS Robotics
[2010-2015] FRC 0020 - The Rocketeers

Last edited by KrazyCarl92 : 01-03-2016 at 16:13. Reason: Added note.