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);
}
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.
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.