//TEST1_button_long_short_V1 - S og L fungerer ved 1 eller 2 og LL.
// Mangler SS
#include <ezButton.h>
#define SHORT_PRESS_TIME 1500 //
#define LONG_PRESS_TIME 1500 //
#define BTN_RED 3
#define BTN_GREEN 2
ezButton button1(BTN_RED);
ezButton button2(BTN_GREEN);
unsigned long pressedTime = 0;
unsigned long releasedTime = 0;
bool buttonIsPressed = false;
bool button1Pressed = false;
bool button2Pressed = false;
bool isLongDetected = false;
bool buttonIsReleased = false;
long pressDuration = 0;
void setup() {
pinMode(BTN_RED, INPUT_PULLUP);
pinMode(BTN_GREEN, INPUT_PULLUP);
Serial.begin(9600);
button1.setDebounceTime(100); //ændret 75 til 100
button2.setDebounceTime(100);
Serial.println("Running .....");
}
void loop() {
button1.loop();
button2.loop();
////////////////////////////////////
if (button1.isPressed()) {
Serial.println("Button 18 is pressed");
if (!button2Pressed) {
pressedTime = millis();
}
button1Pressed = true;
isLongDetected = false;
buttonIsPressed = true;
} // isPressed
if (button1.isReleased()) {
button1Pressed = false;
if (!button2Pressed) {
releasedTime = millis();
pressDuration = releasedTime - pressedTime;
if (pressDuration < SHORT_PRESS_TIME)
Serial.println("S - ");
}
// buttonIsReleased = true;
} // 1isReleased
////////////////////////////////////
////////////////////////////////////
if (button2.isPressed()) {
if (!button1Pressed) {
pressedTime = millis();
}
pressedTime = millis();
button2Pressed = true;
isLongDetected = false;
buttonIsPressed = true;
} // isPressed
if (button2.isReleased()) {
button2Pressed = false;
if (!button1Pressed) {
releasedTime = millis();
pressDuration = releasedTime - pressedTime;
if (pressDuration < SHORT_PRESS_TIME)
Serial.println(" - S ");
}
// buttonIsReleased = true;
} // 2isReleased
////////////////////////////////////
////////////////////////////////////
if (buttonIsPressed && isLongDetected == false) {
pressDuration = millis() - pressedTime;
/*
if (pressDuration < LONG_PRESS_TIME) {
if ( !button1.isReleased() && !button2.isReleased()) {
Serial.println("S S");
}
}
*/
if (pressDuration > LONG_PRESS_TIME) {
if (button1Pressed && button2Pressed) {
Serial.println("L L ");
} else if (button1Pressed && !button2Pressed) {
Serial.println("L -");
} else if (!button1Pressed && button2Pressed) {
Serial.println("- L");
}
isLongDetected = true;
}
} // isPressing
}