Pages: [1]   Go Down
Author Topic: attachInterrupt get interrupted pin  (Read 274 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'll try to use one function to handle interrupts (same type) on multiple pins.
My question is, how can I get the currently interrupted pin number?

Sample Code:
Code:
int pinEcho1 = 30;
int pinEcho2 = 32;

void handleInterrupt()
{
  Serial.print("handle interrupt on pin: ");
  Serial.println(/* how? */);
}

void setup()
{
  Serial.begin(9600);
 
  attachInterrupt(pinEcho1, handleInterrupt, CHANGE);
  attachInterrupt(pinEcho2, handleInterrupt, CHANGE);
}

void loop()
{
}
Logged

0
Online Online
Shannon Member
****
Karma: 216
Posts: 12548
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Use two handler functions.
Logged

[ I won't respond to messages, use the forum please ]

BCC AZ USA
Offline Offline
Edison Member
*
Karma: 38
Posts: 1307
It gets hot so it must be working
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

+1 for a handler for each interrupt.
Take the serial prints out of the interrupt handler(s).  Set a flag in handler and print in loop() when the flag is set.   
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sure two functions work, but I'd like to use only one.
Is there no way to determine the pin which is currently interrupted?

P. S. the Serial.print is just for the example

Thanks in advance
Logged

BCC AZ USA
Offline Offline
Edison Member
*
Karma: 38
Posts: 1307
It gets hot so it must be working
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The external interrupt flag register, EIFR, has a flag for each external interrupt. You should be able to check that register to see which pin caused the interrupt.  Check the data sheet under external interrupts.
Logged

0
Online Online
Shannon Member
****
Karma: 216
Posts: 12548
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The Arduino attachInterrupt mechanism already polls this register to
dispatch to the right function, so just use a function per pin.  These functions
can call common code with a pin number if you like - a macro can be used
to define them even :
Code:
#define PIN_HANDLER(pin) \
void pin_handler_##pin (void) \
{
  common_handler (pin) ;
}

- it would be nice if that could put the attachInterrupt calls in setup() but
alas C macros are lowly beasts with few powers.

Perhaps polling the register yourself is best, on reflection!

It wouldn't be hard to adapt WInterrupts.c to use handler functions
with the signature:

Code:
void handler (int pin)

But it would mean creating some port/mask -> Arduino pin reverse
mappings.

Logged

[ I won't respond to messages, use the forum please ]

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Mhh, not working... smiley-sad
Code:
int pinEcho1 = 30;
int pinEcho2 = 32;

#define PIN_HANDLER(pin) \
void pin_handler_##pin (void) \
{
  handleInterrupt(pin);
}

void handleInterrupt(int pin)
{
  Serial.print("handle interrupt on pin: ");
  Serial.println(pin);
}

void setup()
{
  Serial.begin(9600);
 
  attachInterrupt(pinEcho1, PIN_HANDLER(30), CHANGE);
  attachInterrupt(pinEcho2, PIN_HANDLER(32), CHANGE);
}

void loop()
{
}
Message:
Quote
sketch_sep18a.ino:7:18: error: expected constructor, destructor, or type conversion before '(' token
sketch_sep18a.ino:8:1: error: expected declaration before '}' token

Thanks smiley
Logged

0
Online Online
Shannon Member
****
Karma: 216
Posts: 12548
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry,. forgot all the line continuations on that macro.  Fix it...
Logged

[ I won't respond to messages, use the forum please ]

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Do you have any idea?

Thanks smiley
Logged

0
Online Online
Shannon Member
****
Karma: 216
Posts: 12548
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Do you have any idea?

Thanks smiley

Erm, fix the missing line continuations?  Just \
add \
them \
to \
the \
end.
Logged

[ I won't respond to messages, use the forum please ]

Pages: [1]   Go Up
Jump to: