Hi
I just got my hands on 3 Alps Rotary Encoders, but when i use the different sketches, i need to turn the knop "2 times" before the sketch senses that i turn the knop.
I have wired it up like this.
D to ground
C and E to 5v over a 10k ohm resistor and to pins 2 and 3 on my Seeeduino Mega
The signals on the picture is "homemade" using 2 leds and noticing their behavior.
#define encoder0PinA 2
#define encoder0PinB 3
volatile unsigned int encoder0Pos = 0;
unsigned int tmp_Pos = 1;
unsigned int valx;
unsigned int valy;
unsigned int valz;
boolean A_set;
boolean B_set;
void setup() {
pinMode(encoder0PinA, INPUT);
pinMode(encoder0PinB, INPUT);
// encoder pin on interrupt 0 (pin 2)
attachInterrupt(0, doEncoderA, CHANGE);
// encoder pin on interrupt 1 (pin 3)
attachInterrupt(1, doEncoderB, CHANGE);
Serial.begin (9600);
}
void loop(){
//Check each second for change in position
if (tmp_Pos != encoder0Pos) {
Serial.print("Index:"); Serial.print(encoder0Pos, DEC); Serial.print(", Values: ");
Serial.print(valx, DEC); Serial.print(", ");
Serial.print(valy, DEC); Serial.print(", ");
Serial.print(valz, DEC); Serial.println();
tmp_Pos = encoder0Pos;
}
delay(1000);
}
// Interrupt on A changing state
void doEncoderA(){
// Low to High transition?
if (digitalRead(encoder0PinA) == HIGH) {
A_set = true;
if (!B_set) {
encoder0Pos = encoder0Pos + 1;
valx=analogRead(0);
valy=analogRead(1);
valz=analogRead(2);
}
}
// High-to-low transition?
if (digitalRead(encoder0PinA) == LOW) {
A_set = false;
}
}
// Interrupt on B changing state
void doEncoderB(){
// Low-to-high transition?
if (digitalRead(encoder0PinB) == HIGH) {
B_set = true;
if (!A_set) {
encoder0Pos = encoder0Pos - 1;
}
}
// High-to-low transition?
if (digitalRead(encoder0PinB) == LOW) {
B_set = false;
}
}
Best Regards
Morten