Detecting how fast the encoder is turning!

Hi all, i have done this by far:

#define ENC_A 14
#define ENC_B 15
#define ENC_PORT PINC
 
void setup()
{
  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;      
 int8_t tmpdata;
  tmpdata = read_encoder();
  if( tmpdata ) {
    Serial.print("Counter value: ");
    Serial.println(counter, DEC);
    counter += tmpdata;
  }
}
 
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;                  
  old_AB |= ( ENC_PORT & 0x03 );  
  return ( enc_states[( old_AB & 0x0f )]);
}

It works fine and everything but the next part to my project is to detect how fast my encoder is turning so my motor can react accordingly.
The arduino board im using is a Arduino UNO board and the encoder im using is this:
http://singapore.rs-online.com/web/p/optical-rotary-encoders/6633230/

Any kind of help would be appreciated! :smiley:

P.S. I'm kind of new here so pardon me for any mistakes i made in posting!

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

Zapdeee:
P.S. I'm kind of new here so pardon me for any mistakes i made in posting!

To start with, only ask your question once.

Sorry about that. So do you have any idea on how to do?

Hey, I don't know nothing about what you are asking, but I can give a few tips to your post... Insert the code between CODE tags with the # button, and also comment the code so it would be easier to another person to understand what you are doing, and then is more probable that someone can help you.

Sorry for not answering your question :roll_eyes:

Good luck!

Speed is change in distance over change in time.

With which one of those are you having trouble?
(the current time can be obtained with the millis() function)

Yes i know about the distance over time formula but i dont know how to infuse it with my code. May i see an example? Thank you for the reply!

Here's my thread about rotary encoders and interrupts: