From button to action in command-based WPILib#
In modern WPILib, you bind controls to robot behavior declaratively using Trigger objects, typically in RobotContainer. The recommended class is CommandXboxController. You create it on a port (matching the USB order), then bind its buttons to commands:
// Driver controller on port 0
CommandXboxController driver = new CommandXboxController(0);
// Run the intake while the right bumper is held
driver.rightBumper().whileTrue(intake.runIntakeCommand());
// Score once when A is pressed
driver.a().onTrue(scoreCommand());
The library automatically polls these triggers and schedules the right command — you declare the mapping once at startup.
The binding methods#
Per the WPILib Binding Commands to Triggers docs, each Trigger offers these methods:
onTrue(command)— schedules the command once when the trigger goes fromfalsetotrue; it won't run again until the trigger goes false and true again. Good for discrete actions ("shoot," "go to a position").onFalseis the same on release.whileTrue(command)— schedules the command when the trigger becomestrueand cancels it when the trigger becomesfalse. Good for "run intake while I hold this" or "hold to aim."whileFalseis the mirror.toggleOnTrue(command)— toggles the command on with one press and off with the next. The docs explicitly say toggles are not a highly-recommended option for user control because they require the driver to track robot state; the preferred approach is two buttons (one to turn on, one to turn off).
You can also use the POV/d-pad (driver.povUp()), treat an analog trigger or stick axis as a button with a threshold (e.g., driver.rightTrigger(0.5)), and compose triggers with .and() / .or() and .debounce() to filter noise.
Designing the control scheme#
Good mappings are designed for humans under stress, not for code convenience:
- Split by role: drivetrain on the driver's thumbsticks; mechanisms on the operator's controller or button board.
- Group related actions physically (all scoring buttons together) and keep dangerous actions (like a climber release) guarded — require a second button, or place it where it can't be hit accidentally.
- Prefer hold-to-run for transient actions (
whileTrue) so releasing instantly stops the mechanism — a built-in safety. - Keep it consistent across the season; rebinding the week before champs forces the driver to relearn muscle memory.
Find your button numbers fast#
When a control isn't doing what you expect, the USB Devices tab shows live button and axis values so you can press a physical button and see exactly which index lights up. Some teams write a tiny "joystick investigator" that prints button/axis values to the dashboard for the same purpose. Always verify the physical control matches the code mapping before you trust it in a match.
the part worth keeping
Key takeaways
- Bind controls declaratively with CommandXboxController and Trigger objects in RobotContainer.
- Use onTrue/onFalse for one-shot actions, whileTrue/whileFalse for hold-to-run, and prefer two buttons over toggleOnTrue for critical actions.
- Design mappings around the human: split driver/operator, group actions, guard dangerous ones.
- Use the USB Devices tab's live feedback to confirm physical controls match code indices.
Drive TeamDriver Station and Controlslesson 3 of 4
Keep going
Take the quiz+10 XP with an accountMore in Driver Station and Controls
where this came from
Sources and corrections
This lesson is AI-assisted: drafted from primary sources, then reviewed and edited by hand. Errors still get through. When one is reported we fix it and write down what changed, in public, in the corrections log.
sources and further reading
- docs.wpilib.orgWPILib Docs — Binding Commands to Triggers
- github.wpilib.orgWPILib API — CommandXboxController
clipped to this lesson
Articles that go further on this
The lesson gets you through the topic. These go wider on it, and they read in one sitting.
- 13 min readFRC Code Structure Best Practices: Command-Based Project ArchitectureHow to structure an FRC command-based robot project the right way — subsystems, commands, RobotContainer, and Constants — verified against official WPILib docs./blogread it
- 9 min readFRC Command-Based Programming: Subsystems, Commands, and the SchedulerA beginner-friendly guide to WPILib command-based programming in Java: subsystems, commands, the CommandScheduler, triggers, and composing commands./blogread it
- 7 min readFRC "No Robot Code" & Driver Station Won't Connect: Full Fix ChecklistA verified, step-by-step FRC troubleshooting decision tree for "No Robot Code" and a Driver Station that won't connect to the roboRIO — most common causes first./blogread it
answer sheet
Lesson quiz
All 3 right completes the lesson. Miss one and only that question comes back, anything you already answered correctly stays banked.
0 of 3 answered
01When you bind a command to a Trigger with whileTrue(command), what happens?
02For a critical action, what control scheme is recommended over using toggleOnTrue?
03How can you confirm that a physical control matches your code's button index?
Answer every question to submit.
All 34 lessons in Drive Teamopenclose
01 / prerequisites
02 / the-drive-team-and-its-roles
03 / driver-station-and-controls
04 / driver-practice-and-match-performance
05 / the-pit-and-match-turnaround
06 / worked-examples-mini-projects
- Not read yet:Project 1: A Production-Ready Driver Control Scheme
- Not read yet:Project 2: A Drivetrain That Survives a Whole Match
- Not read yet:Project 3: Build a Paper + Spreadsheet Scouting System
- Not read yet:Project 4: Pull Live OPR & EPA Data with Python
- Not read yet:Project 5: The One-Page Pre-Match Strategy Brief
07 / common-mistakes-troubleshooting
08 / advanced-strategy-case-studies