Hello community,
I am using a code I found in playground to measure the ticks of an incremental encoder I have on a motor.
I am monitoring channel A and B. At the datasheet the manufacturer states that the encoder give 500 pulses per rotation. With my code I see exactly 1000 counts per motor rotation.
Is it because I measure A channel in regards to B?
Should I measure A in regards to Ground?
The encoder as I said is a 5 volt incremental quadrature encoder with 8 cables, 500pulses/rotation.
5V, GND, A,B,A/,B/,I,I/
Code Attached
// Read two Quadrature Encoders
const int encoder1PinA = 2; // encoder 1 on pins 2 and 4
const int encoder1PinB = 4;
volatile int encoder1Pos = 0;
const int encoder2PinA = 3; //encoder 2 on pins 3 and 5
const int encoder2PinB = 5;
volatile int encoder2Pos = 0;
void setup() {
Serial.begin(9600);
Serial.print("Detecting Channels");
attachInterrupt(0, doEncoder1, CHANGE); // encoder pin on interrupt 0 (pin 2)
attachInterrupt(1, doEncoder2, CHANGE); // encoder pin on interrupt 1 (pin 3)
}
void loop() {
int Pos1, Pos2;
static int oldPos1, oldPos2;
uint8_t oldSREG = SREG;
cli();
Pos1 = encoder1Pos;
Pos2 = encoder2Pos;
SREG = oldSREG;
if(Pos1 != oldPos1){
Serial.print("A,B CH=");
Serial.println(Pos1,DEC);
oldPos1 = Pos1;
}
if(Pos2 != oldPos2){
Serial.print("A/,B/ CH=");
Serial.println(Pos2,DEC);
oldPos2 = Pos2;
}
}
void doEncoder1()
{
if (digitalRead(encoder1PinA) == digitalRead(encoder1PinB))
encoder1Pos++; // count up if both encoder pins are the same on pin change interupt
else
encoder1Pos--; //count diown if pins are different
}
void doEncoder2()
{
if (digitalRead(encoder2PinA) == digitalRead(encoder2PinB))
encoder2Pos++; // count up if both encoder pins are the same on pin change interupt
else
encoder2Pos--; //count diown if pins are different
}