//http://domoticx.com/arduino-rotary-encoder-keyes-ky-040/
int clock = 2; // Define encoder pin A
int data = 3; // Define encoder pin B
int count = 0; // pre-init the count to zero
int c = LOW; // pre-init the state of pin A low
int cLast = LOW; // and make its last val the same - ie no change
int d = LOW; // and make the data val low as well
void setup() {
pinMode (clock,INPUT); // setup the pins as inputs
pinMode (data,INPUT);
Serial.begin (9600); // and give some serial debugging
}
void loop() {
c = digitalRead(clock); // read pin A as clock
d = digitalRead(data); // read pin B as data
if (c != cLast) { // clock pin has changed value... now we can do stuff
d = c^d; // work out direction using an XOR
if ( d ) {
count--; // non-zero is Anti-clockwise
}else{
count++; // zero is therefore anti-clockwise
}
Serial.print ("Jog:: count:");
Serial.println(count);
cLast = c; // store current clock state for next pass
}
}
Comments