Skip to content

Support for repeated/long press detection #8

@urish

Description

@urish

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions