I've got a simple piece of code based on an arduino example on interrupts. But neither the original source nor my own seem to ever fire an interrupt. I've tried this on an Arduino Micro and an Arduino Uno with identical results.
int statePin = 13;
int interruptPin = 2;
volatile int state = LOW;
void setup()
{
pinMode(statePin, OUTPUT);
attachInterrupt(0, signaled, CHANGE);
pinMode(interruptPin,INPUT);
Serial.begin(115200);
}
void loop()
{
digitalWrite(statePin, state);
Serial.print((state)?"on ":"off ");
Serial.println((digitalRead(interruptPin)?"high":"low"));
delay(500);
}
void signaled()
{
Serial.println("interrupted");
state = !state;
}
When I run the code and press the button attached to pin two the Arduino correctly reflects the state of the pin, but the interrupt is never fired.
off high
off high
off high
off high
off high
off low
off low
off low
off low
off high
off high
off high
off low
off low
Is there something that I missed that is preventing the interrupt from firing?
You're attaching the interrupt to pin 0 there, but everywhere else you are referencing interruptPin, which is set to 2.
Pin 0 doesn't have any interrupt facilities on it.
Surely that line attachInterrupt(0, signaled, CHANGE); means interrupt#0, which is on pin#2
D'oh, yes you're right - sorry, I've only had one coffee so far today, and my head has been deep in Java inside the IDE source code. As a result I'm now not firing on all cylinders.
I think that's the only instance where there isn't a pin->function mapping in the board variant, so it's often guesswork which pin has which interrupt assigned to it. It would be far better if there was a proper mapping like there is for all the other functions (PWM, analogue, etc).
Ignore me.
But don't ignore the comment by Riva about not using Serial inside an ISR.
JimboZA:
Surely that line attachInterrupt(0, signaled, CHANGE); means interrupt#0, which is on pin#2
That's what I also thought. I connected my wire to Pin 3 instead and things are working. I thought Pin 3 was on interrupt 1.
The white wire in this picture is what I am using to trigger the interrupt (hoping a second set of eyes can tell me that I am reading the pin labels correctly).
j2inet:
That's what I also thought. I connected my wire to Pin 3 instead and things are working. I thought Pin 3 was on interrupt 1.
Uno: Int 0 is pin 2.
Micro/Leonardo: Int 0 is pin 3.
j2inet:
The white wire in this picture is what I am using to trigger the interrupt (hoping a second set of eyes can tell me that I am reading the pin labels correctly).
Without a pull-up resistor, you are going to get random/sporadic behavior.
Something like this might make your life easier when you're coding for interrupts a lot. Uno code is down below.
// Return the interrupt number for the specified pin.
// The output of this function is passed to the Arduino library function attachInterrupt()
// Return <0 on error
inline int iPinToInterrupt_Mega_2560( int iPinNumber )
{
// http://arduino.cc/en/Main/arduinoBoardMega2560
switch( iPinNumber )
{
case 2:
return 0;
case 3:
return 1;
case 18:
return 5;
case 19:
return 4;
case 20:
return 3;
case 21:
return 2;
default:
// Not a valid interrupt pin on Mega-2560
return -1;
};
} // iPinToInterrupt_Mega-2560
inline int iPinToInterrupt_UnoR3( int iPinNumber )
{
// http://arduino.cc/en/Reference/AttachInterrupt
switch( iPinNumber )
{
case 2:
return 0;
case 3:
return 1;
default:
// Not a valid interrupt pin on Uno R3
return -1;
};
} // iPinToInterrupt_UnoR3