Try this sketch with a pushbutton between GND and pin 4, press the button x number of times, if you pause for 1.5 seconds, the onboard led will flash as many times as you pressed.
uint32_t tStart, timeStart, tEnd = 1500;
const byte dbTime = 25, btn = 4, ledPin = 13;
bool btnState = false, bState = true, oldBstate = true,
timing = false;
byte cntr;
void setup()
{
Serial.begin(9600);
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)
{
timing = false;
while(cntr > 0)
{
digitalWrite(ledPin,HIGH);
delay(500);
digitalWrite(ledPin,LOW);
delay(500);
cntr --;
}
}
oldBstate = bState;
}