All submissions

Single Double Click and Long Press Detection

Solving Approach

How do you plan to solve it?

 
 

 

 

 

Code

const int BUTTON_PIN = 2;
unsigned long pressTime = 0;
bool waitingForSecondClick = false;

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  Serial.begin(9600);
  Serial.println("Button Detector Ready");
}

void loop() {
  if (digitalRead(BUTTON_PIN) == LOW) { 
    delay(50); //
    pressTime = millis();
    
    
    while (digitalRead(BUTTON_PIN) == LOW) {
      if (millis() - pressTime > 1000) { 
        Serial.println("Long Press");
        waitingForSecondClick = false;
        return;
      }
    }
    
    delay(50); 
    
    if (waitingForSecondClick) {
      Serial.println("Double Click");
      waitingForSecondClick = false;
    } else {
      waitingForSecondClick = true;
    }
  }
  
  if (waitingForSecondClick && (millis() - pressTime > 400)) {
    Serial.println("Single Click");
    waitingForSecondClick = false;
  }
}

 

 

 

Output

 

Video

Add a video of the output (know more)

 

 

 

 

Submit Your Solution

Note: Once submitted, your solution goes public, helping others learn from your approach!