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