Does the Pin Change library work on Arduino Micro?

I'm trying to create an Arduino Micro based controller with six buttons. I've tried version 1.72 (latest release) and 2.19 (latest beta) from Google Code Archive - Long-term storage for Google Code Project Hosting. but it doesn't seem to work at all.

The code compiles and uploads but grounding the pins doesn't appear to trigger the ISR functions. Replacing the syntax of that library with the standard Arduino syntax does work and the ISR functions get triggered reliably when I press the buttons, but of course using the standard Arduino syntax limits me to four buttons.

I can post the entire sketch if necessary, but the problem appears to be right here:

  pinMode(REDBUTTON, INPUT);
  digitalWrite(REDBUTTON, HIGH);
  attachInterrupt(REDBUTTON, redButtonISR, FALLING);
  //  PCintPort::attachInterrupt(REDBUTTON, &redButtonISR, FALLING);

The code above works, but if I comment out line 3 and uncomment line 4 then pressing the red button no longer has any effect.

I also experimented with removing the & in front of the ISR name although the ampersand is shown in the example documentation.

I also used a wire to ground each digital and analog pin one at a time in case the pin numbering is misleading and the interrupt was getting attached to a pin I wasn't expecting. Again, nothing. The full program uses the PCintPort syntax to attach six different ISR functions, one per button, but despite grounding every digital and analog pin on the Ardunio Micro (one pin at a time) none of the ISR functions were ever triggered.

I'm about to assume that the chip in the Micro is incompatible with the library, but figured I would ask if anyone else has used this library with the Micro.

Your code is incomplete, and it seems nether an installation or troubleshooting problem.

You're correct that the code I posted is incomplete. I'm asking a hardware question, not a software question. I would post it in the Hardware forum but the Hardware forum is marked as read only so this forum "For problems with Arduino itself, NOT your project" seems the most relevant place to ask whether pin change interrupts work on the Arduino Micro.

I'm giving the authors the benefit of assuming that the library does in fact work on some Arduino models.

However, if you really want to read a full program, rather than post my program (most of which has absolutely nothing to do with my question), how about this one:

// PinChangeIntExample, version 1.1 Sun Jan 15 06:24:19 CST 2012
// See the Wiki at http://code.google.com/p/arduino-pinchangeint/wiki for more information.
//-------- define these in your sketch, if applicable ----------------------------------------------------------
// You can reduce the memory footprint of this handler by declaring that there will be no pin change interrupts
// on any one or two of the three ports.  If only a single port remains, the handler will be declared inline
// reducing the size and latency of the handler.
//#define NO_PORTB_PINCHANGES // to indicate that port b will not be used for pin change interrupts
//#define NO_PORTC_PINCHANGES // to indicate that port c will not be used for pin change interrupts
// #define NO_PORTD_PINCHANGES // to indicate that port d will not be used for pin change interrupts
// if there is only one PCInt vector in use the code can be inlined
// reducing latency and code size
// define DISABLE_PCINT_MULTI_SERVICE below to limit the handler to servicing a single interrupt per invocation.
// #define       DISABLE_PCINT_MULTI_SERVICE
//-------- define the above in your sketch, if applicable ------------------------------------------------------
#include <PinChangeInt.h>

// This example demonstrates a configuration of 3 interrupting pins and 2 interrupt functions.
// All interrupts are serviced immediately, but one of the pins (pin 4) will show you immediately
// on the Terminal.  The other function connected to 2 pins sets an array member that is queried in loop().
// You can then query the array at your leisure.
// This makes loop timing non-critical.

// Add more Pins at your leisure.
// For the Analog Input pins used as digital input pins, and you can use 14, 15, 16, etc.
// or you can use A0, A1, A2, etc. (the Arduino code comes with #define's
// for the Analog Input pins and will properly recognize e.g., pinMode(A0, INPUT);
#define PIN1 2
#define PIN2 3
#define PIN3 4

uint8_t latest_interrupted_pin;
uint8_t interrupt_count[20]={0}; // 20 possible arduino pins
void quicfunc() {
  latest_interrupted_pin=PCintPort::arduinoPin;
  interrupt_count[latest_interrupted_pin]++;
};

// You can assign any number of functions to any number of pins.
// How cool is that?
void pin3func() {
  Serial.print("Pin "); Serial.print(PIN3, DEC); Serial.println("!");
}

void setup() {
  pinMode(PIN1, INPUT); digitalWrite(PIN1, HIGH);
  PCintPort::attachInterrupt(PIN1, &quicfunc, FALLING);  // add more attachInterrupt code as required
  pinMode(PIN2, INPUT); digitalWrite(PIN2, HIGH);
  PCintPort::attachInterrupt(PIN2, &quicfunc, FALLING);
  pinMode(PIN3, INPUT); digitalWrite(PIN3, HIGH);
  PCintPort::attachInterrupt(PIN3, &pin3func, CHANGE);
  Serial.begin(115200);
  Serial.println("---------------------------------------");
}

uint8_t i;
void loop() {
  uint8_t count;
  Serial.print(".");
  delay(1000);
  for (i=0; i < 20; i++) {
    if (interrupt_count[i] != 0) {
      count=interrupt_count[i];
      interrupt_count[i]=0;
      Serial.print("Count for pin ");
      if (i < 14) {
        Serial.print("D");
        Serial.print(i, DEC);
      } else {
        Serial.print("A");
        Serial.print(i-14, DEC);
      }
      Serial.print(" is ");
      Serial.println(count, DEC);
    }
  }
}

That's a full program straight out of the Examples sub-directory of Google Code Archive - Long-term storage for Google Code Project Hosting. and it doesn't work. It compiles and uploads and prints a line of periods but it never prints any indication of any interrupt being triggered despite the fact that I grounded every single pin, one at a time, and the fact that on the same Arduino Micro I can trigger interrupts on the four supported hardware interrupt pins using the standard Arduino syntax.

I'm sorry if my explanation of why I want to use the Pin Change library obscured the fact that I'm asking a question about the capabilities of the Arduino Micro specifically, not about my project or programming or any of the other topics of the other forums. I am aware that the various different models of Arduino differ in their capabilities and it is quite possible that pin change interrupts simply don't work on the Micro but I have been unable to locate any information that states one way or the other.

I've Googled every combination of terms I can think of, but given the relatively generic nature of the name "micro" combined with the fact that the Arduino Micro is just one of a myriad of different microcontrollers I was unable to locate any page that could tell me whether or not the Arduino Micro supports pin change interrupts.

attachInterrupt() needs an interrupt number, not a pin number. They are not the same.

The Uno and Micro have different pins associated with interrupts 0 and 1.

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

attachInterrupt() needs an interrupt number, not a pin number. They are not the same.

Understood, but not relevant to my question. As I said, the hardware interrupts (which is what your hyperlink refers to) work fine. I have no problem at all configuring all four of the available hardware interrupts on my Arduino Micro (the Micro and Leonardo have four, unlike the Uno and other models that only have two) but since I want to have six buttons the hardware interrupts are inadequate so I would like to use pin change interrupts.

http://playground.arduino.cc/Main/PinChangeInt

As documented here Google Code Archive - Long-term storage for Google Code Project Hosting. the pin change interrupt library uses pin numbers, not hardware interrupt numbers. The following code is copied directly out of the documentation:

setup() {
  pinMode(2, INPUT); digitalWrite(2, HIGH);
  PCintPort::attachInterrupt(2, &quicfunc, CHANGE);
  Serial.begin(115200);
  Serial.println("---------------------------------------");
}

Note how the exact same number is used in the pinMode statement, the digitalWrite statement and the PCintPort::attachInterrupt statement.

I'm not asking a programming question. I've read the documentation for the PinChangeInt library and it seems very clear and understandable, however the example code provided by the library doesn't work. I'm asking a hardware question, namely, does the Micro support pin change interrupts (i.e. a totally different thing than hardware interrupts, of which the documentation clearly states that it supports four) or is this one of the areas where different models of Arduino are incompatible.

At the risk of confusing people further, I'll clarify why I am asking about hardware support. It's because I have an Uno which is built into another project. If the Micro does not support pin change interrupts then I will disassemble the other project and replace the Uno in it with the Micro and use the Uno for the project where I want to have six buttons. However, I don't want to waste my time disassembling a working project if the Micro does support pin change interrupts and just needs some extra work on the library.

The AVR datasheets, register manipulations, and assembly language hacking are beyond my current level but if someone could assure me that the Micro does support pin change interrupts (not hardware interrupts, I already know how to use those) then I will dig into Google and try to figure out how to fix the pin change interrupt library so that it works on Micro. But if the Micro simply doesn't support pin change interrupts then I'll focus my efforts instead on redesigning my project so that the larger Uno (which the pin change interrupt documentation clearly states is supported) will fit into it and rebuild the project where my Uno is currently in use to substitute the Micro.

The documentation for the pin change interrupt library states

This library was designed for the Arduino Uno/Duemilanove, and has been reported to work fine on the Nano, but it has not been tested there. As mentioned above, MEGA support is included but support for that platform is weak.

but it doesn't mention the Micro, Leonardo or any other models. The Uno contains an ATmega328 while the Micro contains an ATmega32u4. I'm trying to find out whether that "u4" in place of an "8" is sufficient difference that the library can't work due to hardware incapability or if it just means that the library code needs to be extended or fixed to make it work.

I'm not asking a programming question

Then, why are you in the Programming forum?

paulATchakobsaDOTnet:
I'm asking a hardware question, namely, does the Micro support pin change interrupts

Yes. The ATmega32u4 has the same PCINT registers that the ATmega328 does. So it supports pin change interrupts.

However.

Arduino pins and AVR pins do not map the same on the 32u4 and 328. For example on 32u4 PD2 is Arduino D0. While on the 328 PD0 is D0. Pin D7 is a worse (better?) example. 32u4: PE6. While on 328 it is PD7. They are on completely different ports.

Depending on how the library you are using maps pins, that may be the source of the problem.

Then, why are you in the Programming forum?

That's a good question. I should be posting in the Hardware forum, but that one is marked read only as of 2010 and it appears that nobody has created a new Hardware forum. That's why I posted in a forum where the description is

For problems with Arduino itself, NOT your project

but it appears that someone has moved my post to the Programming forum despite the fact that I have clearly stated that my question is about Arduino itself, NOT my project and is not a programming question.

Now, if it turns out that the Micro supports pin change interrupts but the pin change interrupt library needs changes in order to fix it them I'm sure I'll have some programming questions as I try to enhance the library. But I would start a new thread in the programming forum if I decide to take on the challenge of hacking on the library.

Right now I'm just seeking someone with HARDWARE expertise who can tell me whether the Micro HARDWARE supports the capability of pin change interrupts.

Now, I suppose I could post in the Microcontrollers forum on the theory that every Arduino is a microcontroller, but the description

Standalone or alternative microcontrollers, in-system programming, bootloaders, etc.

leads me to believe that the microcontroller forum is intended for alternative microcontrollers which, on an Arduino website, I take to mean other microcontrollers that are NOT Arduino.

I'm really puzzled as to why people are more interested in critiquing the choice of which forum to post in rather than technical discussion of the Arduino hardware itself.

Thank you James, that is helpful. It sounds like it is a worthwhile investment of my time to read through the code of the Pin Change Interrupt library and see if I can comprehend enough of it to patch it to support the 32u4.

I may be back with programming questions later since that's a bit beyond my current level of expertise, but at least now I know it won't be wasted effort.

I was a little worried that the 26 page datasheet for the 32u4 vs the 448 page datasheet for the 328 meant that the 32u4 was severely less capable, but I also suspected that the extreme difference in length might mean that the 32u4 datasheet is "abbreviated" and the 32u4 actually may support a lot of stuff that's documented in other datasheets (which are by and large beyond my current comprehension.)

The PinChangeInt library does work with the Arduino Micro, but its ATmega32U4 microcontroller only supports this functionality on the 8 pins of port B.

These pins are: SS, SCK, MOSI, MISO, as well as digital pins 8, 9, 10 and 11.

In this regard the Micro is at a disadvantage with respect to the ATmega328 based Arduinos, such as the Uno and Nano, who support pin change interrupts on most of their pins.