Button Click Count Behaving Oddly

Hi All,

I’ve been trying to tackle this little issue for a few days now and thought i might come and see if you guys could help me understand where I’m going wrong, so basically i have a push button attached to my DuinoTech Leonardo, i’m attempting to count the time between button clicks and as long as the duration between each click is less than 2.5 seconds than continue performing, my trouble is that after the button is clicked for the first time the duration is always found to be less than 2.5 seconds even if i don’t click the button again. I have a feeling i’m going about the counting the wrong way, could you guys please help

Thanks,
Kind Regards,

#include <Keyboard.h>


unsigned long startTime;
unsigned long endTime;
unsigned long duration;
byte timerRunning;
bool IsTiming = 0;


const int buttonPin = 1;     // the number of the pushbutton pin

int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    Serial.println("Button is off");
  } else {
   
    // turn LED off:
    Serial.println("Button is on");

   if (timerRunning == 0 && buttonState == LOW){ // button pressed & timer not running already
  startTime = millis();
  timerRunning = 1;
  }
  if (timerRunning == 1 && buttonState == HIGH){ // timer running, button released
  endTime = millis();
  timerRunning = 0;
  duration = endTime - startTime;
  Serial.println(duration);

     }


     if (duration < 2500) {
     //Keyboard.press("w"); 
     Serial.println("Player is walking");
    }
    else {
    // Keyboard.release("w");
    Serial.println("Player is not walking"); 
    }
      
  }

}
if (timerRunning == 0 && buttonState == LOW){   //(buttonState == LOW): this check is useless because it will always to true.
if (timerRunning == 1 && buttonState == HIGH){ //do you mean to read the button again here?:
                                                                         //digitalRead(buttonPin)==HIGH instead of buttonState==HIGH

Sorry Arduino_New, i don't really get where you're coming from.

Your counting is probably ok. The indentation of your code not so :wink:

Use tools → autoformat in the IDE to properly indent.

Regarding the problem, buttons bounce. When you press or release a button, its state changes rapidly from closed to open to closed to open … till it stabilises.

You can use hardware debounce using e.g. a RC network on the pin or implement it in software; there is a debounce example in the IDE.

if i may suggest, consider the following pseudo code for more straightforward way to achieve your description.

DURATION = 2500; ///2.5 seconds
count = 0;

void setup()
{
   ...
}

void loop()
{

   button_status = read_button();
   if (button_status == key_pressed()) {
      count++;
      timer = millis();
   } else {
      if (millis() - timer >= DURATION) {
         //it has been more than 2.5s since last time we pressed button
         //reset everything to listen for new count
         count = 0;
      }
   }

}