Pages: [1]   Go Down
Author Topic: Issue using two attachInterrupt  (Read 599 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 everyone,

I have a question concerning the attachInterrupt functions which work pretty nice when only one is settled but which gives weird result when two of them are defined.

I'm using an Arduino Uno Rev3 and I want to use 2 external interrupts (defined on pins 2 and 3) to launch different sequences in my code. Therefore, I tried first to make a very easy example taken from the Arduino website (http://arduino.cc/en/Reference/AttachInterrupt) in which a led toggle if the interrupt is activated with pin 2 but just adding a second interrupt with pin 3. However, when I manually change the value of the pin 2 for example, both interrupts are activated.

Code:
int PIN_LED1 = 11;            // pin on which LED1 is connected
int PIN_LED2 = 12;            // pin on which LED2 is connected
volatile int state = LOW;     // state of the LED1
volatile int state2 = LOW;    // state of the LED2
volatile int n = 1;
volatile int n2 = 1;

void setup()
{
  Serial.begin(9600);
 
  pinMode(PIN_LED1, OUTPUT);             // definition of the pin with LED1 in output
  pinMode(PIN_LED2, OUTPUT);             // definition of the pin with LED2 in output
  attachInterrupt(0, blink1, CHANGE);    // definition of the interrupt on pin 2 for each change of the pin's value
  attachInterrupt(1, blink2, CHANGE);    // definition of the interrupt on pin 3 for each change of the pin's value
}

void loop()
{
  if (n == 2){
    //digitalWrite(PIN_LED1, state);    // LED1 is turned ON or OFF if the interrupt has been called
    Serial.println("LED1");                  // for debugging purpose
    n = 1;
  }
  if (n2 == 2){
    //digitalWrite(PIN_LED2, state2);
    Serial.println("LED2");
    n2 = 1;
  }
}

void blink1(){         // if the interrupt on pin2 is called, we change the LED1's state
  state = !state;
  n = 2;                 // use of n to avoid unwanted glitch effect
}
void blink2(){
  state2 = !state2;
  n2 = 2;
}



I have also tried to define the interrupt "from scratch" with the EICRA and EIMSK registers but the result is the same : both interrupts are activated although only one pin changed.

Any idea? I tried many possibilities and searched on many forums but didn't get a solution yet.
Thanks for your help
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 481
Posts: 18740
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

However, when I manually change the value of the pin 2 for example, both interrupts are activated.

What do you mean by "manually change"? What is your wiring?
Logged


Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 611
Posts: 49092
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
volatile int state = LOW;     // state of the LED1
volatile int state2 = LOW;    // state of the LED2
volatile int n = 1;
volatile int n2 = 1;
This just bugs me. Why would you number one variable and not the other?
Logged

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

What do you mean by "manually change"? What is your wiring?

I just take a wire from the 5V of the arduino and link it to the pins 2 or 3 to generate the pin's value change like if it came from an external sensor

Code:
volatile int state = LOW;     // state of the LED1
volatile int state2 = LOW;    // state of the LED2
volatile int n = 1;
volatile int n2 = 1;
This just bugs me. Why would you number one variable and not the other?

You mean why I didn't define my variables as state1 and state2 instead of state and state2 ?!
I thought the example was easy enough not to make this effort, but I should have, I admit
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 611
Posts: 49092
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I just take a wire from the 5V of the arduino and link it to the pins 2 or 3 to generate the pin's value change like if it came from an external sensor
But, you are not enabling the internal pullup resistor on the interrupt pins, so, without a wire attached, the pins are floating.

You should enable the pullups, and then ground a pin to trigger the interrupt.
Logged

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

 smiley I see now. I didn't take into account the analog aspect of the problem.
Thanks Paul for your response.
It's true that if the pin is continuously attached to an external sensor or device the line will be either high or low enabling the use of the CHANGE mode in the attachInterrupt although in my case I could not. Cool :-)

I will just post the good code as well as the more "advanced" one soon. Thanks again
Logged

Pages: [1]   Go Up
Jump to: