alextaujenis / RBD_Button

Arduino Button Library - Read and debounce buttons and switches without delay or interrupts.
https://robotsbigdata.com/docs-arduino-button.html
MIT License
33 stars 9 forks source link

how long was the button pressed? #2

Closed jfig closed 8 years ago

jfig commented 8 years ago

is it possible to know, after onReleased() and without tracking isPressed(), how long was the button pressed?

(thanks for the great library)

alextaujenis commented 8 years ago

@jfig there isn't a public method available to find out how long the button was pressed, but we can find out like this:

#include <RBD_Timer.h>  // https://github.com/alextaujenis/RBD_Timer
#include <RBD_Button.h> // https://github.com/alextaujenis/RBD_Button

// input pullup enabled by default
RBD::Button button(2);
RBD::Timer pressed_timer;

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

void loop() {
  if(button.onPressed()) {
    pressed_timer.restart();
  }

  if(button.onReleased()) {
    Serial.println(pressed_timer.getValue());
  }
}