Time is a technology
From Lahaag - Project wiki
Jump to navigationJump to searchcode for arduino
https://web.archive.org/web/20120105015729/http://wiki.makerbot.com/mre2
using the makerbot magnetic rotary encoder v2.1
here is the code
#define ENCODER_A_PIN 2
#define ENCODER_B_PIN 3
long position;
void setup()
{
Serial.begin(19200);
Serial.println("Started");
pinMode(ENCODER_A_PIN, INPUT);
pinMode(ENCODER_B_PIN, INPUT);
attachInterrupt(0, read_quadrature, CHANGE);
}
void loop()
{
Serial.print("Position: ");
Serial.println(position, 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)
position++;
else
position--;
} // found a high-to-low on channel A
else
{
// check channel B to see which way
if (digitalRead(ENCODER_B_PIN) == LOW)
position--;
else
position++;
}
}