Skip to content

A digital pin debouncing library for Arduino

License

Notifications You must be signed in to change notification settings

TOLDOTECHNIK/TTBOUNCE

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TTBOUNCE

This is a debouncing button library for Arduino providing click, double click, long press, retick and release events.

History

  • 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

Installation

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.

Usage

Create TTBOUNCE object

Parameters

pin: the number of the pin which is attached to the button/switch

If using in virtual mode without hardware pin: TTBOUNCE_WITHOUT_PIN

Returns

nothing

Example

TTBOUNCE switch = TTBOUNCE(5);

Or without using hardware pin:

TTBOUNCE switch = TTBOUNCE(TTBOUNCE_WITHOUT_PIN);

setActiveHigh()

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.

Parameters

none

Returns

nothing

Example

switch.setActiveHigh();

setActiveLow()

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.

Parameters

none

Returns

nothing

Example

switch.setActiveLow();

setDebounceInterval(unsigned int interval)

Sets the time in which the debouncing happens. Default: 10ms

Take a higher interval if there is digital noise on the pin.

Parameters

interval: Debouncing time in ms

Returns

nothing

Example

switch.setDebounceInterval(50);

setClickInterval(unsigned int interval)

Sets the time window in which a click can happen. Default: 300ms

Parameters

interval: time in ms

Returns

nothing

Example

switch.setClickInterval(200);

setPressInterval(unsigned int interval)

Sets the time after which a long press is detected. Default: 1000ms

Parameters

interval: time in ms

Returns

nothing

Example

switch.setPressInterval(500);

setReTickInterval(unsigned int interval)

Sets the time after which a retick is fired as long as the button is pressed. Default: 200ms

Parameters

interval: time in ms

Returns

nothing

Example

switch.setReTickInterval(100);

attachClick(callbackFunction function) | also applies to attachDoubleClick(), attachPress(), attachReTick() and attachRelease()

Attaches a custom callback method.

Parameters

function: method reference

Returns

nothing

Example

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
}

enableDoubleClickEvent() / disableDoubleClickEvent

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.

Parameters

none

Returns

nothing

enablePullup()

Activates the internal pull up resistor on the digital pin.

Parameters

none

Returns

nothing

Example

switch.enablePullup();

disablePullup()

Turns the internal pull up resistor off.

Parameters

none

Returns

nothing

Example

switch.disablePullup();

update()

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.

Parameters

none

Returns

nothing

Example

void loop(){
	switch.update();
}

with external "virtual" pin state:

void loop(){
	uint8_t pinState = readFromSensor();
	switch.update(pinState);
}

read()

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.

Parameters

none

Returns

HIGH = switch pressed LOW = switch released

Example

if(switch.read() == HIGH){	
	//turn led on
}

getHoldTime()

This method will return the time in milliseconds since beginning of HIGH state.

Parameters

none

Returns

unsigned long (time in milliseconds)

Example

if(switch.getHoldTime() > 2000){	
	//turn led on
}

getLastDetectedEvent()

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.

Parameters

none

Returns

event_type_t (NONE, CLICK, DOUBLE_CLICK or PRESS)

Example

if(switch.getLastDetectedEvent == CLICK){	
	Serial.print("a click was detected");
}

About

A digital pin debouncing library for Arduino

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages