I'm trying to detect the direction of rotation for an incremental rotary encoder. I'm supposed to get +1 for clockwise and -1 for counter-clockwise. However, I receive random values. Any help appreciated.
Following is the code.
const int encoderPinA = 7;
const int encoderPinB = 12;
int val;
int encoderPos = 0;
boolean encoderALast = LOW; // remember the previous pin state
void setup() {
pinMode(encoderPinA,INPUT);
pinMode(encoderPinB,INPUT);
Serial.begin(9600);
}
void loop() {
analogWrite(3,250); //powering motor
val = encoder_direction();
// receive the direction for clockwise 1 and for counter clock wise -1
Serial.println(val); // print it in serial monitor
}
int encoder_direction() {
boolean encoderA = digitalRead(encoderPinA);
if ((encoderALast == HIGH) && (encoderA == LOW)) {
if (digitalRead(encoderPinB)) {
//encoderPos --;
encoderPos = -1;
} else {
//encoderPos ++;
encoderPos = 1;
}
return encoderPos;
// return the value either 1 or -1
}
encoderALast = encoderA;
}
// using digitalread() code for four encoders
const int ENCODERS = 4; // the number of encoders
const int encoderPinA[ENCODERS] = { 7, 8, A1 , 2 };
// encoderA pins on 2 and 4
const int encoderPinB[ENCODERS] = { 12, 5, 4 , A2 };
// encoderB pins on 3 and 5
int encoderPos[ ENCODERS] = { 0, 0, 0, 0 };
// initialize the positions to 0
boolean encoderALast[ENCODERS] = { LOW, LOW, LOW, LOW };
// holds last state of encoderA pin
void setup() {
Serial.begin (9600);
}
int updatePosition(int encoderIndex) {
boolean encoderA = digitalRead(encoderPinA[encoderIndex]);
if ((encoderALast[encoderIndex] == HIGH) && (encoderA == LOW)) {
if (digitalRead(encoderPinB[encoderIndex]) == LOW) {
//encoderPos[encoderIndex]--;
encoderPos[encoderIndex] = -1;
} else {
//encoderPos[encoderIndex]++;
encoderPos[encoderIndex] = 1;
}
Serial.print("Encoder ");
Serial.print(encoderIndex+1,DEC);
Serial.print("=");
Serial.println (encoderPos[encoderIndex]);
}
encoderALast[encoderIndex] = encoderA;
}
void loop() {
updatePosition(0);
updatePosition(1);
updatePosition(2);
updatePosition(3);
}
// using direct access to digitalread code in order to process faster
int val[4];
int encoderPos = 0;
boolean encoderALast[4] ={LOW, LOW, LOW, LOW};
// remember the previous pin state
void setup() {
Serial.begin(9600);
}
void loop() {
for (int ii=0; ii<4;ii++) {
encoder_direction(ii);
}
}
int encoder_direction(int m) {
boolean encoderA;
boolean encoderB;
if(m == 0) {
encoderA = (PIND & _BV (7)) == 0;
// digitalRead(7) encoderPinA
encoderB = (PINB & _BV (4)) == 0;
// digitalRead(12) encoderPinB
}
if (m == 1) {
encoderA = (PINB & _BV (0)) == 0;
// digitalRead(8) encoderPinA
encoderB = (PIND & _BV (5)) == 0;
// digitalRead(5) encoderPinB
}
if (m == 2) {
encoderA = (PINC & _BV (1)) == 0;
// digitalRead(A1) encoderPinA
encoderB = (PIND & _BV (4)) == 0;
// digitalRead(4) encoderPinB
}
if (m == 3) {
encoderA = (PIND & _BV (2)) == 0;
// digitalRead(2) encoderPinA
encoderB = (PINC & _BV (2)) == 0;
// digitalRead(A2) encoderPinB
}
if ((encoderALast[m] == HIGH) && (encoderA == LOW)) {
if (encoderB == LOW) {
//encoderPos --;
encoderPos = -1;
} else {
//encoderPos ++;
encoderPos = 1;
}
Serial.print(" Encoder ");
Serial.print(m+1);
Serial.print(" = ");
Serial.println(encoderPos);
}
encoderALast[m] = encoderA;
}