# 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="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2FSym2TgJW30Chz6djVgTJ%2Fimage.png?alt=media&#x26;token=388d2fe9-f22f-4c7d-b52d-d10b54af4106" 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="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2FhLrgEUGVhPFhE78opPSk%2Fimage.png?alt=media&#x26;token=366adeec-855d-4817-9fb6-158f04a3b313" 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="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2Fh0UepKycb2qPx1kAekwn%2Fimage.png?alt=media&#x26;token=2898914d-f004-45e9-af00-7d790422ca34" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2FFXIEJtBCbULOoBacYXSB%2Fimage.png?alt=media&#x26;token=5868f455-ba35-4894-9797-7cc2a9c607e0" 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="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2Froy2h8nWR8RoNUGFUV8M%2Fimage.png?alt=media&#x26;token=1763c910-e6a3-44e0-b7b3-1a0cecfbef64" 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="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2Fhu3RAeDfocWUhKKbpiSc%2Fimage.png?alt=media&#x26;token=ac94252e-f056-470d-86fd-ef6350fd4c27" 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="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2F6vJnsM2Wdqb5n2QzovGY%2Fimage.png?alt=media&#x26;token=2f7b7388-660f-4f2d-9459-0b278002a61a" 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="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2F3aqmo7XNehqntgx49rGz%2Fimage.png?alt=media&#x26;token=729ff3f3-e3c8-49c3-8f78-d929f9a6f6a9" alt=""><figcaption></figcaption></figure>

Save your OpMode and give it a test!

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2F980uklR3DquKY0kuurfN%2Fimage.png?alt=media&#x26;token=37213d14-ad36-4180-b1a2-099e8b48ed4f" 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="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2F4KEpGwDejszWtqJ07e2B%2Fimage.png?alt=media&#x26;token=1f84f967-0101-4cc3-96f1-2ff8a5b25493" alt=""><figcaption></figcaption></figure>

Adding additional "else ifs" extends the blue block:

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2Fr5XxlWuzYFe4vocI3KaY%2Fimage.png?alt=media&#x26;token=369fa5b1-1572-4f53-85e2-65edfd9ac5c7" 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="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2FQuNAoiCjEob6r3DhwQCK%2Fimage.png?alt=media&#x26;token=545ecf8f-8069-47fa-8ba1-da4d3f706f0d" 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
