> For the complete documentation index, see [llms.txt](https://docs.revrobotics.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.revrobotics.com/rev-professional-development-technical-path/first-time-programming/first-opmode/adding-gamepad-control.md).

# Adding Gamepad Control

## Motors

We have our motors running autonomously[^1], but now let's add some manual control with a gamepad.&#x20;

The Gamepad menu is always available in Blocks to search for the desired button or joystick control.&#x20;

<figure><img src="/files/vqcIfL6KXxeLKx0c6IgI" alt=""><figcaption></figcaption></figure>

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.&#x20;

Click the gamepad block for the LeftStickY to the motor power block of the Core Hex Motor.

<figure><img src="/files/YuwlWuOt4c5dDd4tEKBo" alt=""><figcaption></figcaption></figure>

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.&#x20;

While programming, we can adjust this by adding a negative symbol from the **Math** menu.

<figure><img src="/files/aOjnrR2N0R0oBAikR4eo" alt=""><figcaption></figcaption></figure>

It will snap between setting the motor power and the joystick calls:

<figure><img src="/files/4CyC8IUTR1bxP8vCTwLs" alt=""><figcaption></figcaption></figure>

Save your OpMode and test it out!

When you are ready, add similar controls for the HD Hex Motor on the RightStickY.

<details>

<summary>Example Code</summary>

<figure><img src="/files/Wiu8Bq7ukSSJjysW1uEA" alt=""><figcaption></figcaption></figure>

</details>

## 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.&#x20;

What happens if we try to connect one of these gamepad buttons direction to where we set the servo position?&#x20;

<figure><img src="/files/8fN2rPPnozZzinMsHkE1" alt=""><figcaption></figcaption></figure>

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 \_\_\_\_\_\_\_\_"&#x20;

<figure><img src="/files/F6j1smRuUFsmvpX3wsNv" alt=""><figcaption></figcaption></figure>

### 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.&#x20;

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.&#x20;

<figure><img src="/files/nbCShCZGh1NwEzouyGVV" alt=""><figcaption></figcaption></figure>

Save your OpMode and give it a test!

<figure><img src="/files/TqLJ0p3NDajMPDLEUh4N" alt=""><figcaption></figcaption></figure>

#### 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.

<figure><img src="/files/WlCByfS3zmKG5Y0g8ZhF" alt=""><figcaption></figcaption></figure>

Adding additional "else ifs" extends the blue block:

<figure><img src="/files/XoZIMtzvXNJXIPKor0j2" alt=""><figcaption></figcaption></figure>

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.&#x20;

<figure><img src="/files/Uy5N05Q9Qx2zd5mB1uSW" alt=""><figcaption></figcaption></figure>

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."&#x20;

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**".

[^1]: Without human input
