I am using your 9.7:1 Metal Gearmotor 25Dx48L mm HP with 48 CPR Encoder (item #2271) with an Arduino Mega. I have it set up to use only encoder A, on pin 18 (interrupt 5). The interrupt is triggered on CHANGE. Given that the gear ratio is 9.68:1, I was expecting that 232.32 counts would equal one gearbox revolution. But, in fact, I seem to get roughly 260 counts per gearbox revolution. Any idea what is going on? Thanks.
Here is my code:
[code]/*
Rotary Encoder Interrupt sketch
*/
const int encoderPinA = 18;
const int encoderStepsPerRevolution = 260;
int pos, oldPos;
volatile int encoderPos = 0, revolutions = 0; // variables changed within interrupts are volatile
void setup()
{
pinMode(encoderPinA, INPUT);
Serial.begin(9600);
Serial.println(“Start”);
attachInterrupt(5, doEncoder, CHANGE);
}
void loop()
{
pos = encoderPos;
if (pos != oldPos)
{
float angle = (pos % encoderStepsPerRevolution) * (360 / (float)encoderStepsPerRevolution);
Serial.print(pos, DEC);
Serial.print ("\t");
Serial.print(revolutions);
Serial.print ("\t");
Serial.println (angle);
oldPos = pos;
delay(250);
}
}
void doEncoder()
{
encoderPos++; // count up if both encoder pins are the same
if (encoderPos % encoderStepsPerRevolution == 0) {
revolutions++;
}
}
[/code]