How do you plan to solve it?
const int buttonPin = 2;
unsigned long lastDebounceTime = 0;
unsigned long lastClickTime = 0;
unsigned long buttonPressStartTime = 0;
int clickCount = 0;
int buttonState;
int lastButtonState = HIGH;
const long debounceDelay = 50;
const long doubleClickTimeOut = 300;
const long longPressTimeOut = 1000;
void setup()
{
Serial.begin(115200);
pinMode(buttonPin, INPUT_PULLUP);
Serial.println("Ready to detect button presses...");
}
void loop()
{
int reading = digitalRead(buttonPin);
if (reading != lastButtonState)
{
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay)
{
if (reading != buttonState)
{
buttonState = reading;
if (buttonState == LOW)
{
buttonPressStartTime = millis();
}
else
{
unsigned long pressDuration = millis() - buttonPressStartTime;
if (pressDuration >= longPressTimeOut)
{
Serial.println("Long press detected");
clickCount = 0;
}
else
{
if ((millis() - lastClickTime) < doubleClickTimeOut)
{
clickCount++;
}
else
{
clickCount = 1;
}
lastClickTime = millis();
}
}
}
}
if (clickCount > 0 && (millis() - lastClickTime) > doubleClickTimeOut)
{
if (clickCount == 1)
{
Serial.println("Single click detected");
}
else if (clickCount == 2)
{
Serial.println("Double click detected");
}
clickCount = 0;
}
lastButtonState = reading;
}
Add a video of the output (know more)