Hi guys,
Trying to get more functionality from a simple two button controller. So far the individual button long and short press functions work well.
However after when an individual long press is released (on either button) i'd like to serial print 'normal'.
And for some reason the simultaneous short press is triggered straight away without chance of a the simultaneous long press being triggered and after it's been triggered, no other button presses work.
If anyone has any help it would be greatly appreciated as i'm struggling through this project. Thanks!
#include <ezButton.h>
const int SHORT_PRESS_TIME = 1000; // 1000 milliseconds
const int LONG_PRESS_TIME = 1000; // 1000 milliseconds
ezButton button1(2); // create ezButton object that attaches to pin 2
ezButton button2(3); // create ezButton object that attaches to pin 3
unsigned long pressedTime1 = 0;
unsigned long releasedTime1 = 0;
bool isPressing1 = false;
bool isLongDetected1 = false;
unsigned long pressedTime2 = 0;
unsigned long releasedTime2 = 0;
bool isPressing2 = false;
bool isLongDetected2 = false;
void setup() {
Serial.begin(9600);
button1.setDebounceTime(50); // set debounce time to 50 milliseconds for button1
button2.setDebounceTime(50); // set debounce time to 50 milliseconds for button2
}
void loop() {
button1.loop(); // MUST call the loop() function for button1 first
button2.loop(); // MUST call the loop() function for button2 first
static bool simultaneousShortPress = false;
static bool simultaneousLongPress = false;
// Individual button 1 handling
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 && !simultaneousShortPress && !simultaneousLongPress) {
Serial.println("Up");
delay(200);
}
}
if (isPressing1 && !isLongDetected1 && !simultaneousShortPress && !simultaneousLongPress) {
long pressDuration1 = millis() - pressedTime1;
if (pressDuration1 > LONG_PRESS_TIME) {
Serial.println("Double");
isLongDetected1 = true;
}
}
// Individual button 2 handling
if (button2.isPressed()) {
pressedTime2 = millis();
isPressing2 = true;
isLongDetected2 = false;
}
if (button2.isReleased()) {
isPressing2 = false;
releasedTime2 = millis();
long pressDuration2 = releasedTime2 - pressedTime2;
if (pressDuration2 < SHORT_PRESS_TIME && !simultaneousShortPress && !simultaneousLongPress) {
Serial.println("Down");
delay(200);
}
}
if (isPressing2 && !isLongDetected2 && !simultaneousShortPress && !simultaneousLongPress) {
long pressDuration2 = millis() - pressedTime2;
if (pressDuration2 > LONG_PRESS_TIME) {
Serial.println("Half");
isLongDetected2 = true;
}
}
// Simultaneous press handling
if (isPressing1 && isPressing2 && !simultaneousShortPress && !simultaneousLongPress) {
long pressDuration1 = millis() - pressedTime1;
long pressDuration2 = millis() - pressedTime2;
if (pressDuration1 < SHORT_PRESS_TIME && pressDuration2 < SHORT_PRESS_TIME) {
Serial.println("Looper");
simultaneousShortPress = true;
delay(200); // This delay may not be necessary, you may remove it if it interferes with your logic
}
if (pressDuration1 > LONG_PRESS_TIME && pressDuration2 > LONG_PRESS_TIME) {
Serial.println("Kill dry");
simultaneousLongPress = true;
}
}
// Reset simultaneous press flags and other flags
if (button1.isReleased() && button2.isReleased()) {
simultaneousShortPress = false;
simultaneousLongPress = false;
isLongDetected1 = false;
isLongDetected2 = false;
}
}