How do you plan to solve it?
const int buttonPin = 2;
unsigned long lastDebounceTime = 0;
const unsigned long debounceDelay = 50; // ms
unsigned long lastPressTime = 0;
const unsigned long doubleClickDelay = 400;
const unsigned long longPressTime = 800;
int buttonState = HIGH;
int lastReading = HIGH;
unsigned long pressStartTime = 0;
int clickCount = 0;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
Serial.begin(115200);
}
void loop() {
int reading = digitalRead(buttonPin);
if (reading != lastReading) {
lastDebounceTime = millis();
}
if (millis() - lastDebounceTime > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == LOW) {
pressStartTime = millis();
} else {
unsigned long pressDuration = millis() - pressStartTime;
if (pressDuration >= longPressTime) {
Serial.println("Long press detected");
clickCount = 0;
} else {
clickCount++;
lastPressTime = millis();
}
}
}
}
if (clickCount == 1 && (millis() - lastPressTime > doubleClickDelay)) {
Serial.println("Single click detected");
clickCount = 0;
} else if (clickCount == 2) {
Serial.println("Double click detected");
clickCount = 0;
}
lastReading = reading;
}
Add a video of the output (know more)
https://wokwi.com/projects/442533992111735809