Frequency counter Arduino

Try restructuring without using delays or gotos. For example (compiles, not tested...)

#define BTN_READ_INTERVAL       5ul         //read button every 5mS
#define BTN_PRESS_LEVEL         HIGH        //logic level corresponding to pressed
#define TIME_LED                1500ul      //time LED is on after reboot
#define SAMPLE_TIME             10000ul     //sample period

enum sampleStates_e
{
    ST_REBOOT=0,
    ST_SAMPLE
};

// this constant won't change:
const int  buttonPin = 5;    // the pin that the pushbutton is attached to
const int ledPin = 15;       // the pin that the LED is attached to
const int ledPin2 = 13;      // LED reboot D7

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() 
{
    // initialize the button pin as a input:
    pinMode(buttonPin, INPUT);
    // initialize the LED as an output:
    pinMode(ledPin, OUTPUT);
    pinMode(ledPin2, OUTPUT);
    // initialize serial communication:
    Serial.begin(115200);

    buttonPushCounter = 0;
    lastButtonState = digitalRead( buttonPin );
}

void loop() 
{
    static bool
        bLED = false;
    static uint8_t
        stateSample = ST_REBOOT;
    static uint32_t
        timeSample = 0ul;
    uint32_t
        timeNow = millis();
        
    switch( stateSample )
    {
        case    ST_REBOOT:
            Serial.println();
            Serial.print("MASTER 10-sec: ");
            Serial.println(buttonPushCounter);
            Serial.print("Reboot: ");
            
            digitalWrite(ledPin2, HIGH);
            bLED = true;
            
            buttonPushCounter = 0;
            timeSample = timeNow;
            stateSample = ST_SAMPLE;
            
        break;

        case    ST_SAMPLE:
            if( bLED )
            {
                if( (timeNow - timeSample) >= TIME_LED )
                {
                    bLED = false;
                    digitalWrite(ledPin2, LOW);
                    
                }//if
                
            }//if
            
            if( (timeNow - timeSample) < SAMPLE_TIME )
            {
                if( ReadButton() )
                    buttonPushCounter++;
                
            }//if
            else
            {
                stateSample = ST_REBOOT;
                
            }//else
            
        break;
        
    }//switch

}//loop

bool ReadButton( void )
{
    static uint32_t
        timeButton = 0ul;
    uint32_t
        timeNow = millis();

    if( (timeNow - timeButton) >= BTN_READ_INTERVAL )
    {
        timeButton = timeNow;
        uint8_t bNow = digitalRead( buttonPin );
        if( bNow != lastButtonState )
        {
            lastButtonState = bNow;
            if( bNow == BTN_PRESS_LEVEL )
                return true;
                
        }//if
        
    }//if
            
}//ReadButton

1 Like