Arduino and Pololu 64 CPR Encoder Problem

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 :frowning:

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);
}

It sounds like you are missing encoder pulses because your software cannot keep up with the faster-and-faster pulses.

See this thread from yesterday on a similar issue and my suggestions for how to improve things:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1282398133/12#12

Also, there's no need to put attachInterrupt() in the loop() function. It should go in setup().

--
Check out our new shield: http://www.ruggedcircuits.com/html/gadget_shield.html

Thank you for your reply. I think i undestood reason of the problem. If arduino do not make any serial communication it can read the exact encoder data. I tried it without any serial connection i worked. However, i have to send some data via serial communication. Do u have any sugestion?

You can't send serial data from an interrupt handler. In the handler, you should just increment or decrement the counter.

Serial transmission of the encoder count should be done in loop, at appropriate times.

If you are trying to communicate real time with another system, and keep it informed of every encoder change, you are using the wrong hardware.

i know the restriction of arduino but i was not sure. As a hardware what do u recommend? I think that u r mentioning about some hardwares that have ability of multi-tasking?