Search code examples
cfunctionarduinoredefinition

Arduino: redefinition of a function error


"Getting the error message:

"error: 'int ButtonStateNext' previously declared here" (line 7 is where the issue lies)

"exit status 1"

"redefinition of 'int ButtonStateNext'

Whenever I try to compile my arduino program..Could someone shed some light on what exactly is going wrong? I understand that the error message is telling me that i've declared ButtonNextState previously, but I see nowhere that i've done so (atleast, intentionally?). The below is the entirety of the code (posted it all since its minimal) Thanks.

String ButtonState;
int Pin = 4;
unsigned long timer;
int input;

//function that is to be called in loop to service
int ButtonNextState( input ) {
//Switch statement based on state
    switch( ButtonState )
    {       
        case Idle:
            if(input == LOW)
            {
                //Record time
                timer = millis();
                //Set ButtonState to wait
                ButtonState = "Wait";
            }
            break;
        case Wait:
            if(input == HIGH)
            {
                ButtonState = "Idle";
            }
            else if(timer >= 5)
            {
                ButtonState = "Low";
                return 1;
            }
            break;
        case Low:
            if(input == HIGH)
            {
                ButtonState = "Idle";
            }
            break;
    } //end of switch statement

    return 0; // By default, return 0 indicating nothing is happening    
}

void setup() {
    //Set pin as input
    input = 4;
    //Set ButtonState to Idle
    ButtonState = "Idle";
}

void loop() {
    if( ButtonNextState( digitalRead( input ) ) )
    {
        //Send serial message indicating button press
        Serial.write("Button Pressed.");
    }
}

Solution

  • You have a typo here

    if( ButtonNextState( digitalRead( input ) )
    

    have to be

    if( ButtonNextState( digitalRead( input ) ) )
    

    And

    int ButtonNextState( input )
    

    should be at least

    int ButtonNextState( int input )