Issue using two attachInterrupt

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.

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

TheSotr:
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?

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?

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

PaulS:

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

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.

:slight_smile: 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 :slight_smile:

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