And here's an example of how to read the encoders
#define encoder0PinA 16 // encoder 1
#define encoder0PinB 25
#define encoder1PinA 17 // encoder 2
#define encoder1PinB 26
// Define motor control pins
// M1 Pin 2, Pin 4 and M2 Pin 13, Pin 27
#define m1PinA 2
#define m1PinB 4
#define m2PinA 13
#define m2PinB 27
volatile long encoder0Pos = 0; // encoder 1
volatile long encoder1Pos = 0; // encoder 2
unsigned long currentMillis;
unsigned long prevMillis;
void setup() {
Serial.begin(115200);
pinMode(encoder0PinA, INPUT_PULLUP); // encoder pins
pinMode(encoder0PinB, INPUT_PULLUP);
pinMode(encoder1PinA, INPUT_PULLUP);
pinMode(encoder1PinB, INPUT_PULLUP);
// Set motor control pins to output
pinMode(m1PinA, OUTPUT);
pinMode(m1PinB, OUTPUT);
pinMode(m2PinA, OUTPUT);
pinMode(m2PinB, OUTPUT);
// Serial.println("Basic Encoder Test:");
attachInterrupt(digitalPinToInterrupt(encoder0PinA), change_left_a, CHANGE);
attachInterrupt(digitalPinToInterrupt(encoder0PinB), change_left_b, CHANGE);
attachInterrupt(digitalPinToInterrupt(encoder1PinA), change_right_a, CHANGE);
attachInterrupt(digitalPinToInterrupt(encoder1PinB), change_right_b, CHANGE);
}
void loop() {
Serial.print(encoder0Pos);
Serial.print(",");
Serial.println(encoder1Pos);
}
// ************** encoders interrupts **************
// ************** encoder 1 *********************
void change_left_a(){
// look for a low-to-high on channel A
if (digitalRead(encoder0PinA) == HIGH) {
// check channel B to see which way encoder is turning
if (digitalRead(encoder0PinB) == LOW) {
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
else // must be a high-to-low edge on channel A
{
// check channel B to see which way encoder is turning
if (digitalRead(encoder0PinB) == HIGH) {
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
}
void change_left_b(){
// look for a low-to-high on channel B
if (digitalRead(encoder0PinB) == HIGH) {
// check channel A to see which way encoder is turning
if (digitalRead(encoder0PinA) == HIGH) {
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
// Look for a high-to-low on channel B
else {
// check channel B to see which way encoder is turning
if (digitalRead(encoder0PinA) == LOW) {
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
}
// ************** encoder 2 *********************
void change_right_a(){
// look for a low-to-high on channel A
if (digitalRead(encoder1PinA) == HIGH) {
// check channel B to see which way encoder is turning
if (digitalRead(encoder1PinB) == LOW) {
encoder1Pos = encoder1Pos - 1; // CW
}
else {
encoder1Pos = encoder1Pos + 1; // CCW
}
}
else // must be a high-to-low edge on channel A
{
// check channel B to see which way encoder is turning
if (digitalRead(encoder1PinB) == HIGH) {
encoder1Pos = encoder1Pos - 1; // CW
}
else {
encoder1Pos = encoder1Pos + 1; // CCW
}
}
}
void change_right_b(){
// look for a low-to-high on channel B
if (digitalRead(encoder1PinB) == HIGH) {
// check channel A to see which way encoder is turning
if (digitalRead(encoder1PinA) == HIGH) {
encoder1Pos = encoder1Pos - 1; // CW
}
else {
encoder1Pos = encoder1Pos + 1; // CCW
}
}
// Look for a high-to-low on channel B
else {
// check channel B to see which way encoder is turning
if (digitalRead(encoder1PinA) == LOW) {
encoder1Pos = encoder1Pos - 1; // CW
}
else {
encoder1Pos = encoder1Pos + 1; // CCW
}
}
}