Using If Statements
If we want to make things happen when we press the buttons on our game pad, we’ll need to use if statements.
if statements let us run bits of code only when certain boolean - true or false - values,
like what the game pad object gives us for the buttons, are true.
So, if we wanted to set the position of our servo to 0 when we press the X button, we’d do something like this:
if(gamepad1.x) {
servo.setPosition(0);
}
We can chain several if statements together with else if statements so that only the first condition that is met will run the associated code.
if(gamepad1.x) {
// do one thing
} else if(gamepad1.y) {
// do something else
}
You can also use an else statement to have your code do something if none of the conditions are met:
if(gamepad1.x) {
// do one thing when the button is pressed
} else {
// do something else when the button isn’t pressed
}
Remove your previous code to move your servo.
Instead, use if, else if, and else statements to make the servo move to 0 when the left bumper is pressed,
1 when the right bumper is pressed, and 0.5 when neither is pressed.