Skip to content
learnFRC.org
programminglesson25 min

Triggers & Controller Bindings

Event-driven robot control using CommandXboxController, whileTrue, onTrue, toggleOnTrue, and sensor-based Triggers.

What you will learn

  • Configure driver and operator inputs using CommandXboxController.
  • Apply reactive trigger bindings (whileTrue, onTrue, toggleOnTrue).
  • Construct sensor-based Triggers that react to autonomous conditions.

Prerequisites:Command Lifecycle & Subsystem Requirements

In legacy FRC programming, teams often wrote large if / else statements inside teleopPeriodic() to check controller buttons every cycle. This led to complex state tracking and bugs where multiple buttons commanded conflicting motor voltages.

Modern WPILib uses Event-Driven Triggers. You declare relationships between input events and commands once during startup, and the CommandScheduler handles scheduling and cancellation automatically.


WPILib provides the CommandXboxController class, which wraps physical controller buttons in reactive Trigger objects:

package frc.robot;
import edu.wpi.first.wpilibj2.command.button.CommandXboxController;
public class RobotContainer {
// Port 0 is typically the driver; Port 1 is the mechanism operator
private final CommandXboxController m_driver = new CommandXboxController(0);
private final CommandXboxController m_operator = new CommandXboxController(1);
private void configureBindings() {
// Triggers are bound here during robot startup
}
}

A Trigger wraps a boolean condition evaluated every 20ms. Bind commands to triggers using four standard methods:

Binding MethodBehaviorTypical Mechanism Use Case
.onTrue(Command)Schedules command on rising edge (0 -> 1). Command runs until its own isFinished() returns true.Score preset, zero gyro heading, fire note.
.whileTrue(Command)Runs command while condition is true. Cancels command and calls end(true) when released.Run intake rollers while holding bumper, manual arm jog.
.onFalse(Command)Schedules command on falling edge (1 -> 0).Stowing mechanism when a button is released.
.toggleOnTrue(Command)Starts command on first press; cancels it on second press.Toggling intake deploy/stow or limelight LED mode.
// Hold right bumper to run intake; release to stop
m_operator.rightBumper().whileTrue(m_intake.intakeCommand());
// Tap 'A' button to move elevator to low scoring preset
m_operator.a().onTrue(m_elevator.moveToHeightCommand(Inches.of(15.0)));
// Tap 'Y' button to move elevator to high scoring preset
m_operator.y().onTrue(m_elevator.moveToHeightCommand(Inches.of(42.0)));

Triggers are not limited to physical gamepad buttons. Any Java BooleanSupplier can become a reactive trigger:

Trigger an automated indexing routine the instant a game piece breaks an optical sensor:

Trigger gamePieceDetected = new Trigger(m_intake::isSensorTripped);
// When piece enters intake, automatically run feeder to index it into shooter
gamePieceDetected.onTrue(m_indexer.indexToShooterCommand());

Detect when a roller intake has captured a game piece by monitoring motor load:

Trigger rollerStall = new Trigger(() ->
m_intakeMotor.getStatorCurrent().getValueAsDouble() > 35.0
);
// Stop intake and rumble operator controller when piece is clamped
rollerStall.onTrue(
Commands.runOnce(() -> m_operator.getHID().setRumble(RumbleType.kBothRumble, 1.0))
);

Convert analog joystick triggers or axes into digital triggers with a deadband threshold:

// Trigger fires when left analog trigger is pulled past 50%
Trigger leftTriggerPulled = m_driver.leftTrigger(0.5);
leftTriggerPulled.whileTrue(m_shooter.spinUpCommand());

  • Symptom: RoboRIO CPU utilization spikes, garbage collection pauses loop cycles.
  • Cause: Creating new Command() instances repeatedly inside an execution loop instead of binding once in configureBindings().
  • Fix: Declare triggers and bind commands once inside RobotContainer.configureBindings().
  • Symptom: Pressing gamepad buttons in teleop has no effect.
  • Cause: The gamepad index in Driver Station does not match the port number in code (new CommandXboxController(0)).
  • Fix: Verify in the Driver Station USB tab that your gamepad is dragged into slot 0.

  1. In desktop simulation (SimGUI), open the Joysticks view.
  2. Click button 1 (‘A’) on the virtual joystick.
  3. Verify in the NetworkTables tree that the bound command transitions to scheduled and updates its subsystem.

Verified Sources & Standards

Evergreen Standard Verified: Sep 25, 2026