#define encoder0PinA A0 // encoder 1
#define encoder0PinB A1
#define encoder1PinA A2 // encoder 2
#define encoder1PinB A3
unsigned long currentMillis;
unsigned long previousArmMillis;
unsigned long previousMillis;
volatile long encoder0Pos = 0; // encoder 1
volatile long encoder1Pos = 0; // encoder 2
void setup() {
// put your setup code here, to run once:
pinMode(encoderLPinA, INPUT_PULLUP); // encoder pins
pinMode(encoderLPinB, INPUT_PULLUP);
pinMode(encoderRPinA, INPUT_PULLUP);
pinMode(encoderRPinB, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(encoder0PinA), doEncoder0A, CHANGE);
attachInterrupt(digitalPinToInterrupt(encoder0PinB), doEncoder0B, CHANGE);
attachInterrupt(digitalPinToInterrupt(encoder1PinA), doEncoder1A, CHANGE);
attachInterrupt(digitalPinToInterrupt(encoder1PinB), doEncoder1B, CHANGE);
}
void loop() {
// put your main code here, to run repeatedly:
currentMillis = millis(); // bookmark the time
if (currentMillis - previousMillis >= 10) { // start timed loop for everything else
previousMillis = currentMillis;
Serial.print(encoder0Pos);
Serial.print(",");
Serial.println(encoder1Pos);
}
}
void doEncoder0A(){
// 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 doEncoder0B(){
// 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 doEncoder1A(){
// 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 doEncoder1B(){
// 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
}
}
}