All submissions

Single Double Click and Long Press Detection

Solving Approach

How do you plan to solve it?

 
 

 

 

 

Code

const int buttonPin = 5;

unsigned long pressTime = 0;

unsigned long releaseTime = 0;

int clickCount = 0;

void setup() {

  pinMode(buttonPin, INPUT);   

  Serial.begin(9600);

}

void loop() {

  int state = digitalRead(buttonPin);



  if (state == HIGH && pressTime == 0) {

    pressTime = millis(); 

  }

  if (state == LOW && pressTime > 0) {

    releaseTime = millis() - pressTime; 

    pressTime = 0;

    if (releaseTime > 800) {        

      Serial.println("Long press");

      clickCount = 0;              

    } else {

      clickCount++;

      delay(200);                  

      if (clickCount == 1) {

        Serial.println("Single click");

      } else if (clickCount == 2) {

        Serial.println("Double click");

        clickCount = 0;           

      }

    }

  }

}

 

 

 

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!