Author Topic: external interrupts problem arduino  (Read 10560 times)

0 Members and 1 Guest are viewing this topic.

Offline parasbhanotTopic starter

  • Contributor
  • Posts: 16
  • Country: in
external interrupts problem arduino
« on: February 13, 2016, 04:50:01 am »
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 both arduino uno and arduino leonardo. So i don`t think that something is wrong with my arduino hardware.

 :(

void setup() {
 
  pinMode (2,INPUT_PULLUP); // turn on pull-up resistors

  attachInterrupt(digitalPinToInterrupt(2), INT0_ISR, FALLING);
 
   Serial.begin(9600);

}


void loop() {

}

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
 

Offline alsetalokin4017

  • Super Contributor
  • ***
  • Posts: 2055
  • Country: us
Re: external interrupts problem arduino
« Reply #1 on: February 13, 2016, 05:09:45 am »
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/
The easiest person to fool is yourself. -- Richard Feynman
 

Offline parasbhanotTopic starter

  • Contributor
  • Posts: 16
  • Country: in
Re: external interrupts problem arduino
« Reply #2 on: February 13, 2016, 05:17:01 am »
yes i have tried delay function but it still does not work as expected.
 

Offline obiwanjacobi

  • Frequent Contributor
  • **
  • Posts: 988
  • Country: nl
  • What's this yippee-yayoh pin you talk about!?
    • Marctronix Blog
Re: external interrupts problem arduino
« Reply #3 on: February 13, 2016, 06:08:16 am »
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
Arduino Template Library | Zalt Z80 Computer
Wrong code should not compile!
 

Offline parasbhanotTopic starter

  • Contributor
  • Posts: 16
  • Country: in
Re: external interrupts problem arduino
« Reply #4 on: February 13, 2016, 06:47:18 am »
   :popcorn:

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. 
« Last Edit: February 13, 2016, 06:55:04 am by parasbhanot »
 

Online DimitriP

  • Super Contributor
  • ***
  • Posts: 1300
  • Country: us
  • "Best practices" are best not practiced.© Dimitri
Re: external interrupts problem arduino
« Reply #5 on: February 13, 2016, 07:11:52 am »
   If three 100  Ohm resistors are connected in parallel, and in series with a 200 Ohm resistor, how many resistors do you have? 
 

Offline mikerj

  • Super Contributor
  • ***
  • Posts: 3237
  • Country: gb
Re: external interrupts problem arduino
« Reply #6 on: February 13, 2016, 08:11:40 am »
   :popcorn:

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.

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

Offline parasbhanotTopic starter

  • Contributor
  • Posts: 16
  • Country: in
Re: external interrupts problem arduino
« Reply #7 on: February 13, 2016, 10:46:12 am »
 :)

donalmorrissey`s blog was very helpful.

Problem solved.

Thank you everyone .
« Last Edit: February 13, 2016, 10:49:06 am by parasbhanot »
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: external interrupts problem arduino
« Reply #8 on: February 13, 2016, 12:46:43 pm »
"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.
« Last Edit: February 13, 2016, 12:48:15 pm by dannyf »
================================
https://dannyelectronics.wordpress.com/
 

Offline Kilrah

  • Supporter
  • ****
  • Posts: 1852
  • Country: ch
Re: external interrupts problem arduino
« Reply #9 on: February 14, 2016, 04:47:54 pm »
void INT0_ISR()
{
   Serial.println("interrup fired");
   
   // detachInterrupt(digitalPinToInterrupt(2));
  // delay (100); 

}
2 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.
-
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: external interrupts problem arduino
« Reply #10 on: February 14, 2016, 05:37:31 pm »
Quote
You'd want to detachInterrupt() first thing when entering the ISR

That seems to be the standard operating procedure for the arduino crowd, and is an absolute idiocy.
================================
https://dannyelectronics.wordpress.com/
 

Online DimitriP

  • Super Contributor
  • ***
  • Posts: 1300
  • Country: us
  • "Best practices" are best not practiced.© Dimitri
Re: external interrupts problem arduino
« Reply #11 on: February 14, 2016, 10:27:46 pm »
:)

donalmorrissey`s blog was very helpful.

Problem solved.

Thank you everyone .

Captain Kirk:  What happened to the horse?
Dr McCoy:  It's dead Jim.
   If three 100  Ohm resistors are connected in parallel, and in series with a 200 Ohm resistor, how many resistors do you have? 
 

Offline parasbhanotTopic starter

  • Contributor
  • Posts: 16
  • Country: in
Re: external interrupts problem arduino
« Reply #12 on: February 15, 2016, 04:05:54 am »
 :popcorn:

This is my final code  (without sleep)

Code: [Select]
int pin2 = 2;

static int flag = 0;

void pin2Interrupt(void)
{

detachInterrupt(0);

flag = 1;

}

void setup()
{

// Serial.println is used just for debugging purposes

Serial.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: [Select]
#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();
 
}
 

Offline obiwanjacobi

  • Frequent Contributor
  • **
  • Posts: 988
  • Country: nl
  • What's this yippee-yayoh pin you talk about!?
    • Marctronix Blog
Re: external interrupts problem arduino
« Reply #13 on: February 15, 2016, 06:34:01 am »

static volatile int flag = 0;    // volatile because var is accessed by both loop and isr.


Arduino Template Library | Zalt Z80 Computer
Wrong code should not compile!
 

Offline parasbhanotTopic starter

  • Contributor
  • Posts: 16
  • Country: in
Re: external interrupts problem arduino
« Reply #14 on: February 15, 2016, 09:35:04 am »
ok, thanks
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf