# Switch debouching using interrupt

#### jab99407

##### Member
Hello

I want to toggle LED by Switch Button using timer interrupt. I want to understand how timer interrupt use for Switch debouching

#### rjenkinsgb

##### Well-Known Member
One way is to use a byte as a counter.

In the interrupt routine:
If the switch is pressed, count up if the counter is less than a certain value.
If the switch is released, count down if it's above zero.

When the counter reaches the maximum value, consider the switch pressed. When it reaches zero, consider it released (setting or clearing a bit).

Use another bit as an "image" of the debounced switch signal; if the switch pressed is on and the image is off, it's a new press.
Increment another counter.

Either way, then copy the debounced input to the image.

The low bit of the second counter is the "toggle" bit you can use to control your LED.

There are many other ways, that's just the first I thought of.

#### jab99407

##### Member
One way is to use a byte as a counter.

In the interrupt routine:
If the switch is pressed, count up if the counter is less than a certain value.
If the switch is released, count down if it's above zero.

When the counter reaches the maximum value, consider the switch pressed. When it reaches zero, consider it released (setting or clearing a bit).
Can you help me with pseudo-code

I have written one

Code:
void main (void)
{

while (1)

{

}

}

// interrupt for 1 ms
void interrupt(void)
{
if(FlagSet)
{
if (switch === 1) //If the switch is pressed, count up

count++;

if (switch === 0) //If the switch is released, count down

count--;

ClearTFlag
}
}

#### Pommie

##### Well-Known Member
I use an interrupt to count mS and check keys every 20mS. Works perfectly.

Mike.

#### rjenkinsgb

##### Well-Known Member
Can you help me with pseudo-code
OK, your code extended to match my idea above.

Code:
void main (void) {
while (1) { }
}

// interrupt for 1 ms

void interrupt(void) {
if(FlagSet) {
if ( switch == 1 && count < 10 ) //If the switch is pressed, count up
count++;

else if ( switch == 0 && count > 0 ) //If the switch is released, count down
count--;

if ( count == 10 ) {
switchstate = true;

if (switchimage == false) {
state++;
}
else if (count == 0 )
switchstate = false;

switchimage = switchstate;

output = state & 0x01;

ClearTFlag
}
}

#### rjenkinsgb

##### Well-Known Member
You could also reset the counter instantly if the input was off & only verify the on state.
It's relatively complex, but allows for a programmable debounce time and consistent response time.

If input response is not critical and you want to sample at a slower rate, so more than any possible contact bounce duration, you can just compare the present state to the previous state and take it as valid if they are equal.

(Then copy the present state to the previous state store, for the next interrupt).

#### Ian Rogers

##### User Extraordinaire
Forum Supporter
Switch de-bouncing is overthought.. If its a high speed switching pulse, then I use a state machine.. But if it's your finger operating the switch.. well delay for 250mS as your input cannot be fast..

I have a keypad... I wait for a press.. Then delay 150mS.. Then beep a speaker for 150mS.. Then I know to let go of the button.. This way I get a decent key repeat..

If it is indeed a high speed mechanical switch they reckon 20mS but it will depend on your particular application..

One application I have is photosensor input from lifting forks on a fork truck... I wait for the switch.. toggle internal buffer, then wait for it to clear... then toggle internal buffer this way I can deduce state very similar to quad input.. Worked well for me for years..