Pages: [1]   Go Down
Author Topic: Reading off a Bouncing Push Button  (Read 615 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 631
Posts: 50137
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 631
Posts: 50137
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What's that methodology.
To not use delay(). It appears that you didn't even look at the example.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Norfolk UK
Offline Offline
Faraday Member
**
Karma: 70
Posts: 2570
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe looking at Debounce (http://arduino.cc/en/Tutorial/Debounce) and adapting it to interrupt use.
Logged


Sydney, Australia
Online Online
Edison Member
*
Karma: 33
Posts: 1283
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 633
Posts: 34513
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 631
Posts: 50137
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: