How do you plan to solve it?
// Define the push button pin
const int buttonPin = 2;
// Variables for button state tracking
int buttonState;
int lastButtonState = LOW;
// Timestamps for timing events
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
unsigned long lastClickTime = 0;
unsigned long longPressTime = 1000;
unsigned long doubleClickDelay = 250;
// Variables for click counting and long press detection
int clickCount = 0;
bool longPressTriggered = false;
void setup() {
// Set the button pin as an input with the internal pull-up resistor enabled
pinMode(buttonPin, INPUT_PULLUP);
// Initialize serial communication
Serial.begin(9600);
}
void loop() {
// Read the current state of the button
int reading = digitalRead(buttonPin);
// Debounce the button input
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
// Detect button press
if (buttonState == LOW) {
clickCount++;
lastClickTime = millis();
longPressTriggered = false;
}
}
}
// Handle button release to check for clicks
if (buttonState == HIGH && !longPressTriggered) {
if (clickCount > 0 && (millis() - lastClickTime) > doubleClickDelay) {
if (clickCount == 1) {
Serial.println("Single Click!");
} else if (clickCount == 2) {
Serial.println("Double Click!");
}
clickCount = 0;
}
}
// Handle long press detection
if (buttonState == LOW && (millis() - lastClickTime) > longPressTime && !longPressTriggered) {
Serial.println("Long Press!");
longPressTriggered = true;
clickCount = 0;
}
// Update last button state for the next loop iteration
lastButtonState = reading;
}
Add a video of the output (know more)