hello
i need your advise on reading an encoder accurate.
the encoder should function as a rotary knob (for turning it with the hand, not a motor). incrementing by one dirction, decrementing by the other.
but the readings are inaccurate. while turning the knob, the 'encoder0Pos' variable shows a jumping behaviour. it should count up, but somtimes it counts one or two numbers down despite i did not change the turning direction.
i do know the code on the playground about reading an encoder. at least the code below is a modified version of it.
but neither the version below nor the version on the playground gives proper readings.
a link to the datasheet of the encoder
http://www2.produktinfo.conrad.com/datenblaetter/700000-724999/700708-da-01-en-ENCODER_STEC12E08.pdf
#define encoder0PinA 2
#define encoder0PinB 4
volatile unsigned int encoder0Pos = 100;
int oldEncoder0Pos = 100;
void setup() {
pinMode(switchPin, INPUT);
pinMode(encoder0PinA, INPUT);
digitalWrite(encoder0PinA, HIGH);
pinMode(encoder0PinB, INPUT);
digitalWrite(encoder0PinB, HIGH);
attachInterrupt(0, doEncoder, CHANGE);
Serial.begin(9600);
Serial.println("start");
}
void loop(){
Serial.println(encoer0Pos);
delay(100);
}
void doEncoder(){
if (digitalRead(encoder0PinA) == digitalRead(encoder0PinB)) {
encoder0Pos = encoder0Pos - 1;
}
if (digitalRead(encoder0PinA) != digitalRead(encoder0PinB)) {
encoder0Pos = encoder0Pos + 1;
}
}