This should attach an interrupt to pin number 2 (according to http://www.arduino.cc/en/Reference/AttachInterrupt ). I have also tried using the old way of attaching to interrupt '0' to use pin 2 and I have also tried interrupt '4' to use pin 19 as well as tried attach interrupt '19' to use pin 19 but nothing is working. I realise it may be bad practice to put a serial command in an interrupt function but this is just to get the interrupt working.
Has anyone used external interrupts on the Due yet?
Are you sure it's not working using pin 19? Recall that once inside a ISR all interrupts are disabled and serial transmit is now interrupt driven, so the command can't execute even if the interrupt is properly triggered?
int state = 0;
void setup(){
Serial.begin(115200);
pinMode(2, INPUT);
attachInterrupt(19, state_change, CHANGE);
}
void loop(){
Serial.println(state);
delay(1);
}
void state_change(){
state = (state + 1);
}
So everytime I change the state of pin 19 (which I did by connecting it to another Arduino with a simple 'blink' type sketch, it should increment state. But it doesn't
Ah, thanks Graynomad! I will try it out. I apologise for creating this whole new thread for it, I did not come across that thread in my initial search for my answer.
For future reference, when using global variables shared by both the ISR and main loop you should define the variable as volatile. In your example it should be: