Reading Encoder Gray code 2 bit

HI!
I have a encoder:
http://cgi.ebay.com/12mm-Rotary-Encoder-Switch-Flat-Top-Knob-NEW-/220671209494?pt=LH_DefaultDomain_0&hash=item3361077016

I'm trying read the encoder using the code: http://www.arduino.cc/playground/Main/RotaryEncoders :

#define encoder0PinA 2
#define encoder0PinB 3

volatile unsigned int encoder0Pos = 0;

void setup() {

pinMode(encoder0PinA, INPUT);
digitalWrite(encoder0PinA, HIGH); // turn on pullup resistor
pinMode(encoder0PinB, INPUT);
digitalWrite(encoder0PinB, HIGH); // turn on pullup resistor

attachInterrupt(0, doEncoder, CHANGE); // encoder pin on interrupt 0 - pin 2
Serial.begin (9600);
Serial.println("start"); // a personal quirk

}

void loop(){
// do some stuff here - the joy of interrupts is that they take care of themselves
}

void doEncoder() {
/* If pinA and pinB are both high or both low, it is spinning

  • forward. If they're different, it's going backward.
  • For more information on speeding up this process, see
  • [Reference/PortManipulation], specifically the PIND register.
    */
    if (digitalRead(encoder0PinA) == digitalRead(encoder0PinB)) {
    encoder0Pos++;
    } else {
    encoder0Pos--;
    }

Serial.println (encoder0Pos, DEC);
}

/* See this expanded function to get a better understanding of the

  • meanings of the four possible (pinA, pinB) value pairs:
    */
    void doEncoder_Expanded(){
    if (digitalRead(encoder0PinA) == HIGH) { // found a low-to-high on channel A
    if (digitalRead(encoder0PinB) == LOW) { // check channel B to see which way
    // encoder is turning
    encoder0Pos = encoder0Pos - 1; // CCW
    }
    else {
    encoder0Pos = encoder0Pos + 1; // CW
    }
    }
    else // found a high-to-low on channel A
    {
    if (digitalRead(encoder0PinB) == LOW) { // check channel B to see which way
    // encoder is turning
    encoder0Pos = encoder0Pos + 1; // CW
    }
    else {
    encoder0Pos = encoder0Pos - 1; // CCW
    }

}
Serial.println (encoder0Pos); // debug - remember to comment out
Serial.println ('/ '); // before final program run
// you don't want serial slowing down your program if not needed
}

But not is working very well! to right its ok, and to back failed!
See a return in serial monitor:

start
1
2
3
4
5
6
7
8
9
8 ---> here start forward
9
8
9
8

Some one can help me ?

I found this code on a separate site. I can't remember who's code it is, but it works well. Just connect your common wire to grnd, and the other two pins to pin 0 and pin 1 of the analog inputs.

/* Rotary encoder read example */
#define ENC_A 14
#define ENC_B 15
#define ENC_PORT PINC
 
void setup()
{
  /* Setup encoder pins as inputs */
  pinMode(ENC_A, INPUT);
  digitalWrite(ENC_A, HIGH); 
  pinMode(ENC_B, INPUT);
  digitalWrite(ENC_B, HIGH);
  Serial.begin (115200);
  Serial.println("Start"); 
}
 
void loop()
{
 static uint8_t counter = 0;      //this variable will be changed by encoder input      
 int8_t tmpdata;
 /**/
  tmpdata = read_encoder();
  if( tmpdata ) {
    Serial.print("Counter value: ");
    Serial.println(counter, DEC);
    counter += tmpdata;
  }
}
 
/* returns change in encoder state (-1,0,1) */
int8_t read_encoder()
{
  int8_t enc_states[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0}; 
  static uint8_t old_AB = 0;
  /**/
  old_AB <<= 2;                   //remember previous state
  old_AB |= ( ENC_PORT & 0x03 );  //add current state
  return ( enc_states[( old_AB & 0x0f )]);  
}

Rather than nail that code to a specific two pins requiring the use of direct port manipulation, it would be better to replace
"old_AB |= ( ENC_PORT & 0x03 );"
with a pair "digitalReadFast"s (or possibly even plain old "digtalRead"s).

e.g.
old_AB |= ((digitalRead (ENC_B) << 1) | digitalRead (ENC_A));

(uncompiled, untested)