arduino push button double click code problem

hi, I have made a code that can detect when I double click a button in quick succession (700 milliseconds). though when I keep the button pressed, the code also presumes that it is also a double click. can someone please help me. I have attached the ground to one of the pins on the button and pin 13 to the other pin on the button and used the internal pullup resistor.

int button;
int clicks = 0;

unsigned long current = 0;
unsigned long limmit = 0;

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

void loop() {
  button = digitalRead(13);
  
  if(button == false) {  // when the button is pressed
    clicks = clicks + 1;
    Serial.println(clicks);
    delay(200);  //gives it 200 milliseconds to keep the button pressed in

    if(clicks == 1) {
      current = millis();
      limmit = current + 700;
    }

    else if (clicks == 2) {
      clicks = 0;
      current = millis();
      
      if(current < limmit) {
        Serial.println("dubble clicked");
      }
      Serial.println(clicks);
    }
  }
  current = millis();
  if(current > limmit) {
    clicks = 0;
    Serial.println(clicks);
  }
  delay(1);
}

Hi,
Welcome to the forum.

From your code I see you are looking for the button press.
You should be looking for the button BEING pressed, in other words look for the change from LOW to HIGH.

This link may help.

Tom... :slight_smile:

Don't act on the button PRESS, act on it's RELEASE.

hey Tom,
thanks, it really helped. the code is now working great.

sonatman:
thanks, it really helped. the code is now working great.

So you should post it for review.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.