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?

 
 

 

 

Code

const int BUTTON =4;

int laststate=HIGH;
unsigned long presstime=0;
unsigned long releasetime=0;
bool doublec=false;



void setup() {
  
  Serial.begin(115200);
  pinMode(BUTTON,INPUT_PULLUP);

}

void loop() {
  int currentstate=digitalRead(BUTTON);
  if (laststate==HIGH&&currentstate==LOW){
    presstime=millis();
  }
  if(laststate==LOW&&currentstate==HIGH){
    unsigned long about=millis()-presstime;
    if (about>50){
      if(about<1000){
        if (doublec&&millis()-releasetime<250){
          Serial.println("Double click!");
          doublec=false;
        }else{
          doublec=true;
        }
        releasetime=millis();

      }
    }
  }

  if(currentstate==LOW&&millis()-presstime>1000){
    static bool longpress=false;
    if(!longpress){
      Serial.println("LOng click");
      longpress=true;
      doublec=false;
    }
    if (currentstate==HIGH) longpress=false;
  }

  if(doublec&&millis()-releasetime>250){
    Serial.println("single click!");
    doublec=false;

  }
  laststate=currentstate;
}

 

 

 

Output

Video

Add a video of the output (know more)

 

 

 

 

Was this helpful?
Upvote
Downvote