SparkFun Forums 

Where electronics enthusiasts find answers.

For the discussion of Arduino related topics.
By crxguy52
#160244
Hey all,

I'm working with a Sparkfun ProMicro 5V 16 MHz. I have an IR sensor that outputs high or low, depending on the color of the paper it's in front of. The problem is that I can get it to trigger an interrupt on pins 2 or 3 (int.0 or int.1), but using the same setup and program, I can't get interrupt 2, 3, or 4 to work. According to the AttachInterrupts page (http://arduino.cc/en/Reference/attachInterrupt) the Leonardo and ProMicro should support interrupts on pins 0, 1, and 7 as well. Am I missing something? My program is as follows, I tested the sensor with int.0 and int.1 and it works fine. When I changed the interrupt to int.2, 3, or 4, I get nothing.

Code: Select all
int flag = 0;

void setup() {
  attachInterrupt(0, test, CHANGE);

}

void test(){
  if (flag == 0){
    flag = 1;
  }
  else {
    flag = 0;
  }
}

void loop() {

if (flag == 1){
  digitalWrite(17, HIGH);
}
else {
  digitalWrite(17, LOW);
}

}
#160318
The short answer is that there are only two external interrupts on most of the AVR chips. The slightly longer answer is that you need to use a pin change interrupt to detect changes on other (digital) pins. Check out the Arduino library PinChangeInt which could meet your needs. It's on the playground with an example at:
http://playground.arduino.cc/Main/PinChangeInt
#160322
The 32U4 that's the brain of the ProMicro supposedly has more than 2 external interrupts. See section 11.
http://www.atmel.com/Images/doc7766.pdf
The problem seems to be that the Arduino IDE doesn't support those pins in that function at present. A "fix" on the product page was tried by the OP and found not to work ... for reasons unknown.
************************************************************************************
Member #250989 | about a year ago 1
Has anyone had trouble using external interrupts on the Pro Micro? I have sketch that runs fine on an Uno and a Pro Mini but when I load it on the Pro Micro the external interrupts are inoperative. Any thoughts?

M-Short | about a year ago 2
I don’t think Arduino has added external interrupts for the 32U4 to the IDE yet so you are going to have to code them in C or find someone who’s done it already.

meccup | about 10 months ago 1
Yes, I managed to make them working. But, I had to do some changes for the addon.

Here the files which I’ve changed:

http://dl.dropbox.com/u/3419665/arduino ... terrupts.c
http://dl.dropbox.com/u/3419665/arduino ... _private.h

Just replace old ones, whereupon you can use external interrupts:

0 – SCL
1 – SDA
2 – RX
3 – TX
4 – D7

Like that:

attachInterrupt(0..4, function, CHANGE);

I have no idea why sparkfun guys didn’t implemented this. Guys, why?

Member #341939 | about 3 months ago 1
I tried your modified files with no luck. Any hints? Do I need to rebuild the addon or is that done when compiling my sketch?

meccup | about 3 months ago 1
Nope. Just replace them.

****************************************************************************
One solution would be to use a different C compiler/environment.

Still you'd think the Leonardo would have the same problem and that someone would have a fix for that.
#160326
Apparently I missed that ProMicro bit. Looks like the library is out of step with the hardware. You could still code the ISRs by hand, since the IDE is using AVR-GCC to compile the code anyway. It's not too difficult to do, and you'd have access to all the interrupts on your chip. There's a ton of examples out there. A quick search on google located this one: http://gonium.net/md/2006/12/20/handlin ... h-arduino/
The code isn't that pretty, but it will let you see how to set up the handler and enable interrupts.
#160331
Aha! See this link:

http://propaneandelectrons.com/blog/int ... atmega32u4
INT6 on Arduino Leonardo / ATMega32U4
Mar 19 2013
The documentation for Arduino's attachInterrupt function lists the pins for the four interrupts available on an Arduino Leonardo. But the Leonardo uses the ATMega32U4, which has a fifth external interrupt (called external interrupt 6, or INT6, just to be confusing). INT6 is not available from the attachInterrupt() function, but is available if you access it directly via the registers EICRB (External Interrupt Control Register B) and EIMSK (External Interrupt Mask Register):

EICRB |= (1<<ISC60)|(1<<ISC61); // sets the interrupt type
EIMSK |= (1<<INT6); // activates the interrupt
For more information, see sections 11.0.2 and 11.0.3 of the ATMega32U4 datasheet.

To set the interrupt function, define it in the normal AVR way:

ISR(INT6_vect) {
// interrupt code goes here
}
The interrupt type is described in Table 11-3 of the datasheet, and is similar to interrupts 0 - 3:

ISC61 ISC60 Triggered By
0 0 INT6 low
0 1 Any logical change on INT6
1 0 Falling edge between two INT6 samples
1 1 Rising edge between two INT6 samples

For INT6 to work, it requires the AVR I/O clock to be present, unlike INT0 - INT3.

INT6 uses Port E Pin 6 (PE6), which corresponds to Digital Pin 7 on the Leonardo.
I tried this, and the external interrupt on pin 7 works. I summarized this in a sample code below:
Code: Select all
void setup(){

EICRB |= (1<<ISC60)|(0<<ISC61); // sets the interrupt type for EICRB (INT6). 
				                  // EICRA sets interrupt type for INT0...3

/*
ISCn0  ISCn1	Where n is the interrupt. 0 for 0, etc
  0      0	Triggers on low level
  1      0	Triggers on edge
  0      1	Triggers on falling edge
  1      1	Triggers on rising edge
*/

EIMSK |= (1<<INT6); // activates the interrupt. 6 for 6, etc
}

void loop(){
//do other things here
}

ISR(INT6_vect) {
  // interrupt code goes here
  }
It would be great if they included this in the IDR, but for now I'll post links in the product description and the arduino forum. There are also 8 pin change interrupts (which is really all I need, but this will work too), which is described in chapter 11 of the datasheet (all 4 pages of it). There aren't any examples, but I'm sure a google search would reveal one. I would assume it's similar to this.
#160592
track I had a 91 for about 7 years, it was a great car. I ended up swapping out the engine and turning it into a track car. I sold it about a year ago and bought an s2000 :).

I'll go ahead and mark it solved