I am making my own DIY circuit board that uses two rotary controller. I cant seem to read the values of the rotary encoder correctly. The values that I do read only work in one direction when rotating the encoder. And even these values are only sometimes work.
I have wired the two rotary encoders as in the image attached. With the four resistors as 10k
I am using the encoders WITHOUT breakout boards. So i had to add my own resistors
I have the code that I wrote below. I have only shown the code for one encoder. However, its obvious and easy how to duplicate the code for the 'right' encoder
Im not sure if my problem is the way that I have wired the rotary encoders. Or if it is a coding problem
Any help is greatly appreciated
// rotary encoders
int LeftOutApin = 43;
int LeftOutBpin = 44;
int LeftCounter = 0;
int LeftState;
int LeftLastState;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(LeftOutApin, INPUT);
pinMode(LeftOutBpin, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
LeftRotaryEncoder();
Serial.print("Left: ");
Serial.println(LeftCounter);
}
void LeftRotaryEncoder() {
LeftState = digitalRead(LeftOutApin);
if (LeftState != LeftLastState) {
if (digitalRead(LeftOutBpin) != LeftState) {
LeftCounter ++;
} else {
LeftCounter --;
}
}
LeftLastState = LeftState;
}