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);