Count +1 only within a given time frame

Hi,

I have a button and a program that counts the number of times the button is pushed. I wonder if it would be possible to only allow the counter to increase the number of counts within a given time frame. In practice, if I push the button the program returns “1” if I push the button again the program returns “2”.

Would it be possible for the program to only increase the count once, then wait for e.g. 5 seconds before it can count the next push?

UPDATE: I think I figured it out by adding the delay(5000); after Serial.printIn(numberOfTurnOn);
For more experienced users, your confirmation of whether or not this would be sufficient is very much appreciated.

Here is my code in the current state:

/*
 * Program for the 1960s Panco Mini-Match table top soccer game.
*/

//Define constants
const int BUTTON_PIN = 2;//Pin2 connected to Button
const int LED_PIN = 13;//Pin3 connected to a LED
int currentButtonState = 0;//HIGH / LOW
int previousButtonState = 0;//HIGH / LOW
int numberOfTurnOn = 0;

void setup() {
  //run once  
  pinMode(BUTTON_PIN, INPUT);
  pinMode(LED_PIN, OUTPUT);  
  Serial.begin(9600);
}

void loop() {   
  //Run repeatedly   
  currentButtonState = digitalRead(BUTTON_PIN);
  if(currentButtonState != previousButtonState) {
      if(currentButtonState == HIGH) {
      numberOfTurnOn = numberOfTurnOn + 1;
      Serial.print("Number of button pushes: ");
      Serial.println(numberOfTurnOn);
      delay(5000);
      digitalWrite(LED_PIN, HIGH);    
    } else {
      digitalWrite(LED_PIN, LOW);
    }
  }
  
  previousButtonState = currentButtonState;
}

Any help or suggestions would be highly appreciated :slightly_smiling_face:

a delay prevents any other processing

consider

/*
* Program for the 1960s Panco Mini-Match table top soccer game.
*/
//Define constants
#undef MyHW
#ifdef MyHW
const int BUTTON_PIN = A1;

#else
const int BUTTON_PIN = 2;//Pin2 connected to Button
#endif

enum { Off = HIGH, On = LOW };

#define Interval  5000

const int LED_PIN = 13;//Pin3 connected to a LED
int currentButtonState = 0;//HIGH / LOW
int previousButtonState = 0;//HIGH / LOW
int numberOfTurnOn = 0;

unsigned long msecLst = 0;

void setup() {
    //run once
    pinMode(BUTTON_PIN, INPUT_PULLUP);
    pinMode(LED_PIN, OUTPUT);
    Serial.begin(9600);
}

void loop() {
    unsigned long msec = millis ();
    //Run repeatedly
    currentButtonState = digitalRead(BUTTON_PIN);
    if(currentButtonState != previousButtonState) {
        previousButtonState = currentButtonState;

        if(currentButtonState == On) {
            if ( (msec - msecLst) > Interval)  {
                msecLst = msec;
                numberOfTurnOn++;
                Serial.println (numberOfTurnOn);
            }
        }
    }
}
1 Like

@gcjr you are absolutely correct. Although this did seem to work it would as you wrote stop any other processing. A very good point and a heads up for a beginner like me.

Okay, I probably will need some time to understand exactly what practice and changes you did with the program, but it sure does work like a charm.

I am very grateful for what you did and I hope I can return the favor sometime in the future.

Thank you!

when the code detects a button press, it compares the difference between the current timestamp (msec) to a previous timestamp (msecLst) to some Interval. if the delta > Interval, it processes the button press: incrementing the count and resetting the previous timeStamp. othersize it ignores the button press

1 Like

@gcjr I can only say thanks once again for the elaborate. I will for sure go through each line of the code to get the understanding of what is actually happening. With the logic posted above it will make it easier for me.

Thank you!

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