Hi, I'm using Arduino UNO and trying to do position control of geared DC motor with encoder.
The following is code to check if the encoder counting is working. (well it's not..)
#define encA 2 // encoder channel A
#define encB 3 // encoder channel B
volatile int enc0 = 0; // initial encoder count #
int en1 = 5; // motor1 enable
int dir1 = 4; // motor1 direction
int pwm1 = 6; // motor1 pwm
void setup() {
pinMode(en1, OUTPUT);
pinMode(dir1, OUTPUT);
pinMode(encA, INPUT);
attachInterrupt(0, doEncoder_Expanded, CHANGE); // do counting if encA CHANGE
pinMode(encB, INPUT);
Serial.begin(9600);
Serial.println("start");
}
void loop(){
Serial.println(enc0);
analogWrite(pwm1, 150);
digitalWrite(en1, HIGH);
digitalWrite(dir1, HIGH);
if (enc0>100){
analogWrite(pwm1, 0);
delay(1000);
enc0 = 0;
}
}
void doEncoder_Expanded() {
if (digitalRead(encA) == HIGH) {
if (digitalRead(encB) == LOW) {
enc0 = enc0 + 1;
}
else {enc0 = enc0 - 1;}
}
else {
if (digitalRead(encB) == LOW) {
enc0 = enc0 - 1;
}
else {enc0 = enc0 + 1;}
}
}
encoder channel A and B are attached to interrupt pins and enc0 is supposed to increase/decrease as motor rotates. But the motor keeps rotating and en0(counting #) is printed as 0 all the time.
Maybe because the resolution of the encoder is 1024 and the geared ratio is 16:1, the Arduino interrupt pins cannot read the signal. (Because of so many countings..)
Is it really the problem of slow interrupt pin rate?
I'm not sure what is the critical problem.. THX