Mega2560 ISR

Hi,

I am working with an Arduino Mega 2560. When I was looking to the subreddit for Arduino, I came across some projects which used the ISR(PCINT1_vect).

FYI, I have been working with the PIC16F1829 microcontroller so I have experience with microcontrollers and interrupts.

The appearance of this interrupt service routine made me wonder if the way of doing interrupts using attachInterrupt() and detachInterrupt() is the only way using the Mega2560. As I have (will have) around eight ping sensors, I want to handle them using interrupts (and so keeping my loop a bit cleaner). I found out that I can not attach an interrupt on both the rising and falling edge at the same time.

So I decided to first attach the rising edge interrupt, when I handled that interrupt, I detach it and attach the falling edge interrupt, and vice versa. This way I am able to measure the length of an pulse and thus get the time.

I can do this for one, and for more when I use a multiplexer (74HC238). But I was wondering if using the ISR(PxINTx_vect) routine I could use more pins with interrupts than the documentation suggests?

Regards,
Martin.

I found out that I can not attach an interrupt on both the rising and falling edge at the same time.

What about using CHANGE and examining the pin state in the ISR ?

UKHeliBob:
What about using CHANGE and examining the pin state in the ISR ?

Wow. I completely read over that one.. That solves at least the ugly code of detaching-attaching :slight_smile:

That solves at least the ugly code of detaching-attaching

You can use pin change interrupts on any pin that supports them, to increase the number of interrupts that you can deal with. The SoftwareSerial reference page shows the pins on the Mega that support pin change interrupts (and the SoftwareSerial code shows how they can be used).

@PaulS: So only the pins described in the documentation of SoftwareSerial are the possible pins for an interrupt handler on CHANGE?

mbeentjes:
@PaulS: So only the pins described in the documentation of SoftwareSerial are the possible pins for an interrupt handler on CHANGE?

They are the only pins that support pin change interrupts.

The external interrupt pins can, of course, be used, too, with the regular attachInterrupt() function.

According to the reference on AttachInterrupt() the external interrupt pins are:

Mega, Mega2560, MegaADK 2, 3, 18, 19, 20, 21

So. The pins listed in the AttachInterrupt() documentation and the pins in the SoftwareSerial are the pins available for interrupts. The SoftwareSerial documentation describes the pins I can use for the CHANGE interrupt handle type.

In what situation do I use the ISR(..) { ... } routine then?

mbeentjes:
So. The pins listed in the AttachInterrupt() documentation and the pins in the SoftwareSerial are the pins available for interrupts. The SoftwareSerial documentation describes the pins I can use for the CHANGE interrupt handle type.

In what situation do I use the ISR(..) { ... } routine then?

It sounds like you are confusing hardware interrupts as described in the attachInterrupt() documentation that can use only a few pins https://www.arduino.cc/en/Reference/AttachInterruptand pin change interrupts that can use many more pins Arduino Playground - HomePage

Nick Gammon has a good interrupt tutorial

Reading the Atmel datasheet is also essential IMHO.

...R

The problem is (/was) that I have multiple ping sensors which I have connected to the Mega. I want to use interrupts to measure the length of the returning pulse (which corresponds to the measured distance). And as I am planning to work to use around eight ping sensors.

But with the use of ISR I see that I will have enough with one interrupt vector. With that said, using the ISR solve the problem.

And to get my head clear: I can use the ISR and corresponding vectors in my code to use every pin.

You could use the NewPing library, which knows how to do interrupt driven pinging.

Thanks for your suggestion. I've implemented the library in my code now. It looks much better.

And the example of multiple sensors is exactly what I need. Thanks!