Control a Motor with a Game Pad Joystick
Your Driver Hub can connect up to 2 game pads, which provide input to your robot and allow your drivers to have direct control over what your robot does on the field. Plug a controller into your Driver Hub and press the START and A buttons simultaneously to identify it as gamepad 1. When you need a second controller later, you’ll press START and B at the same time to mark it as gamepad 2.
The buttons on the game pad show up in your code as boolean values - true or false.
The joysticks show up as numbers that range between -1 and 1.
The y-axes go from 1 at the bottom to -1 at the top, and the x-axes go from -1 to the left & 1 to the right.
The triggers on the back of the gamepad give you a number between 0 and 1.
Let’s directly turn how far up or down you move the left joystick into how fast our motor spins. Change your line in the loop method that controls your motor speed to this:
myMotor.setPower(gamepad1.left_stick_y);
Build & run your OpMode and see what happens! Try to get it to move when you move the right stick from left to right instead.