Count button presses within timefrime

Hi folks,

I want to count button presses within let's say 2 seconds and start an action.
For example:

If button was pushed 3 times, do action a
If button was pushed 4 times, do action b
If button was pushed 5 times, do action c

If neither of those amount of presses is reached within 2 seconds, the counter should be reset.

Thanks for any help!

(deleted)

Another thing to think about is when do I start/reset the timer? When I press a button do I set that as t0 or do I check backwards to see if there has already been a button pushed? Regardless of whichever strategy you pursue, it will be useful to have a flag set while you're within the timing window. If the flag is not set, you know that you're the first (and set it).

Of course you know we help you with your code, not write code for you.

Always show us your ‘current’ complete sketch.
Use CTRL T to format the sketch.
Please use code tags.
Use the </> icon in the posting menu.

[code] Paste sketch here. [/code]

for counting the presses of the button, use a lastButtonState variable so you don't count it every time through the loop.

like this:

if (lastButtonState != buttonState && buttonPin == HIGH) {
  counter++;
}

DH12043:
for counting the presses of the button, use a lastButtonState variable so you don't count it every time through the loop.

like this:

if (lastButtonState != buttonState && buttonPin == HIGH) {

counter++;
}

That's a wee bit presumptuous at this point, don't you think? Whilst any state change is typically the catalyst for some action, that action has yet to be defined and the conditions under which the trigger occurred are as yet unknown. Granted, there are only a few options, but as long as options exist we should not be so quick as to decide a path and then force the design to conform. A button push for example, is most easily handled as active low using the input pullup resistors - no external hardware.

Maintaining an audit of the previous/existing state when testing for a new value is of course paramount and a point well mentioned to be added to OP's checklist.

Put button from pin 4 to GND, press as many times as desired, wait for 1.5 sec timeout after presses stop.

uint32_t tStart, timeStart, tEnd = 1500; // adjust tEnd for timeout duration
const byte dbTime = 25, btn = 4, ledPin = LED_BUILTIN;
bool btnState = false, bState = true, oldBstate = true,
     timing = false;
byte cntr;

void setup()
{
  Serial.begin(9600);
  Serial.println("Button press counter");
  pinMode(btn, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
}
void loop()
{
// debounce-----------------------------
  if (digitalRead(btn) != btnState) // they are different!
  {
    btnState ^= 1;                  // make them equal
    timeStart = millis();           // restart timer
  }
  if (millis() - timeStart > dbTime)  // if not changed for dbTime,
     bState = btnState; // btnState is valid, use it
// end debounce-------------------------
  
  if(!timing && !bState && oldBstate)
  {
    tStart = millis();
    timing = true;
  }
  if(timing && !bState && oldBstate)
  {
    cntr++;
    tStart = millis();
    oldBstate = bState;
  }
  if(millis() - tStart > tEnd) // timed out
  {
    timing = false;
    while(cntr > 0)
    {
      Serial.println(cntr);
      digitalWrite(ledPin,HIGH);
      delay(50);
      digitalWrite(ledPin,LOW);
      delay(500);
      cntr --; 
    }
  }
  oldBstate = bState;
}