0

I have an interrupt signal attached to the A12 pin of an Arduino Mega 2560.

Would the following source line activate the interrupt processing? I am calling the digitalPinToInterrupt function on an Analog pin. It compiles fine.

attachInterrupt(digitalPinToInterrupt(A12), MessageFromIOBInt, FALLING);

where my interrupt code is

void MessageFromIOBInt(void) {
    ++gNumInterrupts;
}
  • What Arduino board are you using? – Edgar Bonet Oct 24 '16 at 13:38
  • I am using MEGA 2560 – Gregg Oct 24 '16 at 13:40
2

Interrupts only work on interrupt pins. Trying to translate a pin number into an interrupt number for a pin that isn't an interrupt pin will never work.

External Interrupts: 2 (interrupt 0), 3 (interrupt 1), 18 (interrupt 5), 19 (interrupt 4), 20 (interrupt 3), and 21 (interrupt 2). These pins can be configured to trigger an interrupt on a low level, a rising or falling edge, or a change in level.

Many pins (including the analog pins) can use the pin change interrupt. This requires an external library which has its own way of interfacing with them (see the examples).

2

Does 'digitalPinToInterrupt' work with analog pins?

It works in the sense that it compiles. When executed, for a pin that is not one of the interrupt pins it will return the value NOT_AN_INTERRUPT.

Thus a sensible test would be to check whether or not you get NOT_AN_INTERRUPT returned from digitalPinToInterrupt(A12).

A quick test confirms this:

void setup ()
{
  Serial.begin (115200);
  Serial.println ();
  Serial.println (digitalPinToInterrupt(A12));
}  // end of setup

void loop () { }

Output:

-1

In Arduino.h is this define:

#define NOT_AN_INTERRUPT -1

I have a page about interrupts. On that page, amongst other things, is a list of external interrupt pins for various Arduinos:

Interrupt pins

You can see that the pins on the board labelled D2, D3, D18, D19, D20, D21 are the only ones which support these sorts of interrupts (external interrupts). Various other pins support pin-change interrupts.

On the Mega2560 in particular these pins are supported for pin-change interrupts:

10, 11, 12, 13, 14, 15, 50, 51, 52, 53, A8 (62), A9 (63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68), A15 (69)


As Majenko mentioned, you use pin-change interrupts in a different way, possibly using the PinChangeInt library, or just accessing the registers yourself as described on the page I mentioned above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.