Pages: [1]   Go Down
Author Topic: Arduino Due External Interrupts  (Read 2297 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

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

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
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17262
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, so I changed the code to:

Code:
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 smiley-sad
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8472
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Quote
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
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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).
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8472
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley

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

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17262
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:
volatile int state = 0;

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

Lefty
« Last Edit: November 15, 2012, 10:52:48 am by retrolefty » Logged

Pages: [1]   Go Up
Jump to: