Debouncer
Easydifficulty
A low-pass filter answers "what number should I believe?" A debouncer answers "did this actually happen?" It's a few lines of code and it stops the robot from reacting to one-loop glitches.
One press can look like many
A mechanical switch doesn't close cleanly. Its contacts bounce between open and closed for a few milliseconds. Optical and magnetic sensors flicker too, from electrical noise, a shiny surface, or a part sitting right at the edge of their range. If your intake changes state on every raw change, one game piece can look like three.
Require an uninterrupted dwell time
Keep two values: the raw input and the debounced output. When the raw input changes to something different from the output, start a timer. Only copy the new value to the output if the input stays that way for the whole debounce time . Any bounce back resets the timer.
So the output lags the input by up to , on purpose, and it never changes on a pulse shorter than .
| Raw input | Has it stayed changed for ? | Output |
|---|---|---|
false → true for 8 ms, then false | No | stays false |
false → true for 50 ms with ms | Yes | becomes true after 30 ms |
already true | n/a | stays true |
Choose which transition needs proof
WPILib defines three modes, named for which change of the output has to wait:
| Mode | Waits before | Changes immediately | FTC example |
|---|---|---|---|
| Rising | false → true | true → false | confirm a beam break really sees a game piece |
| Falling | true → false | false → true | confirm a released limit switch is truly released |
| Both | either transition | neither | make the displayed state stable in both directions |
Debounce the change where a false alarm is expensive. For a beam break, rising-only is often right: wait before deciding a piece is there, but clear it as soon as it leaves. Only debounce both ways if the extra delay in both directions is safe.
The output is a steady true or false. To run an action once, compare this loop's output with last
loop's and act on the change. Don't start the action every loop while the output stays true.
A time-based FTC implementation
Pass in a timestamp in seconds every loop. The constructor takes the sensor's current reading so the robot doesn't see a fake change at startup. Because it's based on time rather than loop counts, 30 ms means 30 ms even when the loop slows down.
public final class BooleanDebouncer {
public enum Mode { RISING, FALLING, BOTH }
private final double debounceSeconds;
private final Mode mode;
private boolean output;
private boolean candidate;
private double candidateSinceSeconds;
public BooleanDebouncer(boolean initialValue, double debounceSeconds, Mode mode) {
if (debounceSeconds < 0.0) {
throw new IllegalArgumentException("debounceSeconds must be nonnegative");
}
this.output = initialValue;
this.candidate = initialValue;
this.debounceSeconds = debounceSeconds;
this.mode = mode;
}
public boolean update(boolean input, double nowSeconds) {
if (input == output) {
// The candidate disappeared before it was accepted: cancel its timer.
candidate = output;
candidateSinceSeconds = nowSeconds;
return output;
}
if (input != candidate) {
candidate = input;
candidateSinceSeconds = nowSeconds;
}
if (!shouldWait(output, candidate)
|| nowSeconds - candidateSinceSeconds >= debounceSeconds) {
output = candidate;
candidateSinceSeconds = nowSeconds;
}
return output;
}
private boolean shouldWait(boolean from, boolean to) {
switch (mode) {
case RISING: return !from && to;
case FALLING: return from && !to;
default: return true; // BOTH
}
}
}
Use one debouncer per signal, create it during init, and call update once per loop. This beam break
reads low when blocked, so !beamBreak.getState() means "a piece is present."
private final ElapsedTime runtime = new ElapsedTime();
private DigitalChannel beamBreak;
private BooleanDebouncer pieceDebouncer;
private boolean hadPiece;
public void init() {
beamBreak = hardwareMap.get(DigitalChannel.class, "beamBreak");
beamBreak.setMode(DigitalChannel.Mode.INPUT);
boolean piecePresent = !beamBreak.getState(); // invert only if your wiring is active-low
pieceDebouncer = new BooleanDebouncer(
piecePresent, 0.030, BooleanDebouncer.Mode.RISING);
hadPiece = piecePresent;
}
public void loop() {
boolean rawPiecePresent = !beamBreak.getState();
boolean piecePresent = pieceDebouncer.update(rawPiecePresent, runtime.seconds());
if (piecePresent && !hadPiece) {
advanceIntakeState(); // runs once, after 30 ms of continuous detection
}
hadPiece = piecePresent;
}
You log the raw input while triggering the sensor over and over. The longest false pulse is 12 ms, and
real detections stay high for at least 80 ms. What debounce time do you start with, and what does it
cost you? Around 20 to 30 ms. That's longer than the 12 ms glitch, so the glitch can't get through, and it still
leaves 50 ms of margin before the shortest real detection ends. The cost is that the robot reacts 20 to
30 ms later than it could. Test it on your mechanism instead of copying a number that worked somewhere
else.Show solution
Don't use it to hide a wiring bug
Debouncing removes short flickers. It can't fix a loose connector, inverted logic, or a sensor that
never settles. Put the raw value on telemetry first and check which physical situation reads true.
Then pick the delay from the glitches you actually recorded. For safety limit switches, make sure the
safe direction reacts fast enough; a long delay in both directions can be the wrong choice.
- WPILib: Debouncer - the source for the rising, falling, and both-edge definitions used here.
- Signals & Noise - identify whether a digital input is bouncing, electrically noisy, or being read with the wrong polarity before adding a filter.
- Low-Pass Filter - use a numeric smoother for continuously valued sensor jitter; use this lesson's debouncer for a boolean event.