Encoder readings

Hello everyone!!
I am using the following motor with encoder:

And the following code (got it from playground):http://playground.arduino.cc/Main/RotaryEncoders

#define encoder0PinA 2

#define encoder0PinB 3


unsigned int encoder0Pos = 0;

void setup() {

  pinMode(encoder0PinA, INPUT); 
  pinMode(encoder0PinB, INPUT); 

// encoder pin on interrupt 0 (pin 2)

  attachInterrupt(0, doEncoderA, CHANGE);

//encoder pin on interrupt 1 (pin 3)

 attachInterrupt(1, doEncoderB, CHANGE);  

  Serial.begin (115200);

}

void loop(){ //Do stuff here 
}

void doEncoderA(){

  // look for a low-to-high on channel A
  if (digitalRead(encoder0PinA) == HIGH) { 

    // check channel B to see which way encoder is turning
    if (digitalRead(encoder0PinB) == LOW) {  
      encoder0Pos = encoder0Pos + 1;         // CW
    } 
    else {
      encoder0Pos = encoder0Pos - 1;         // CCW
    }
  }

  else   // must be a high-to-low edge on channel A                                       
  { 
    // check channel B to see which way encoder is turning  
    if (digitalRead(encoder0PinB) == HIGH) {   
      encoder0Pos = encoder0Pos + 1;          // CW
    } 
    else {
      encoder0Pos = encoder0Pos - 1;          // CCW
    }
  }
  Serial.println (encoder0Pos, DEC);          
  // use for debugging - remember to comment out

}

void doEncoderB(){

  // look for a low-to-high on channel B
  if (digitalRead(encoder0PinB) == HIGH) {   

   // check channel A to see which way encoder is turning
    if (digitalRead(encoder0PinA) == HIGH) {  
      encoder0Pos = encoder0Pos + 1;         // CW
    } 
    else {
      encoder0Pos = encoder0Pos - 1;         // CCW
    }
  }

  // Look for a high-to-low on channel B

  else { 
    // check channel B to see which way encoder is turning  
    if (digitalRead(encoder0PinA) == LOW) {   
      encoder0Pos = encoder0Pos + 1;          // CW
    } 
    else {
      encoder0Pos = encoder0Pos - 1;          // CCW
    }
  }

}

I am able to get some values but for some reason the values jumps. It's like

0,2,4,6,8...and so on

where am I going wrong?

First, it's usually a good idea to use the volatile keyword for any variable used in an interrupt service routine (ISR). Therefore, you should use:

volatile unsigned int encoder0Pos = 0;

Second, make the ISR as short as possible. While the compiler makes the code change for you, you might consider replacing code like:

    if (digitalRead(encoder0PinB) == LOW) {  
      encoder0Pos = encoder0Pos + 1;         // CW
    } 
    else {
      encoder0Pos = encoder0Pos - 1;         // CCW
    }

with the more common:

    if (digitalRead(encoder0PinB) == LOW) {  
      encoder0Pos++;         // CW
    } 
    else {
      encoder0Pos--;         // CCW
    }

Finally, don't use the Serial object in an ISR as the print() methods use their own interrupt blocking routines which can mess things up. These suggestions may not fix your problem, but they are standard ISR concepts.

Serial.println (encoder0Pos, DEC);          
  // use for debugging - remember to comment out

The Serial print statement is only with in the doEncoderA() interrupt function. If you copy the lines to the same position at the end of doEncoderB() you will see the missing counts.

That said, Serial print statements should not be placed inside of the interrupt service routine, because interrupts are disabled inside of an isr and serial print relys on interrupts. It would be a good exercise for you to pull the serial print statement from the isrs and place it within the loop to display when encoder0 position changes.

Thanks a lot econjack and cattledog for your suggestions!!

Solved the problem.

Cheers!!

Can't you simplify doEncoderB() like this?

void doEncoderB(){
  if (digitalRead(encoder0PinB) == digitalRead(encoder0PinA)) { 
      encoder0Pos++;         // CW
    }
    else {
      encoder0Pos--;         // CCW
    }
  }
}