I need a working debounce code for a single push button. This push button I use common shaft rotary encode type which consist of a Push Button. It is not a momentary switch. Pls don't ask me to use momentary switch as I need to use this encoder type.
I use the below code which is not working properly. The error is it the Mode jump from 1,2,0, 1,2,0 ..It doesn't go into Mode 3. Even I change the debounce to 6 states, it is still working.
Any ideas to fix this from the below code.
void debounce(void){
//Counter for number of equal states
static uint8_t count = 0;
//Keep track of current states
static uint8_t button_state = 0;
//Check if button is high or low for the moment
uint8_t current_state = (~PINA & BUTTON_MASK) !=0;
if (current_state != button_state){
//button state is about to be change
count++;
if (count >=4){
//The button have not bounced for four state, change state
button_state = current_state;
//if the button was pressed, tell me so
if (current_state !=0) {
button_down =1;
}
count =0;
}
} else {
count = 0;
}
}
int main(){
while (1)
{
debounce();
if (button_down)
{
button_down = 0;//reset flag
Mode ++;
//disp.setNumber(mode);
if (Mode==4) Mode = 0;
switch (Mode)
{
case 0 ://nothing
bla....
}
}
}
}








