All submissions

Single Double Click and Long Press Detection

Solving Approach

How do you plan to solve it?

 
 

 

 

 

Code

/*Paste your code here*/
const int buttonPin = 7;
unsigned long press = 0;
unsigned long release = 0;
int Count = 0;

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);   
  Serial.begin(9600);
}

void loop() {
  int state = digitalRead(buttonPin);

  if (state == LOW && press == 0) {
    press = millis(); 
  }

  if (state == HIGH && press > 0) {
    release = millis() - press; 
    press = 0;

    if (release > 800) {        
      Serial.println("Long press");
      Count = 0;              
    } else {
      Count++;
      delay(200);  

      if (Count == 1) {
        Serial.println("Single click");
      } else if (Count == 2) {
        Serial.println("Double click");
        Count = 0;           
      }
    }
  }
}

 

 

 

Output

Video

Add a video of the output (know more)

https://drive.google.com/file/d/1xU3vS8q8nwr9xbpn_N_4ekT8y1eahzp3/view?usp=drive_link

 

 

Submit Your Solution

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