Gamepad Input
Introduction: The gamepad is the driver’s control device. Your code reads sticks, triggers, and buttons each loop.
Basic theory: Joystick values are analog (-1.0 to 1.0) and button values are digital (true/false). Read inputs first, then apply logic, then send telemetry.
gamepad1 values
→
Java decision
→
motor/servo command
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
/**
* This OpMode demonstrates how to read basic gamepad inputs.
* It follows the same structure as the Telemetry Demo.
*/
@TeleOp(name="Gamepad Demo", group="Training")
public class GamepadDemo extends LinearOpMode {
@Override
public void runOpMode() {
// --- Initialization Phase ---
telemetry.addData("Status", "Initialized. Press Play to start.");
telemetry.update();
// Essential: wait for the driver to press the "PLAY" button.
waitForStart();
// --- Run Phase ---
while (opModeIsActive()) {
// Read the vertical position of the left stick.
double drive = -gamepad1.left_stick_y;
// Send the stick value to the Driver Hub.
telemetry.addData("Drive Power", "%.2f", drive);
// Check if the 'A' button is being pressed.
if (gamepad1.a) {
telemetry.addData("Button", "A Pressed");
}
// Essential: send the accumulated data to the Driver Hub.
telemetry.update();
}
}
}
| Input | Action |
|---|---|
| left_stick_y | Drive / motor power |
| A button | Button event / servo command |
| B button | Servo command |