Arduino Playground is read-only starting December 31st, 2018. For more info please look at this Forum Post

MomentaryButton

Encapsulate Sensing Taps and Holds on A Momentary Button

This library encapsulates tracking a momentary button's pressed state, reporting whether it was tapped (pressed and released quickly) or held (closed for longer than some threshold).

Example:

// One side of the button is attached to pin 2, the other to ground.
#define PIN_BUTTON 2
#include "MomentaryButton.h"
MomentaryButton button(PIN_BUTTON);
void setup() {
  button.setup(); // set as INPUT, set HIGH
}
void loop() {
  button.check();
  if (button.wasClicked()) {
    // Respond to the button being briefly closed, then released.
  } else if (button.wasHeld()) {
    // Respond to the button being closed for longer.
  }
}

To install, add the .cpp and .h files to your project, and #include "MomentaryButton.h". (The files are not presented as an Arduino library, though a github fork to do so is welcome.)

See also: TButton