View Single Post
  #5   Spotlight this post!  
Unread 02-21-2016, 07:24 PM
NickB NickB is offline
Registered User
FRC #2002
 
Join Date: Jan 2016
Location: Oregon
Posts: 5
NickB is an unknown quantity at this point
Re: Toggle Button For reverse

As Ether said, you need to detect the "rising edge" of the button being held down. Here is a function I wrote for doing just this:
Code:
/*
This function allows a boolean to be toggled with a joystick button, inside of a while loop that is constantly updating.
	button:        Joystick button for toggling.
	buttonPressed: Boolean for tracking whether the button was pressed in the last cycle. This prevents toggleBool from rapidly switching states while the joystick button is held down.
	toggleBool:    Boolean to be toggled.
*/
void Robot::ToggleBool(bool button, bool &buttonPressed, bool &toggleBool){
	if(button && !buttonPressed){
		buttonPressed = true;
		toggleBool ? toggleBool = false : toggleBool = true;
	}else if(!button)
		buttonPressed = false;
}
...and here is how I implemented it:
Code:
ToggleBool(oJoystick->GetRawButton(JOYSTICK_BUTTON_REVERSE), reverseButtonPressed, reverse);
Reply With Quote