encoder and interrupt

hello

i need your advise on reading an encoder accurate.

the encoder should function as a rotary knob (for turning it with the hand, not a motor). incrementing by one dirction, decrementing by the other.

but the readings are inaccurate. while turning the knob, the 'encoder0Pos' variable shows a jumping behaviour. it should count up, but somtimes it counts one or two numbers down despite i did not change the turning direction.

i do know the code on the playground about reading an encoder. at least the code below is a modified version of it.
but neither the version below nor the version on the playground gives proper readings.

a link to the datasheet of the encoder
http://www2.produktinfo.conrad.com/datenblaetter/700000-724999/700708-da-01-en-ENCODER_STEC12E08.pdf

#define encoder0PinA  2
#define encoder0PinB  4

volatile unsigned int encoder0Pos = 100;
int oldEncoder0Pos = 100;


void setup() { 

  pinMode(switchPin, INPUT);
  pinMode(encoder0PinA, INPUT); 
  digitalWrite(encoder0PinA, HIGH);    
  pinMode(encoder0PinB, INPUT); 
  digitalWrite(encoder0PinB, HIGH);    

  attachInterrupt(0, doEncoder, CHANGE);  
  Serial.begin(9600);
  Serial.println("start");                

} 

void loop(){
Serial.println(encoer0Pos);
  delay(100);
}

void doEncoder(){
  if (digitalRead(encoder0PinA) == digitalRead(encoder0PinB)) {
    encoder0Pos = encoder0Pos - 1;        
  } 
  if (digitalRead(encoder0PinA) != digitalRead(encoder0PinB)) {
    encoder0Pos = encoder0Pos + 1; 
  }
}

no one is working with an encoder ?

how to debounce an encoder with interrupt ?

is it possible that it is a debouncing problem?

turning the encoder only to left gives a signature like this:

LLLLLLLLLRRLLLLLLLL(LLL)RLLLL

while every

  • 'L' means one step to the left,
  • 'R' means one to the right and
  • 'L' in brakets that i turned it only one step, but it jumps several steps

sorry, but it is very essential to me.

can anyone share his expierence with encoders, rotary knobs and/or the interrupt function.

thanks,
s