Usage - PaulMurrayCbr/DebounceInput GitHub Wiki

To use DebouncedInput, create an instance on an input pin, eg

DebouncedInput button(4);

Or create an instance and then attach it to a pin in your setup

DebouncedInput button;

void setup() {
	pinMode(4, INPUT); // we have an external pullup resistor
	button.attach(4);
}        

Then in your loop, make a call to read() to read the pin. This should be done frequently. The object will sample the input no more than once every 4ms, so provided you are calling read() more frequently than that, it should debounce correctly.

void loop() {
    button.read();
    // rest of your sketch goes here
}

You can look at the debounced state of the pin like so:

if(button.high()) { }

Or any of these methods:

  • high()
  • low()
  • changing()
  • rising()
  • falling()

For convenience, these method combine a read() with a check for a change of state.

  • readChanging()
  • readRising()
  • readFalling()

the read() method itself also returns the state of the pin, same as high().

The changing, rising, and falling methods return true if the debounced pin state changed as a result of the most resent read. So do not do this:

// Bad! Don't do this!

if(button.readRising()) {
}
else if(button.readFalling()) {
}

If you need to detect either case, then use something like this:

button.read();

if(button.rising()) {
}
else if(button.falling()) {
}

For more detailed information one use and tuning, see DebouncedInput, DebounceFilter, DebounceFilter4ms and Algorithm. In particular, to bypass the 4ms sample rate limiting, use DebounceFilter directly.