Go Down

Topic: Reading off a Bouncing Push Button (Read 635 times) previous topic - next topic

adrean2land

Hi,

I'm having a problem reading off a bouncing push button that triggers an external interrupt. Here's how the code looks like:



int ledPin = 0; // connected to a LED
volatile int state = LOW; // start with the LED off

void setup()
{
  pinMode(ledPin, OUTPUT);
  pinMode(2, INPUT);
  digitalWrite(2, HIGH);  // set the built in pull up resistor
  attachInterrupt(0, blink, FALLING);
}

void loop()
{
  digitalWrite(pin, state);
}

void blink()
{
  state = !state;
}



Now I can't use a delay function inside the ISR, and even though i can still use a while loop that loops decrementing a unsigned long counter (4000000000), before the   state = !state; statement, it still doesn't make a difference and i still get incorrect readings off the push button. Any ideas on how to deal with this bouncing problem?


Thanks in advance!

PaulS

Quote
Any ideas on how to deal with this bouncing problem?

You can't use delay(), but you can use the blink without delay methodology. Has it been long enough since the last time I was called?

adrean2land


Quote
Any ideas on how to deal with this bouncing problem?

You can't use delay(), but you can use the blink without delay methodology. Has it been long enough since the last time I was called?



What's that methodology.

PaulS

Quote
What's that methodology.

To not use delay(). It appears that you didn't even look at the example.

adrean2land


Quote
What's that methodology.

To not use delay(). It appears that you didn't even look at the example.



Just took a look at it here:
http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay

I'm not trying to blink an LED. The code i posted here is just an example to demonstrate the problem I'm facing in my original program.
I'm trying to use both external interrupt pins to select two different modes (warm, hot) for a heating device that's reading off a temperature sensor. So i basically have two push buttons wired to pins 2 and 3 on the Uno to interrupt the processing cycle when i need to heat water inside the heating device to a specific temperature (two heating targets: warm and hot).

The problem is when pushing those switches, they bounce every now and then triggering interrupts that are not supposed to be triggered, so the ISR is executed more than once for a single push (theoretically)..
 

Thanks for the fast replies, Paul


marco_c

Do you need to use an interrupt at all? If you consider that in the time you press a button the CPU can execute hundreds of thousands of instructions, can you use this speed to adopt a polling mechanism instead, using the techniques shown in blink without delay?
Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Grumpy_Mike

Quote
I'm not trying to blink an LED. The code i posted here is just an example to demonstrate the problem I'm facing in my original program.

That is not the point.
What you do is to make a note in the ISR of the millis time when you do what ever you want to do in it, then the next time the ISR is called you return immediate if insufficient time has elapsed because then you know it is a bounce.

temononi

From my understanding millis don't work we'll in interrupts. See attachinterrupts tutorial. I would use the debounce library.

PaulS

Quote
From my understanding millis don't work we'll in interrupts.

It works just fine. It simply returns the same value every time it is called in the same interrupt, but the value is correct and meaningful the first time. Calling it in different interrupts, and determining the time between two interrupts is quite possible, and even easy.

Go Up