user73108: i hope to get some help with my code.
I am trying to distinguish between a single press and a quick double press of the button with debouncing.
I am making some silly mistake in my code and i can't find out the problem. If I double press the button, both 'single' and 'double' gets printed on the serial monitor.
Thanks in advance for some help.
const int buttonPin = 5;
unsigned long lastPressTime;
int buttonState = 0;
int singlePress = 0;

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);

  lastPressTime = millis();
  Serial.begin(9600);
}

void loop() {
  if (!digitalRead(buttonPin) == HIGH && buttonState == 0) {
    unsigned long currentPressTime = millis();

    //two presses within 500ms
    if (currentPressTime - lastPressTime < 500) {
      singlePress = 0;
      //Do double press action
      Serial.println("double");
    } else {
      singlePress = 1;
    }

    //update the last pressed time
    lastPressTime = currentPressTime;
    buttonState = 1;
  } else if (!digitalRead(buttonPin) == LOW) {
    buttonState = 0;
  }

  //check if 500ms have passed with no second button press
  if (singlePress == 1 && lastPressTime  - millis() > 500) {
    singlePress = 0;
    //DO single press stuff
    Serial.println("single");
  }
}



user19964: The problem is this statement:
lastPressTime  - millis() > 500

Because time is stored as an unsigned long, this test as it is written now is always true.
You want to measure the time since the last press, so you should subtract the timestamp of the last press from the current time. Replace it with:
millis() - lastPressTime > 500

And the code will run fine.

73108: <p>i hope to get some help with my code.</p>
<p>I am trying to distinguish between a single press and a quick double press of the button with debouncing.</p>
<p>I am making some silly mistake in my code and i can't find out the problem. If I double press the button, both 'single' and 'double' gets printed on the serial monitor.</p>
<p>Thanks in advance for some help.</p>
<pre><code>const int buttonPin = 5;
unsigned long lastPressTime;
int buttonState = 0;
int singlePress = 0;

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);

  lastPressTime = millis();
  Serial.begin(9600);
}

void loop() {
  if (!digitalRead(buttonPin) == HIGH &amp;&amp; buttonState == 0) {
    unsigned long currentPressTime = millis();

    //two presses within 500ms
    if (currentPressTime - lastPressTime &lt; 500) {
      singlePress = 0;
      //Do double press action
      Serial.println(&quot;double&quot;);
    } else {
      singlePress = 1;
    }

    //update the last pressed time
    lastPressTime = currentPressTime;
    buttonState = 1;
  } else if (!digitalRead(buttonPin) == LOW) {
    buttonState = 0;
  }

  //check if 500ms have passed with no second button press
  if (singlePress == 1 &amp;&amp; lastPressTime  - millis() &gt; 500) {
    singlePress = 0;
    //DO single press stuff
    Serial.println(&quot;single&quot;);
  }
}

</code></pre>
 19964: <p>The problem is this statement:</p>
<pre><code>lastPressTime  - millis() &gt; 500
</code></pre>
<p>Because time is stored as an unsigned long, this test as it is written now is always true.</p>
<p>You want to measure the time since the last press, so you should subtract the timestamp of the last press from the current time. Replace it with:</p>
<pre><code>millis() - lastPressTime &gt; 500
</code></pre>
<p>And the code will run fine.</p>
