So and I’m kinda constructing a suped-up power wheels car. There’s been a couple bumps in a road but it’s been mostly a nice learning experience. There’s a lot of little things that pop up when working with upgrading inductive motors in one of was software Debouncing for the gas pedal so the logic below is just an example of what I use my code to resolve this issue. First when you detect that your switch is either open or closed, start a timer in some form. In my case I use a global variable, or a stack/local variable in the current function then I loop over it multiple times incrementing either a register or the value the variable. If each time I loop over the bun is still in a close position I can safely assume that it’s been Debounce and held down for at least some human period of time and it’s not some sort of electrical false positive. You can of course adjust the sleep time between checks or the amount of intervals you like to adjust through. In my case I felt that 200 µs 100 times was a reliable way to the sense that the signals Mindy bounced however if you want to tweak it you may.
if (GAS_PIN & (1 << GAS_BIT))
{ // is switch open?
while (debouncer != 100 )
{
if (!(GAS_PIN & (1 << GAS_BIT)))
{
lcd_write_instruction_4d(lcd_Clear);
_delay_us(80);
lcd_write_instruction_4d(lcd_SetCursor | lcd_LineOne);
_delay_us(80);
lcd_write_string_4d(“Opps, Debounced”);
_delay_us(80);
return;
}
_delay_us(200);
debouncer++;
}
//Do something
}