/*
Created by ArduinoGetStarted.com
This example code is in the public domain
Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-long-press-short-press
*/
#include <ezButton.h>
const int SHORT_PRESS_TIME = 2000; // 1000 milliseconds
const int LONG_PRESS_TIME = 4000; // 1000 milliseconds
ezButton button(2); // create ezButton object that attach to pin 2;
ezButton button1(19);
unsigned long pressedTime = 0;
unsigned long releasedTime = 0;
bool isPressing = false;
bool isLongDetected = false;
unsigned long pressedTime1 = 0;
unsigned long releasedTime1 = 0;
bool isPressing1 = false;
bool isLongDetected1 = false;
void setup() {
Serial.begin(9600);
button.setDebounceTime(50); // set debounce time to 50 milliseconds
button1.setDebounceTime(50);
}
void loop() {
button.loop(); // MUST call the loop() function first
button1.loop();
/// button
if (button.isPressed()) {
pressedTime = millis();
isPressing = true;
isLongDetected = false;
}
if (button.isReleased()) {
isPressing = false;
releasedTime = millis();
long pressDuration = releasedTime - pressedTime;
if ( pressDuration < SHORT_PRESS_TIME )
Serial.println("A short press is detected");
}
if (isPressing == true && isLongDetected == false) {
long pressDuration = millis() - pressedTime;
if ( pressDuration > LONG_PRESS_TIME ) {
Serial.println("A long press is detected");
isLongDetected = true;
}
} ///end button
/// button1
if (button1.isPressed()) {
pressedTime1 = millis();
isPressing1 = true;
isLongDetected1 = false;
}
if (button1.isReleased()) {
isPressing1 = false;
releasedTime1 = millis();
long pressDuration1 = releasedTime1 - pressedTime1;
if ( pressDuration1 < SHORT_PRESS_TIME )
Serial.println("A short press yel is detected");
}
if (isPressing1 == true && isLongDetected1 == false) {
long pressDuration1 = millis() - pressedTime1;
if ( pressDuration1 > LONG_PRESS_TIME ) {
Serial.println("A long press yel is detected");
isLongDetected1 = true;
}
}
/// end button1
}