Below is the sketch I have. I assume that I use a Arduino Mega. The way I calculate displacement based on count is likely wrong. I try to use the equation Displacement = 1/xN*(1/PPI) (http://www.ni.com/tutorial/7109/en/) where N is pulse per revolution of the encoder, PPI is pulse per inch and x is encoding type. I couldn't find N for my encoder (HEDS-9200-QOO).
volatile long countAB = 0; // count for encoder 1
volatile long countCD = 0; // count for encoder 2
volatile long positionAB, positionCD;
unsigned long timep, time, etime;
volatile boolean A,B,C,D; // C is channel A of encoder 2, D is channel B of encoder 2
volatile byte stateAB, stateABp, indexAB, stateCD, stateCDp, indexCD;
int QEM[16]={0,-1,0,1,1,0,-1,0,0,1,0,-1,-1,0,1,0};
void setup()
{
Serial.begin(9600);
pinMode(2, INPUT);//Channel A
pinMode(3, INPUT);//Channel B
pinMode(21, INPUT);//Channel C
pinMode(20, INPUT);//Channel D
attachInterrupt(0,Achange,CHANGE);
attachInterrupt(1,Bchange,CHANGE);
attachInterrupt(2,Cchange,CHANGE);
attachInterrupt(3,Dchange,CHANGE);
timep = micros(); //set intial time
//read the intial value of A & B
A = digitalRead(2);
B = digitalRead(3);
C = digitalRead(21);
D = digitalRead(20);
//set intial state value
if ((A==HIGH)&&(B==HIGH)) stateABp = 1;
if ((A==HIGH)&&(B==LOW)) stateABp = 2;
if ((A==LOW)&&(B==LOW)) stateABp = 3;
if ((A==LOW)&&(B=HIGH)) stateABp = 4;
if ((C==HIGH)&&(D==HIGH)) stateCDp = 1;
if ((C==HIGH)&&(D==LOW)) stateCDp = 2;
if ((C==LOW)&&(D==LOW)) stateCDp = 3;
if ((C==LOW)&&(D=HIGH)) stateCDp = 4;
}
void loop()
{
time = micros();
etime = time - timep;
if (etime > 100000) //0.1s
{
Serial.println(countAB);
Serial.println(positionAB);
Serial.println(countCD);
Serial.println(positionCD);
timep = time;
}
}
void Achange()
{
A = digitalRead(2);
B = digitalRead(3);
//determine state value
if ((A==HIGH)&&(B==HIGH)) stateAB = 0;
if ((A==HIGH)&&(B==LOW)) stateAB = 1;
if ((A==LOW)&&(B==LOW)) stateAB = 2;
if ((A==LOW)&&(B==HIGH)) stateAB = 3;
indexAB = 4*stateAB + stateABp;
countAB = countAB + QEM[indexAB];
positionAB = (countAB/4)*(1/180);
stateABp = stateAB;
}
void Bchange()
{
A = digitalRead(2);
B = digitalRead(3);
//determine state value
if ((A==HIGH)&&(B==HIGH)) stateAB = 0;
if ((A==HIGH)&&(B==LOW)) stateAB = 1;
if ((A==LOW)&&(B==LOW)) stateAB = 2;
if ((A==LOW)&&(B==HIGH)) stateAB = 3;
indexAB = 4*stateAB + stateABp;
countAB = countAB + QEM[indexAB];
positionAB = (countAB/4)*(1/180);
stateABp = stateAB;
}
void Cchange()
{
C = digitalRead(21);
D = digitalRead(20);
//determine state value
if ((C==HIGH)&&(D==HIGH)) stateCD = 0;
if ((C==HIGH)&&(D==LOW)) stateCD = 1;
if ((C==LOW)&&(D==LOW)) stateCD = 2;
if ((C==LOW)&&(D==HIGH)) stateCD = 3;
indexCD = 4*stateCD + stateCDp;
countCD = countCD + QEM[indexCD];
positionCD = (countCD/4)*(1/180);
stateCDp = stateCD;
}
void Dchange()
{
C = digitalRead(21);
D = digitalRead(20);
//determine state value
if ((C==HIGH)&&(D==HIGH)) stateCD = 0;
if ((C==HIGH)&&(D==LOW)) stateCD = 1;
if ((C==LOW)&&(D==LOW)) stateCD = 2;
if ((C==LOW)&&(D==HIGH)) stateCD = 3;
indexCD = 4*stateCD + stateCDp;
countCD = countCD + QEM[indexCD];
positionCD = (countCD/4)*(1/180);
stateCDp = stateCD;
}