Problem with MEGA2560 and interrupts

although I've been programming for 40 years, this is one of my first excursions into C. Read a couple of books on it. So I am not 100% new.

I have a program I developed that reads a rotary encoder on interrupts 0 and 1 (Pin 2 and 3 of UNO). The program works great on UNO, Mini-pro, NANO. But does not function on the MEGA2560. I have tried using pins 20 and 21 on the MEGA (INT0 and INT1), modified the directive to those pins, but no joy. I am obviously missing something here. BTW the rest of the program functions 100%. Its just the interrupts that are not functioning. I can find no references on the web that indicate the MEGA should not work.
I have not listed the code as it is a long program, but I can put snippets. I am hoping there is a neat "trick" to using interrupts on the MEGA.

The library function I am using is called 'Rotary.h'. It works with the UNO and NANO , but not the MEGA

Thanks,

Tim

Hello Tim,
did you use the digitalPinToInterrupt() function?
When you say the Interrupts are not working, what do you mean exactly? Are they never firing, firing sporadically, or just not doing what they should?
I would recommend going ahead and posting the snippets of where the Interrupts are initialized and the code for the interrut Service request itself.

I noticed you used capital 'R' for the header file. I had trouble using encoders on the Mega until I started to see that two strobes were being sent. See if this sheds any light.

JaBa, econjack, Thanks for the replies.

The interrupt is not recognized at all. I have a trap where I set a digital output (13) on if an interrupt is received. That trap works on the UNO, but not the MEGA. On the UNO, the encoder is connected to pins 2 and 3 (INT0 and INT1). I tried the exact same configuration on the MEGA. No joy. I then tried pins 20 and 21 (INT0 and INT1) on the MEGA. I modified the directive Rotary r = Rotary(20,21); Still no joy.

ORIGINAL CODE SNIPPETS

#include <rotary.h>

Rotary r = Rotary(2,3); // sets the pins the rotary encoder uses. Must be interrupt pins.

void setup ()

PCICR |= (1 << PCIE2);
PCMSK2 |= (1 << PCINT18) | (1 << PCINT19);
sei();

.
.

void loop()

ISR(PCINT2_vect) {
digitalWrite(13,1); // trap to show interrupt detected
unsigned char result = r.process();
if (result) {
if (result == DIR_CW){rx=rx+increment;}
else {rx=rx-increment;};
if (rx >=30000000){rx=rx2;}; // UPPER VFO LIMIT
if (rx <=1000000){rx=rx2;}; // LOWER VFO LIMIT
}
}

.
.
Tim

If that requires a change interrupt, you are limited to certain pins on the Mega. This is from the SoftwareSerial reference page.

Not all pins on the Mega and Mega 2560 support change interrupts, so only the following can be used for RX: 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).

  1. Please use code tags.
  2. It helps if we can compile the example to see the problem. It looks like that will compile with just a few more braces but I'm on a tablet now and can't check.
  3. There are better encoder libraries. Paul Stoffregen's one is the best I know. It works on all types of Arduino including Teensy and it does not require interrupt pins, although it is beneficial if one of the encoder pins is on an INTx pin.
 PCMSK2 |= (1 << PCINT18) | (1 << PCINT19);

On the Mega, PCINT18 and PCINT19 are pin change interrupts on Analog10 and Analog11.

SurferTim,

AHHHH, that looks like what I need. Yes, it needs interrupt on change. I must have missed that information when looking into the MEGA. I certainly will give this a try tonight.

Many thanks to all that replied!

Tim

Great Help Guys!!!

I used A10 and A11 as suggested by cattledog. Worked perfectly. I have some more studying to do on the Mega.

Again, great forum, thanks again.

Tim

8LandHam:
Great Help Guys!!!

I used A10 and A11 as suggested by cattledog. Worked perfectly. I have some more studying to do on the Mega.

Again, great forum, thanks again.

Tim

Please edit your #1 post to show "Solved"