Rotary Encoder
Exemplo de como usar um "Rotary Encoder" com o Arduino:
Esquema geral de ligação ao Arduino:
Existem vários exemplos na Web, o que escolhi e testei foi este:
//http://www.circuitsathome.com/mcu/reading-rotary-encoder-on-arduino
/* 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;
if( counter == 22 ) {
Serial.println("BINGO");}
}
}
/* returns change in encoder state (-1,0,1) */
int8_t read_encoder()
{
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 )]);
}