Adding Gamepad Control
Motors
We have our motors running autonomously, but now let's add some manual control with a gamepad.
The Gamepad menu is always available in Blocks to search for the desired button or joystick control.

For our motors, we can set the power of the motor to be equal to the movement of the joystick. As the joystick moves up and down along the y-axis, it's value changes from -1 to 1 with 0 being off.
Click the gamepad block for the LeftStickY to the motor power block of the Core Hex Motor.

Before testing, it's good to note that by default the direction of the Y-axis on the gamepad is inverted, meaning if I moved the joystick up expecting the robot to move "forward" the motor will actually spin in reverse.
While programming, we can adjust this by adding a negative symbol from the Math menu.

It will snap between setting the motor power and the joystick calls:
Save your OpMode and test it out!
When you are ready, add similar controls for the HD Hex Motor on the RightStickY.
Angular Servos
Because our servos are moving between positions rather than setting a power, they must be bond to a Boolean button on the gamepad, such as the D-Pad, bumpers, or symbol buttons.
What happens if we try to connect one of these gamepad buttons direction to where we set the servo position?

Blocks will not allow this connection since it would not be functional. We need to instead use a Logic block to say "If the gamepad button is pressed, do ________"

If/Else Statements
If/Else statements are one of the most common and fundamental logic statements in programming. As the name suggestion, this allows us to program a check for the robot to see if something is happening then react based on the options available.
In this instance, let's set our If/Else to be if the Triangle button on the gamepad is pressed move the servo to Position 1.

Save your OpMode and give it a test!

If I want to add a second button for the servo to respond to, do I need a separate If/Else?
If If/Else block is editable to add additional checks all together! To do so click the gear to open the block's menu.

Adding additional "else ifs" extends the blue block:

Give this a try to set a variety of servo positions based on which button is pressed!
Reading If/Else Statements:
Let's take a look an example of how an If/Else Statement reads in a human friendly way.

The above can be read "If the Y button is pressed then move the servo to position 0, else if the A button is pressed move the servo to position 1."
Each pair contains the check or "if" and what should happen, the "do". Since there are multiple checks, after the first is reported as false the robot moves on to the next one, "else if".
Last updated
Was this helpful?


