Arduino mega interrupt problem. Code or wiring?


My rotary encoder works fine with UNO, but when I switched to Mega2560 I cannot get it to work. I checked all the pins on Mega for interrupt but now In thinking the issue is in the code.

I’m trying to get the code to read the number of steps the motor is taking

Could anyone help me figure it out or guide me in some direction?

I have tried almost all the pins mentioned on the net as Arduino interrupt pins, but regardless it should work with the default digital pins 2-3 in code since those are mega interrupt pins too.

motors_using_methods_.ino (4.07 KB)

PinChangeInt.h (15.6 KB)

#define ENCODER_LEFT_A 2  // Motor 2 Interrupt Pin - INT 1 - Right Motor
#define ENCODER_RIGHT_A 21  // Motor 1 Interrupt Pin - INT 0 - Left Motor

Pin 2 is not external interrupt 1.
Pin 21 is not external interrupt 0.

Not that it matters, since you are not using the external interrupt pins. You are using pins that need to support pin change interrupts. The SoftwareSerial reference page explains which pins on the Mega support pin change interrupts. You MUST use value from that list, if you are going to expect pin change interrupts to function.

#define ENCODER_LEFT_A 2 // Motor 2 Interrupt Pin - INT 1 - Right Motor
#define ENCODER_RIGHT_A 3 // Motor 1 Interrupt Pin - INT 0 - Left Motor

sorry, I had a typo on that still, even if I have the interrupt in pin 2 and 3 it doesn't work.

yeah and for the pins I'm using the interrupt (Digital Pins Usable For Interrupts)

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

I tried all of them and still, it's not working.

You must use pins that support pin change interrupts. Those are separate from the INTx stuff.
On an Uno every pin supports PCINTs but only a small subset of the pins on a Mega2560:
0, 10-15, 50-53, A8-A15.