# Programming a Motor with a Gamepad

## Driving Motors with the Gamepad&#x20;

In the previous section you learned how to set the motor to run at a specific power level in a specific direction. However, in most applications, it will be necessary to control the motor with a gamepad, to easily change the direction or power level of a mechanism.&#x20;

We are able to use a button to set the motor to a specific power or we can program it so it changes based on the direction of one of the gamepad's joysticks!

***

From the Gamepad Menu in Blocks select the <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MWACJyDlnpPjKwQvDeW%2F-MWBHSepopUHbk0AbzGq%2Fgamepad%20-%20leftsticky.svg?alt=media&#x26;token=214b31c1-622c-4913-884c-a6b9d2e6ebb9" alt="" data-size="original"> Bloc&#x6B;*.*&#x20;

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FrI3dwoLP9zBPoUYPZwlR%2Fimage.png?alt=media&#x26;token=92c08027-4f9d-488a-b3db-eaf43fc71958" alt=""><figcaption></figcaption></figure>

When using Blocks we are able to snap some blocks together.&#x20;

Looking at the <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MWACJyDlnpPjKwQvDeW%2F-MWBHSepopUHbk0AbzGq%2Fgamepad%20-%20leftsticky.svg?alt=media&#x26;token=214b31c1-622c-4913-884c-a6b9d2e6ebb9" alt="" data-size="original"> block you might notice it is the perfect shape to snap into the end of <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MWACJyDlnpPjKwQvDeW%2F-MWBG9sHCXphC82ifCqS%2FMotor%20-%20%20testmotor%20set%20power.svg?alt=media&#x26;token=41a570cb-db24-4cc9-a41f-7bbd92ce8ff2" alt="" data-size="original"> block, over the 1, like a puzzle piece!

This set of blocks will now continually loop and read the value of gamepad #1’s left joystick (the y position) and set the motor power to the Y value of the left joystick.

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FqvVGk6m8KNjuQ7RlHG6p%2Fmotorwithcontroller.PNG?alt=media&#x26;token=811aef81-d742-48e7-8407-6685a64e902b" alt=""><figcaption></figcaption></figure>

### Quick Check!

Save your OpMode and test it out with your gamepad! Think about the following questions while testing:

* What happens when you move the left joystick up a small amount versus a large amount?
* What happens if you move the joystick to the left or right along the X-axis?
* What happens if you move the joystick at a diagonal or rotate it 360 degrees?

You may notice that when moving along the X-axis nothing happens at the moment. However, once the joystick hits an angle near the Y-axis vertices the motor may start to jitter and spin.

## Adjusting Y-axis Direction

When you tested your program, did the motor spin the expected direction while moving the joystick up or down?&#x20;

In the FTC SDK for most controllers the Y value of a joystick ranges from -1 when a joystick is in its **topmost** position, to +1 when a joystick is in its **bottommost** position.&#x20;

That may be a little confusing to control, but we can add a **negative symbol**, or negation operator, to the line of code to change the direction of the motor in relation to the gamepad.&#x20;

From the **Math** Menu in Blocks select the <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MWACJyDlnpPjKwQvDeW%2F-MWBJQVp02qR-776BhMA%2FMath%20-%20negatives.svg?alt=media&#x26;token=de3bec61-c328-4b6e-be0c-9c89c64e112b" alt="" data-size="original"> block in the image belo&#x77;*.*&#x20;

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FF0RtJ6xeh2EmHBpdw3Hz%2Fimage.png?alt=media&#x26;token=c2c2eae1-716c-424f-bd0d-17912b7e9d47" alt=""><figcaption></figcaption></figure>

Drag the negative symbol block so it snaps in place between the <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MWACJyDlnpPjKwQvDeW%2F-MWBG9sHCXphC82ifCqS%2FMotor%20-%20%20testmotor%20set%20power.svg?alt=media&#x26;token=41a570cb-db24-4cc9-a41f-7bbd92ce8ff2" alt="" data-size="original"> and <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MWACJyDlnpPjKwQvDeW%2F-MWBHSepopUHbk0AbzGq%2Fgamepad%20-%20leftsticky.svg?alt=media&#x26;token=214b31c1-622c-4913-884c-a6b9d2e6ebb9" alt="" data-size="original"> blocks:

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FAWzDNXtOW6mvwSMPlCOZ%2Fimage.png?alt=media&#x26;token=4c6d143d-e18f-44c2-97f5-e8d59a3c0b2a" alt=""><figcaption></figcaption></figure>
