Count +1 only within a given time frame

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