This is a debouncing button library for Arduino.
- 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)
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
nothing
TTBOUNCE switch = TTBOUNCE(5);
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() and attachReTick()
Attaches a custom callback method.
function: method reference
nothing
switch.attachClick(click);
switch.attachDoubleClick(doubleClick);
switch.attachReTick(reTick);
void click(){
digitalWrite(13, HIGH);
}
void doubleClick(){
//do something here
}
void reTick(){
//do something here
}
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();
}
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
}