Pages: [1]   Go Down
Author Topic: Detecting how fast the encoder is turning!  (Read 414 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all, i have done this by far:
Code:
#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-grin

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


Moderator edit: [code] [/code] tags added.
« Last Edit: November 26, 2012, 12:51:51 am by Coding Badly » Logged

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11173
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Argentina
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Offline Offline
Edison Member
*
Karma: 17
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here's my thread about rotary encoders and interrupts:

http://www.gammon.com.au/forum/?id=11130
Logged

Pages: [1]   Go Up
Jump to: