Button icon indicating copy to clipboard operation
Button copied to clipboard

Support for repeated/long press detection

Open urish opened this issue 4 years ago • 0 comments

Hi, Thanks for maintaining this library! I want to use it in an alarm clock example I'm building for GoodArduinoCode, and for that, I'd need an option to detect long presses and generate repeated events as long as the button is held down.

I imagine something like:

#include <Button.h>

Button hourButton = Button(A0);

void setup() {
  Serial.begin(115200);
  hourButton.begin();

  // After 500ms delay, generate repeated press every 200ms:
  hourButton.setRepeat(500, 200);
}

void loop() {
  if (hourButton.pressed()) {
    Serial.print("Pressed! Repeat count");
    Serial.println(hourButton.repeatCount());
  }
  if (hourButton.release()) {
     Serial.println("Released!");
  }
}

So if you press the button, hold it down for 1 second, and release, you'll see something like:

[0000] Pressed! Repeat count: 0
[0500] Pressed! Repeat count: 1
[0700] Pressed! Repeat count: 2
[0900] Pressed! Repeat count: 3
[1000] Release

(the numbers in the square brackets are just the milliseconds since the button was pressed).

If you are interested, I can also send a PullRequest which adds this functionality, simply let me know.

urish avatar Jul 25 '20 08:07 urish