Arduino UNO and interrupts (strange things)

Hi all,

i'm playing with UNO and the interrupts, but I'm facing some strange (of course for me) problems.

The project is: two ISR, one attached to PIN2 and the other to PIN 3.
One executed during the raising of the input (on PIN2) and the other during the falling (on PIN3).

The strange thing is: just connecting an input sugnal on the PIN 2, when it is raising the "blink1" is executed (and this is normal) but when the signal is falling the "blink2" routine is called (i think this is not normal: i was expecting that this routine was executed just on the falling of the PIN3, NOT ON THE pin2).

Here below my code.
I'm confused, i'm sure i'm doing a mistake but I don't know where.
Thanks in advance


void setup()
{
pinMode(2, INPUT); // Pin 2 is input to which a switch is connected = INT0
pinMode(3, INPUT); // Pin 3 is input to which a switch is connected = INT1
attachInterrupt(0, blink1, RISING);
attachInterrupt(1, blink2, FALLING);
Serial.begin(9600);
}

void loop() {
}

void blink1() {
Serial.println("blink1");
}

void blink2() {
Serial.println("blink2");
}

try getting the serial.prints out of the ISR's like this:

otherwise, it may be hardware

volatile int counter1 = 0;
volatile int counter2 = 0;
void setup()
{
  pinMode(2, INPUT);      // Pin 2 is input to which a switch is connected = INT0
  pinMode(3, INPUT);      // Pin 3 is input to which a switch is connected = INT1
  attachInterrupt(0, blink1, RISING);
  attachInterrupt(1, blink2, FALLING);
  Serial.begin(9600);
}

void loop() 
{
  if (counter1 > 0)
  {
    Serial.println("blink1");
    counter1--;
  }
  if (counter2 > 0)
  {
    Serial.println("blink2");
    counter2--;
  }
}  

void blink1() 
{              
  counter1++;
}

void blink2() 
{              
  counter2++;
}
void blink1() {              
    Serial.println("blink1");
}

void blink2() {              
    Serial.println("blink2");
}

Interrupts are automatically disabled when an ISR is called. Serial.print() depends on interrupts.
As suggested, have the ISRs simply update a variable (declared as volatile),and move the Serial.prints outside of the ISRs and have the prints controlled by the value of the variable updated by the ISR.

First of all thanks for the answers!

This is what i did:

  1. I tried to put the serial.print outside of the ISR (as suggested) but the same :frowning:
  2. To be sure that it is not a problem of the serial.print, i put the switch on/off of a led inside the ISR...but the same :frowning:
  3. i load the sw on another Arduino UNO to exclude hardware problems, but the same :frowning:

I'm lost.... :slight_smile:
Can someone with UNO board try to load the code above?
Thanks again
luke

How have you wired the devices that generate the interrupts?

Your first error is here:

  pinMode(2, INPUT);      // Pin 2 is input to which a switch is connected = INT0
  pinMode(3, INPUT);      // Pin 3 is input to which a switch is connected = INT1

you need to put:

  pinMode(2, INPUT_PULLUP); // Pin 2 is input to which a switch is connected = INT0
  pinMode(3, INPUT_PULLUP); // Pin 3 is input to which a switch is connected = INT1

or add external pullup or pulldown resistors.

What you saw was the capacitive coupling between the pins driving pin 3
when it was floating. This is because a CMOS input is very sensitive when floating
as no current is taken (well a current so tiny its negligible). Enable pull-ups and
pins are immune to such capacitive cross-coupling.

BTW "Your first error" is a phrase borrowed from the standard form letter
sent out to people with broken proofs of Fermat's Last Conjecture! I'm not
implying there are other errors.

Now is working!!!!! :sweat_smile:
That was the problem....
Thanks to all, in particular Mark, for the support! :slight_smile:
luke