9. Single Double Click and Long Press Detection

Back To All Submissions
Previous Submission
Next Submission

Solving Approach

How do you plan to solve it?


To start the serial communication, set up an internal pull-up resistor on Arduino digital pin 7. To address switch noise, use robust debouncing with a 50 ms delay. Track press/release timing to identify single clicks click fast with the mouse and when double clicks click fast 2 times and lastly for long presses hold the click/button on the mouse and release.

 

 

 

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)

 

 

 

Was this helpful?
Upvote
Downvote