Arduino Due External Interrupts

Hi,

I'm trying to use external interrupts on the Arduino Due but can't seem to get it working. Here is my code:

void setup(){
  Serial.begin(115200);
  pinMode(2, INPUT);
  attachInterrupt(2, state_change, CHANGE);
}

void loop(){
}

void state_change(){
  Serial.println("woot");
}

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?

Thanks
Mark

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?

Lefty

Ok, so I changed the code to:

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 :frowning:

What IDE version do you have? I think the interrupts will only be supported as of 1.5.2

Recall that once inside a ISR all interrupts are disabled

Not the case natively with the Due, interrupts are nested by default. What they will do with the core files to "fix" that I'm not sure.


Rob

I am using 1.5. I'll give it a try as soon as I can get a hold of 1.5.2 (doesn't seem to be out yet).

I found the thread, it's "IDE 1.5.1 (released on Nov, 5)" that they will be implemented, or should that be were implemented :slight_smile:

http://arduino.cc/forum/index.php/topic,128525.0.html


Rob

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.

Yay, it works on 1.5.2r2 IDE! Thanks again! woop woop

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:

 volatile int state = 0;

http://arduino.cc/en/Reference/Volatile

Lefty

Is it possible to generate external Pin Change Interrupts in the Arduino IDE withou the attachIntterupt() routine?
Is there any examplecode?

@chec , you can try this one. YMFC-3D part 2 - Connect RC transmitter and receiver - Arduino quadcopter. - YouTube