Simple Rotory Encoder Code

I found some pretty simply rotary encoder code I'm trying to get working, but it's not working as expected. I based my sketch from this discussion on HiFiDuino.

I have a RGB rotary encoder from Sparkfun. I've got Encoder pin B connected to D3 and pin A to D2 and encoder pin C to ground on my Arduino Uno. When I run my sketch, I expect to see the encoder value print on the serial monitor every time the value changes, but it doesn't seem to be changing and it's not printing any encoder values. Here's my sketch:

#define ENCODER_A  2
#define ENCODER_B  3
static int encoderval = 0; 
static boolean rotating = false;

void rotEncoder() {
  rotating = true; // If motion is detected in the rotary encoder, set the flag to true
}

void setup() {
  Serial.begin(9600);
  Serial.println("Encoder test");
  pinMode(ENCODER_A, INPUT);
  pinMode(ENCODER_B, INPUT);
  attachInterrupt(0, rotEncoder, CHANGE);  // Attach Interrupt for rotary encoder
}

void loop()  {
   while(rotating)
  {
    delay(2);  // debounce by waiting 2 milliseconds
    if (digitalRead(ENCODER_B) == digitalRead(ENCODER_A))  
    { encoderval--; }  // Clockwise rotation
    else                 
    { encoderval++; }  // Counter-clockwise rotation
    rotating = false; // Reset the flag
    Serial.println(encoderval);  // print encoder counter value
  }  
}

I think this code is more complete, I Don't think your code is nearly complete (From the device page @ Sparkfun) It's a shame you didn't do some searching on your own instead of asking others to do it for you...
From this page... http://www.circuitsathome.com/mcu/programming/reading-rotary-encoder-on-arduino

/* 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()
{
  static 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 )]);
}

There are several different Rotary encoder libraries and sketches... robtillaart has a nice one and so does Nick Gammon
and here is the bildr example it even has PICTURES TOO... Makes it Real Easy..

Doc

Thanks for the code, I'll try it out. I actually did lots of searching on encoder code and tried a few different versions. I also researched debouncing hardware vs software, which is how I came across the code I asked about. My problem is there was a lot of different code and it's hard to tell which was right for my application. This code seemed nice and simple and from the description seemed like it would work well for my purpose, but for some reason, didn't work at all.

Docedison:
I think this code is more complete, I Don't think your code is nearly complete (From the device page @ Sparkfun) It's a shame you didn't do some searching on your own instead of asking others to do it for you...
From this page... http://www.circuitsathome.com/mcu/programming/reading-rotary-encoder-on-arduino

/* 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()
{
 static 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 )]);
}



There are several different Rotary encoder libraries and sketches... robtillaart has a nice one and so does Nick Gammon
and here is the bildr example it even has PICTURES TOO... Makes it Real Easy..

Doc