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