This is a debouncing button library for Arduino providing click, double click, long press, retick and release events.
- 2014-01-23 Initial release
- 2014-02-25 added click/doubleclick/press and getHoldTime()
- 2018-07-26 added attachReTick() for continous button press (usable for counting up and down)
- 2018-10-19 added attachRelease() for notification when button is released (Thanks to DaveDischord)
- 2020-01-02 Can now be used without hardware pin. Write the "virtual" pin state by calling the update(pinState) method and let the library do the rest. This comes handy if you're using sensors (e.g. 3D magnet sensors like the TLE493D)
- 2022-02-17 added getLastDetectedEvent() and enable/disableDoubleClickEvent() for polling events (if event methods (e.g. attachClick) are not used)
- 2022-02-18 fixed a bug in the double click logik
Download the ZIP file and extract it's content. Put the TTBOUNCE folder in "ARDUINOAPP/hardware/libraries/". In the Arduino IDE you can test the sample sketches under Samples->TTBOUNCE.
pin: the number of the pin which is attached to the button/switch
If using in virtual mode without hardware pin: TTBOUNCE_WITHOUT_PIN
nothing
TTBOUNCE switch = TTBOUNCE(5);
Or without using hardware pin:
TTBOUNCE switch = TTBOUNCE(TTBOUNCE_WITHOUT_PIN);
When a switch is connected between a digital pin and VDD (5V) and the pin is pulled down over a resistor, the digital pin will be HIGH when the switch is pressed. You can activate this behaviour by calling the setActiveHigh(). ActiveHigh is the default operation mode.
read() will return HIGH if switch is pressed and LOW if switch is released, no matter how the switch is attached to the digital pin.
none
nothing
switch.setActiveHigh();
When a switch is connected between a digital pin and GND and the pin is pulled up over a resistor, the digital pin will be LOW when the switch is pressed. You can activate this behaviour by calling the setActiveLow().
read() will return HIGH if switch is pressed and LOW if switch is released, no matter how the switch is attached to the digital pin.
none
nothing
switch.setActiveLow();
Sets the time in which the debouncing happens. Default: 10ms
Take a higher interval if there is digital noise on the pin.
interval: Debouncing time in ms
nothing
switch.setDebounceInterval(50);
Sets the time window in which a click can happen. Default: 300ms
interval: time in ms
nothing
switch.setClickInterval(200);
Sets the time after which a long press is detected. Default: 1000ms
interval: time in ms
nothing
switch.setPressInterval(500);
Sets the time after which a retick is fired as long as the button is pressed. Default: 200ms
interval: time in ms
nothing
switch.setReTickInterval(100);
attachClick(callbackFunction function) | also applies to attachDoubleClick(), attachPress(), attachReTick() and attachRelease()
Attaches a custom callback method.
function: method reference
nothing
switch.attachClick(click);
switch.attachDoubleClick(doubleClick);
switch.attachReTick(reTick);
switch.attachRelease(release);
void click(){
digitalWrite(13, HIGH);
}
void doubleClick(){
//do something here
}
void reTick(){
//do something here
}
void release(){
//do something here
}
By default the double click detection is disabled. The reason why is that when enabled the single click gets delayed because a double click needs some time to be detected.
This methods are only needed when event pulling is used. Double click detection are automatically enabled when ttachDoubleClick()
got called.
none
nothing
Activates the internal pull up resistor on the digital pin.
none
nothing
switch.enablePullup();
Turns the internal pull up resistor off.
none
nothing
switch.disablePullup();
Be sure to call this method over and over again. Place it somewhere in your loop routine. This will update the debouncing method and also the switch state.
none
nothing
void loop(){
switch.update();
}
with external "virtual" pin state:
void loop(){
uint8_t pinState = readFromSensor();
switch.update(pinState);
}
This method will return the current switch status. HIGH if switch is pressed, LOW if switch is released. Ensure to call the setActive... methods accordingly.
none
HIGH = switch pressed LOW = switch released
if(switch.read() == HIGH){
//turn led on
}
This method will return the time in milliseconds since beginning of HIGH state.
none
unsigned long (time in milliseconds)
if(switch.getHoldTime() > 2000){
//turn led on
}
This method will return the last seen event (click, double click or press). After calling this function the last detected event is reset to NONE
, so a detected event can only be read once.
Ensure to call enableDoubleClickEvent()
when double clicks should also be detected.
none
event_type_t (NONE
, CLICK
, DOUBLE_CLICK
or PRESS
)
if(switch.getLastDetectedEvent == CLICK){
Serial.print("a click was detected");
}