Hey guys,
I bought a rotary encoder from sparkfun (
http://www.sparkfun.com/products/9117). I have never worked with rotary, first I thought that it was like a potentiometer, but it doesn't. So my question is: Is it possible to know the direction of the encoder?
I want to built a weather vane using this encoder as a way to know the direction of the wind. (N,S,E,W).
I've this piece of code from
http://www.circuitsathome.com/mcu/programming/reading-rotary-encoder-on-arduino, but I don't understand almost anything, even reading the website which is explained. Maybe it is not the best way to start.
Thank you.
/* 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 )]);
}