The program reads the number of clicks (4096 clicks = 1 revolution) from a rotary encoder. Channel A is connected to pin 2 (interrupt 0), and B to pin 3 (interrupt 1). The program works 100% correct when running on a Uno (Will print the correct number of ticks and revolutions), however when running on a Mega2560 the output is a lot different.
When running on a Mega2560, the number of clicks has been multiplied by around 512, and they don't decrease when the encoder is reversed (as the Uno does).
Not sure what's going on here? I've tried different pins and interrupts but it's not making sense to me at the moment.
#define c_LeftEncoderInterruptA 0
#define c_LeftEncoderInterruptB 1
#define c_LeftEncoderPinA 2
#define c_LeftEncoderPinB 3
//declare quatrature encoder variables
volatile bool _LeftEncoderASet;
volatile bool _LeftEncoderBSet;
volatile bool _LeftEncoderAPrev;
volatile bool _LeftEncoderBPrev;
volatile long int _LeftEncoderTicks = 0;
int i = 0; //iterator
void setup() {
Serial.begin(9600);
//Setup Encoder
pinMode(c_LeftEncoderPinA, INPUT); // sets pin A as input
digitalWrite(c_LeftEncoderPinA, LOW); // turn on pullup resistors
pinMode(c_LeftEncoderPinB, INPUT); // sets pin B as input
digitalWrite(c_LeftEncoderPinB, LOW); // turn on pullup resistors
attachInterrupt(c_LeftEncoderInterruptA, HandleLeftMotorInterruptA, CHANGE);
attachInterrupt(c_LeftEncoderInterruptB, HandleLeftMotorInterruptB, CHANGE);
}
void loop() {
checkMovement(); //check movement of encoder
}
void checkMovement()
{
rev = _LeftEncoderTicks/4096; //get the current amount of revolutions
Serial.print("LeftEncoderTicks: ");
Serial.print(_LeftEncoderTicks);
Serial.println();
}
// Interrupt service routines for the left motor's quadrature encoder
void HandleLeftMotorInterruptA(){
_LeftEncoderBSet = digitalReadFast(c_LeftEncoderPinB);
_LeftEncoderASet = digitalReadFast(c_LeftEncoderPinA);
_LeftEncoderTicks+=ParseEncoder();
_LeftEncoderAPrev = _LeftEncoderASet;
_LeftEncoderBPrev = _LeftEncoderBSet;
}
// Interrupt service routines for the left motor's quadrature encoder
void HandleLeftMotorInterruptB(){
// Test transition;
_LeftEncoderBSet = digitalReadFast(c_LeftEncoderPinB);
_LeftEncoderASet = digitalReadFast(c_LeftEncoderPinA);
_LeftEncoderTicks+=ParseEncoder();
_LeftEncoderAPrev = _LeftEncoderASet;
_LeftEncoderBPrev = _LeftEncoderBSet;
}
int ParseEncoder(){
if(_LeftEncoderAPrev && _LeftEncoderBPrev){
if(!_LeftEncoderASet && _LeftEncoderBSet) return 1;
if(_LeftEncoderASet && !_LeftEncoderBSet) return -1;
}else if(!_LeftEncoderAPrev && _LeftEncoderBPrev){
if(!_LeftEncoderASet && !_LeftEncoderBSet) return 1;
if(_LeftEncoderASet && _LeftEncoderBSet) return -1;
}else if(!_LeftEncoderAPrev && !_LeftEncoderBPrev){
if(_LeftEncoderASet && !_LeftEncoderBSet) return 1;
if(!_LeftEncoderASet && _LeftEncoderBSet) return -1;
}else if(_LeftEncoderAPrev && !_LeftEncoderBPrev){
if(_LeftEncoderASet && _LeftEncoderBSet) return 1;
if(!_LeftEncoderASet && !_LeftEncoderBSet) return -1;
}
}
}
It's almost like the second interrupt isn't working at all, because when I disable it and run it on the Uno, I get the exact output as on the Mega.