Hello All,
i bought pololu's 29:1 Metal Gearmotor 37Dx52L mm with 64 CPR Encoder (Pololu - 30:1 Metal Gearmotor 37Dx52L mm 12V with 64 CPR Encoder (No End Cap)). I'm driving this motor with Ardumoto and trying to read encoder data by using interrupt.
Firstly, i read the encoder data exactly.However, after sometime i started to read data wrong and strange (i did not make any change on code)
While motor is turning continously in one direction i gain these data;
0,1,1,1,1,2,2,3,4,5,6,7,8,9,10,11,12,13,14,14,14,14,15,15,16,17,18,19,20,21,20,19,19,18,18,17,17,16,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,29,28........
i could not solve the problem
Here is my code:
#define encoder0PinA 2
#define encoder0PinB 3
volatile unsigned int encoder0Pos = 0;
const int sag=12;
const int sol=13;
const int ena=10;
const int enb=11;
void setup()
{
pinMode(sag, OUTPUT);
pinMode(sol, OUTPUT);
pinMode(ena, OUTPUT);
pinMode(enb, OUTPUT);
pinMode(encoder0PinA, INPUT);
digitalWrite(encoder0PinA, HIGH);
pinMode(encoder0PinB, INPUT);
digitalWrite(encoder0PinB, HIGH);
ileri();
Serial.begin (9600);
Serial.println("start");
}
void loop ()
{
attachInterrupt(0, donulan, CHANGE);
}
void donulan(){
if (digitalRead(encoder0PinA) == HIGH) {
if (digitalRead(encoder0PinB) == LOW) {
encoder0Pos = encoder0Pos - 1;
}
else {
encoder0Pos = encoder0Pos + 1;
}
}
else
{
if (digitalRead(encoder0PinB) == LOW) {
// encoder is turning
encoder0Pos = encoder0Pos + 1;
// delay(10);
}
else {
encoder0Pos = encoder0Pos - 1;
// delay(10);
}
}
Serial.println (encoder0Pos, DEC);
}
void ileri (void)
{
digitalWrite(sag, HIGH);
digitalWrite(sol, HIGH);
analogWrite(enb, 50);
analogWrite(ena,40);
}
void dur (void)
{
// digitalWrite(sag,LOW);
//digitalWrite(sol,LOW);
analogWrite(ena,0);
analogWrite(enb,0);
}