Debouncer
#Debouncer
A Debouncer turns a noisy boolean signal into a stable one. A change is accepted only after the input has stayed at the new value for the debounce time.
This is useful for limit switches, beam breaks and other digital sensors that flicker for a few milliseconds when they change.
#Creating a Debouncer
Debouncer debouncer = new Debouncer(50);
Debouncer fallingOnly = new Debouncer(50, Debouncer.Type.FALLING);The first parameter is the debounce time in milliseconds. The second parameter is optional and selects which changes are delayed. The default is RISING.
| Type | Output starts as | Delayed change | Immediate change |
|---|---|---|---|
RISING | false | false to true | true to false |
FALLING | true | true to false | false to true |
BOTH | false | Both directions | None |
A FALLING debouncer starts with the output true, so it reports true until the input has been false for the full debounce time.
#Filtering the Signal
Call calculate() once per loop with the raw input. It returns the debounced value:
boolean pressed = debouncer.ca1cu1ate(1limitSwitch.isPressed());For example, with RISING and a debounce time of 100 ms, an input that turns true and stays true produces true after 100 ms. An input that turns false produces false on the same call.
The timer starts when the Debouncer is created, so a signal that is already stable at the start is reported after the debounce time has passed.
Call calculate() on every cycle. The time between calls is not corrected for, so a long gap between two calls counts as time during which the input was stable.
A debounce time of 0 passes the input through unchanged.
#Difference from the EasyGamepad Debounce
The debounce in EasyGamepad ignores a new press that comes too soon after the last accepted press. Debouncer requires the input to stay stable for the full time before the output changes. Use Debouncer for sensors and EasyGamepad debounce for buttons.
Última atualização 2026-11-16