Interrupt pins

Interrupts are great. But it's a bit difficult to use them in Arduino because the int numbers and pins don't line up. As a beginner, it was confusing to have to use "0" for pin 2 and "1" for pin 3. Why not add a line or two at the beginning of attachInterrupts and detachInterrupts to make the pins correspond? So then if you wanted a rising interrupt on pin2, you would do attachInterrupts(2, RISING, function); and attachInterrupts would decrement the number by 2. If we were using a mega's higher interrupts, then the formula is -pin + 23 (that's negative pin + 23)

So the complete listing:
pin int number how?
2 0 -2
3 1 -2
21 2 -pin + 23
20 3 -pin + 23
19 4 -pin + 23
18 5 -pin + 23

the only other modification needed would be to change the interrupt variable to an (signed) int to accommodate the intermediate negative numbers.

baum

you mean something like this..

void myIRQ(uint8_t pin, void (*userFunc)(void), int mode) 
{
  if (1 < pin && pin < 4) attachInterrupt(pin-2, userFunc, mode);
  else if (17<pin && pin < 22) attachInterrupt(23-pin, userFunc, mode);
}

probably needs am #ifdef MEGA or so ...

yes, exactly. Wouldn't that make it a tad easier for beginners to use interrupts? And isn't part of the idea of the Arduino project to create an easy-to-use board and language for beginners?

baum

Agree, but only partly

If people are so far that they can program interrupts they should be able to write a function like the one above. The interface of aattachInterrupt() is not too difficult so that extra layer is "costly".

Alternative could be to make some defines, which have the pinnummers in their name,

#define IRQ_PIN1 0
#define IRQ_PIN2 1
#define IRQ_PIN18 2
#define IRQ_PIN19 3
#define IRQ_PIN20 4
#define IRQ_PIN21 5

Think the energy of the development team could be better focussed on documentation how to use attachinterrupt() and interrupts, volatile etc

Yes, that is true.

baum

I just recently published a new higher performance Encoder library. Indeed, I had to put a bunch of definitions for the pin to int mapping on various boards. It's a shame that info isn't available as a macro or inline function from the core...

Here's that library, if anyone's interested.

http://www.pjrc.com/teensy/td_libs_Encoder.html

Thank you for your contribution Paul.

Lefty