function call errormessages - beginners problem


hi all!
 i trying create function holds statemachine tackling bouncing of buttons.

i errormessage initialization needed. here's code. greatful help.
kindest regards,
lasse


const int buttonpin = 2;    // number of pushbutton pin
const int ledpin = 13;      // number of led pin
const long debouncedelay = 50;    // debounce time; increase if output flickers
long starttime=0;
long timebuttonpressed;  // last time output pin 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 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 message , place code inside code marks. select code , press button of toolbar symbol '<>'. place error messages too.


Arduino Forum > Using Arduino > Programming Questions > function call errormessages - beginners problem


arduino

Comments

Popular posts from this blog

invalid use of void expresion in FlexiTimer2 library

error: a function-definition is not allowed here before '{' token

LED Strip Code