All submissions

Single Double Click and Long Press Detection

Solving Approach

How do you plan to solve it?
Configure Arduino digital pin 7 with an internal pull-up resistor and initialize serial communication. Implement robust debouncing with a 50ms delay to handle switch noise while tracking press/release timing to detect single clicks (within 400ms), double clicks (two presses under 400ms apart), and long presses (over 1000ms). Connect the push button between digital pin 7 and GND (located near pin 13 on most Arduino boards), using the internal pull-up so no external resistor is needed. 

 

 

 

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)

 

 

 

Submit Your Solution

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