external interrupts problem arduino  - Page 1
Forum section: electronics / general

author: parasbhanot
author_rank: Contributor
timestamp: February 13, 2016, 04:50:01 am
content: Hello everyone ,I am trying to implement external interrupt using push button in my project But the only problem is when i press the button the interrupt routine is fired several times . i have tried to disable interrupt in interrupt routine using detachInterrupt function but the problem still exist. i have tested the following code in botharduino unoandarduino leonardo. So i don`t think that something is wrong with my arduino hardware.voidsetup() {pinMode(2,INPUT_PULLUP); // turn on pull-up resistorsattachInterrupt(digitalPinToInterrupt(2), INT0_ISR, FALLING);Serial.begin(9600);}voidloop() {}void INT0_ISR(){Serial.println("interrup fired");// detachInterrupt(digitalPinToInterrupt(2));// delay (100);}I was watching Jeremy blum`s video on youtube in which he used RC circuit with Schmitt trigger to hardware de-bounce the push button.But i am not sure if it the most efficient way to solve this problem.Is there any alternative ?Can I de-bounce this push button in interrupt routine ?Thanks in advance

author: alsetalokin4017
author_rank: Super Contributor
timestamp: February 13, 2016, 05:09:45 am
content: Have you tried simply using the delay() in the interrupt routine (without the detach statement) , and increasing the value until it stops bouncing? I don't know if this will work and I'm too lazy at the moment to try it myself...The standard way is to do it in hardware though, as shown in the video or in this instructable (pretty much the same thing):http://www.instructables.com/id/Arduino-Push-Switch-Debouncing-Interrupts/

author: parasbhanot
author_rank: Contributor
timestamp: February 13, 2016, 05:17:01 am
content: yes i have tried delay function but it still does not work as expected.

author: obiwanjacobi
author_rank: Super Contributor
timestamp: February 13, 2016, 06:08:16 am
content: It is not recommended to solve it this way. Buttons are pushed by slow humans and handling doesn't require any interrupts. Using an interrupt for push buttons will make your code execution less predictable because you cannot predict how much time is spent in the ISR. When buttons wear out more and more time is spent in the interrupt - starving your program and changing its behavior possibly to a point of failure.Usually buttons are scanned in the main loop and debounced in software (detect a downstate for 10-20 ms) or in hardware (R-C).Hope it helps,Marc

author: parasbhanot
author_rank: Contributor
timestamp: February 13, 2016, 06:47:18 am
content: I am aware that buttons are generally processed in main loop but In my main project i am trying to wake up arduino on button press . So i am out of options here.

author: DimitriP
author_rank: Super Contributor
timestamp: February 13, 2016, 07:11:52 am
content: Look at this and take it from there:http://donalmorrissey.blogspot.com/2010/04/putting-arduino-diecimila-to-sleep.html

author: mikerj
author_rank: Super Contributor
timestamp: February 13, 2016, 08:11:40 am
content: Quote from: parasbhanot on February 13, 2016, 06:47:18 amIn that case you only need to enable the external interrupt just before you put the arduino to sleep (clear any pending interrupt before enabling though). In the interrupt handler, disable interrupts and then then start polling the button(s) in your main loop again.

author: parasbhanot
author_rank: Contributor
timestamp: February 13, 2016, 10:46:12 am
content: donalmorrissey`s blog was very helpful.Problem solved.Thank you everyone .

author: dannyf
author_rank: Super Contributor
timestamp: February 13, 2016, 12:46:43 pm
content: "When i press the button the interrupt routine is fired several times . "Because your interrupt is edge triggered, there is really no solution to your problem within your framework.Hardware denouncing is an option, or scanning (activated in the isr) is another.BTW, no point in disabling the interrupt in the isr as it is done automatically.

author: Kilrah
author_rank: Unknown
timestamp: February 14, 2016, 04:47:54 pm
content: Quote from: parasbhanot on February 13, 2016, 04:50:01 am2 things:- You'd want to detachInterrupt() first thing when entering the ISR... your serial.println() is a relatively long process that gives enough time for your switch to bounce and retrigger the interrupt before getting to the next statement.- As specified in the arduino doc, delay() does not do work in an ISR.-

author: dannyf
author_rank: Super Contributor
timestamp: February 14, 2016, 05:37:31 pm
content: QuoteThat seems to be the standard operating procedure for the arduino crowd, and is an absolute idiocy.

author: DimitriP
author_rank: Super Contributor
timestamp: February 14, 2016, 10:27:46 pm
content: Quote from: parasbhanot on February 13, 2016, 10:46:12 amCaptain Kirk: What happened to the horse?Dr McCoy: It's dead Jim.

author: parasbhanot
author_rank: Contributor
timestamp: February 15, 2016, 04:05:54 am
content: This is my final code (without sleep)Code:int pin2 = 2;static int flag = 0;void pin2Interrupt(void){detachInterrupt(0);flag = 1;}void setup(){// Serial.println is used just for debugging purposesSerial.begin(9600);pinMode(pin2, INPUT_PULLUP);attachInterrupt(0, pin2Interrupt, LOW);Serial.println("Initialisation complete.");}void loop(){if (flag == 1){Serial.println("interrupt fired ");flag = 0;delay(200);attachInterrupt(0, pin2Interrupt, LOW);}else {// main code}}And this is my final code (with sleep)Code:#include <avr/sleep.h>#include <avr/power.h>int pin2 = 2;void pin2Interrupt(void){detachInterrupt(0);}void enterSleep(void){attachInterrupt(0, pin2Interrupt, LOW);delay(100);set_sleep_mode(SLEEP_MODE_PWR_DOWN);sleep_enable();sleep_mode();sleep_disable();}void setup(){Serial.begin(9600);pinMode(pin2, INPUT_PULLUP);Serial.println("Initialisation complete.");enterSleep();}void loop(){delay(1000);Serial.println("interrupt fired ");enterSleep();}

author: obiwanjacobi
author_rank: Super Contributor
timestamp: February 15, 2016, 06:34:01 am
content: Quote from: parasbhanot on February 15, 2016, 04:05:54 am

author: parasbhanot
author_rank: Contributor
timestamp: February 15, 2016, 09:35:04 am
content: ok, thanks