Pre increment of struct counter in a switch statement

I have the following code snippet:

In a separate .h file:

typedef struct{ int pin; int buttonState; int debounceTics; int holdTime; } button;

In my main file:

int buttonFSM(volatile button *ptrButton){ volatile int i; i = digitalRead(ptrButton->pin); switch(ptrButton->buttonState){ case SW_UP: if(i==0) //swtich went down { if(++ptrButton->debounceTics >= DBNC_TICS) { ptrButton->buttonState = SW_DOWN; ptrButton->debounceTics = 0; return SW_TRANS_UD; } } ptrButton->debounceTics = 0; return SW_UP; case SW_DOWN: if(i==1) //switch is back up { if(++ptrButton->debounceTics >= DBNC_TICS) { ptrButton->buttonState = SW_UP; ptrButton->debounceTics = 0; return SW_TRANS_DU; } } ptrButton->debounceTics = 0; return SW_DOWN; } }

The incremented timer count check never evaluates to true.. If I comment out the debounce counter the code works as expected. I have tried calling this inside and out side of an interrupt handler...

What have I goofed up??

Be better with all the code, and you need to put your code in code tags. You have this:

if(i==0) //swtich went down
{
if(++ptrButton->debounceTics >= DBNC_TICS)  
{         
    ptrButton->buttonState = SW_DOWN;
    ptrButton->debounceTics = 0;
    return SW_TRANS_UD;
}       
}
ptrButton->debounceTics = 0;

So this increments the debounceTics, but below the if statement it sets it back to 0. Do you even need to set it to 0 outside the if()? That goes for both switch up and down

Thanks for quick reply…

I want to reset the debounce count if the switch ever goes up before the debounce time, do not want to accumulate tics… then could get a premature debounce…

Do you see the issue though? Your incrementing one at best, but ALWAYS resetting to 0. so testing for any value above 1 is futile.

There is a return statement in the inner if, it should never execute the reset if the switch is held down.. maybe the compiler is doing something different??

Just above every single return you set the variable to zero. It is always getting reset, for every different path through the function.

And while this might do what you think it does, it doesn't look good to me.

if(++ptrButton->debounceTics >= DBNC_TICS)

You are dependent on the operator -> having a higher precedence than ++ but it just reads wrong. I would change this to:

if(++(ptrButton->debounceTics) >= DBNC_TICS)

so that every programmer reading this (including yourself one month from now) can see what you intended to happen.

Sorry my description was a pretty bad, Morgan basically said what I was trying to say :)

Thanks!! I knew it was something bone headed....!!

Here is the fixed version....

int buttonFSM(volatile button *ptrButton){ 
  volatile int i;
  i = digitalRead(ptrButton->pin); 
  switch(ptrButton->buttonState){
    case SW_UP:
      if(i==0) //swtich went down
      {   
        if(++(ptrButton->debounceTics) >= DBNC_TICS)
        {     
          ptrButton->buttonState = SW_DOWN;
          ptrButton->debounceTics = 0;
          return SW_TRANS_UD;
        }        
      }
      else
      {
        ptrButton->debounceTics = 0;
      }
      return SW_UP;
    case SW_DOWN:
      if(i==1) //switch is back up
      {
        if(++(ptrButton->debounceTics) >= DBNC_TICS)
        {
          ptrButton->buttonState = SW_UP;
          ptrButton->debounceTics = 0;
          return SW_TRANS_DU;
        }      
      }
      else
      {
        ptrButton->debounceTics = 0;
      }
      return SW_DOWN;   
  }  
}