Wiring for incremental mode is shown below:
Arduino code:
/*
This sketch was developed buy Ing. Dmitry Kochubey
in Technical university of Liberec
Czech Republic
year 2013
*/
#define ENCODER_A_PIN 2
#define ENCODER_B_PIN 3
long pos = 0;
void setup()
{
pinMode(ENCODER_A_PIN, INPUT);
pinMode(ENCODER_B_PIN, INPUT);
attachInterrupt(0, read_quadrature, CHANGE);
Serial.begin(9600);
}
void loop()
{
Serial.print("Position: ");
Serial.println(pos, DEC);
delay(1000);
}
void read_quadrature()
{
// found a low-to-high on channel A
if (digitalRead(ENCODER_A_PIN) == HIGH)
{
// check channel B to see which way
if (digitalRead(ENCODER_B_PIN) == LOW)
pos++;
else
pos--;
}
// found a high-to-low on channel A
else
{
// check channel B to see which way
if (digitalRead(ENCODER_B_PIN) == LOW)
pos--;
else
pos++;
}
}
Enjoy!
No comments:
Post a Comment