Programming a Motor with a Gamepad
Driving Motors with the Gamepad
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.
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 Block.
![](https://docs.revrobotics.com/~gitbook/image?url=https%3A%2F%2F1359443677-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FUOOiQ4S2QcMWmVoSmeQ8%252Fuploads%252FrI3dwoLP9zBPoUYPZwlR%252Fimage.png%3Falt%3Dmedia%26token%3D92c08027-4f9d-488a-b3db-eaf43fc71958&width=768&dpr=4&quality=100&sign=464a3d44&sv=1)
When using Blocks we are able to snap some blocks together.
Looking at the block you might notice it is the perfect shape to snap into the end of
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.
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?
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.
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.
From the Math Menu in Blocks select the block in the image below.
![](https://docs.revrobotics.com/~gitbook/image?url=https%3A%2F%2F1359443677-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FUOOiQ4S2QcMWmVoSmeQ8%252Fuploads%252FF0RtJ6xeh2EmHBpdw3Hz%252Fimage.png%3Falt%3Dmedia%26token%3Dc2c2eae1-716c-424f-bd0d-17912b7e9d47&width=768&dpr=4&quality=100&sign=b8a81e99&sv=1)
Drag the negative symbol block so it snaps in place between the and
blocks:
![](https://docs.revrobotics.com/~gitbook/image?url=https%3A%2F%2F1359443677-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FUOOiQ4S2QcMWmVoSmeQ8%252Fuploads%252FAWzDNXtOW6mvwSMPlCOZ%252Fimage.png%3Falt%3Dmedia%26token%3D4c6d143d-e18f-44c2-97f5-e8d59a3c0b2a&width=768&dpr=4&quality=100&sign=8c324bd5&sv=1)
Last updated