function call errormessages - beginners problem

Hi all! I am trying to create a function that holds a statemachine tackling the bouncing of buttons.

I get the errormessage that an initialization is needed. Here's my code. Greatful for any help. Kindest regards, Lasse

const int buttonPin = 2; // the number of the pushbutton pin const int ledPin = 13; // the number of the LED pin const long debounceDelay = 50; // the debounce time; increase if the output flickers long startTime=0; long timeButtonPressed; // the last time the output pin was toggled

//buttonstates - possible states in statemachine const int start = 1; const int firstButtonPressDetected =2; const int countingMilliseconds = 3; const int countingBiggerThanButtonDelayTime=4; const int CountingBiggerThenButtonDelayTimeNoReleaseYet=5;

int state;//Current state in statemachine int ledState; int buttonReallyPressed; //output-signal from the statemachine

void setup() { state=start; ledState=LOW; buttonReallyPressed=LOW; pinMode(buttonPin, INPUT); pinMode(ledPin, OUTPUT); digitalWrite(ledPin, ledState); Serial.begin(9600); } void loop() { int reading = digitalRead(buttonPin); buttonReallyPressed = changeState(reading,state); if (buttonReallyPressed && state == countingBiggerThanButtonDelayTime){ ledState=!ledState; digitalWrite(ledPin,ledState); }//end if }//end loop

int function changeState(int reading, int state){ int buttonReallyPressed=0; switch (state){ case start: if (reading == HIGH) state = firstButtonPressDetected; break; case firstButtonPressDetected: if (reading == HIGH){ startTime=millis(); state = countingMilliseconds; } else state = start; break; case countingMilliseconds: if (reading == HIGH){ timeButtonPressed=millis()-startTime; if (timeButtonPressed > debounceDelay){ state = countingBiggerThanButtonDelayTime; } }else{ state = start; timeButtonPressed=0; buttonReallyPressed=LOW;

} break; case countingBiggerThanButtonDelayTime: buttonReallyPressed = HIGH; if (reading == HIGH) state = CountingBiggerThenButtonDelayTimeNoReleaseYet; else{ state = start; timeButtonPressed=0; buttonReallyPressed=LOW; } break; case CountingBiggerThenButtonDelayTimeNoReleaseYet: if (reading == LOW){ state = start; timeButtonPressed=0; buttonReallyPressed=LOW; } break; default: break;

}//end switch return buttonReallyPressed;

}//end function

Edit your message and place the code inside the code marks. Select you code and press the button of the toolbar with the symbol ‘<>’. Place the error messages that you get too.

In C you don't use the word "function" to define a function.

int function changeState(int reading, int state){

Should be

int changeState(int reading, int state){

Thank you no compilation errors!

But putting the code into a function-call was is not working.

Looks like I cannot modify global variables inside a function the way that I am doing it...

laskar01:
Thank you no compilation errors!

But putting the code into a function-call was is not working.

Looks like I cannot modify global variables inside a function the way that I am doing it…

If you want to change global variables don’t pass them, just change them in your function.

when you pass a global variable, the function gets a copy of it. It’s this copy that gets changed within your function. Once you return from your function, the original global variable will still be intact.