Go Down

Topic: Detecting how fast the encoder is turning! (Read 654 times) previous topic - next topic

Zapdeee

Nov 26, 2012, 02:21 am Last Edit: Nov 26, 2012, 06:51 am by Coding Badly Reason: 1
Hi all, i have done this by far:
Code: [Select]
#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! :D

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


Moderator edit: [code] [/code] tags added.

PeterH


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.
I only provide help via the forum - please do not contact me for private consultancy.

Zapdeee

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

monti73

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 :smiley-roll-blue:

Good luck!

WizenedEE

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)

Zapdeee

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!

Nick Gammon

Here's my thread about rotary encoders and interrupts:

http://www.gammon.com.au/forum/?id=11130
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Go Up