here is the code,
int pulses;
int deg = 0;
int encoderA = 15;//Yellow originals were 1
int encoderB = 16;//Blue originals were 2
int pulsesChanged = 0;
void setup(){
Serial.begin(115200);
Serial.println("k");
pinMode(encoderA, INPUT);
pinMode(encoderB, INPUT);
attachInterrupt(0, A_CHANGE, CHANGE);
attachInterrupt(1, B_CHANGE, CHANGE);
}//setup
void loop(){
if (pulsesChanged != 0) {
pulsesChanged = 0;
Serial.println(pulses);
}
}
void A_CHANGE(){
if( digitalRead(encoderB) == 0 ) {
if ( digitalRead(encoderA) == 0 ) {
// A fell, B is low
pulses--; // moving reverse
} else {
// A rose, B is low
pulses++; // moving forward
}
} else {
if ( digitalRead(encoderA) == 0 ) {
// A fell, B is high
pulses++; // moving forward
} else {
// A rose, B is high
pulses--; // moving reverse
}
}
// Make sure the pulses are between 0 and 359
if (pulses > 359) {
pulses = pulses - 360;
} else if (pulses < 0) {
pulses = pulses + 360;
}
// tell the loop that the pulses have changed
pulsesChanged = 1;
}
void B_CHANGE(){
if ( digitalRead(encoderA) == 0 ) {
if ( digitalRead(encoderB) == 0 ) {
// B fell, A is low
pulses++; // moving forward
} else {
// B rose, A is low
pulses--; // moving reverse
}
} else {
if ( digitalRead(encoderB) == 0 ) {
// B fell, A is high
pulses--; // moving reverse
} else {
// B rose, A is high
pulses++; // moving forward
}
}
// Make sure the pulses are between 0 and 359
if (pulses > 359) {
pulses = pulses - 360;
} else if (pulses < 0) {
pulses = pulses + 360;
}
// tell the loop that the pulses have changed
pulsesChanged = 1;
}
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.