All submissions

Single Double Click and Long Press Detection

Solving Approach

How do you plan to solve it?

 using arduino  and a switch
 

 

 

 

Code

#define but 13


int buttonState;
int lastButtonState = LOW;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
unsigned long Time = 0;

 

void setup() {
Serial.begin(115200);
pinMode(but, INPUT);

}

void loop() {
int button = digitalRead(but);

if (button != lastButtonState) {
lastDebounceTime = millis();
}

if ((millis() - lastDebounceTime) > debounceDelay) {
if (button != buttonState) {
buttonState = button;

if (buttonState == HIGH) {
if (millis() - Time < 300) {
Serial.println("Double click detected");
Time = 0; 
} else {
Time = millis();
}
}
}
}

if (buttonState == HIGH && (millis() - Time) > 1000 && Time != 0) {
Serial.println("Long press detected");
Time = 0; 
} else if (buttonState == LOW && (millis() - Time) > 300 && Time != 0) {

if ((millis() - Time) < 1000) {
Serial.println("Single click detected");
Time = 0;
}
}
lastButtonState = button;
}

 

 

 

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!