Go Down

### Topic: Interrupt pins (Read 1 time)previous topic - next topic

#### baum

##### Jan 02, 2012, 04:54 pmLast Edit: Jan 02, 2012, 04:56 pm by baum Reason: 1
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

#### robtillaart

#1
##### Jan 04, 2012, 07:50 pm
you mean something like this..

Code: [Select]
`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 ...
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### baum

#2
##### Jan 04, 2012, 09:07 pm
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

#### robtillaart

#3
##### Jan 04, 2012, 09:59 pm
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

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### baum

#4
##### Jan 04, 2012, 10:18 pm
Yes, that is true.

baum

#### pjrc

#5
##### Jan 05, 2012, 01:30 am
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

#### retrolefty

#6
##### Jan 05, 2012, 02:13 am

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

Go Up