I'm trying to interface an encoder and a 1602 i2c display. My encoder seems to be giving an output between steps.
I have tried to make my code as simple as possible and am outputting whether the encoder rotated clockwise or counter clockwise to serial. When I rotate the knob one physical step clockwise, '++' (separated by a new line) outputs to the serial monitor when I am expecting just '+'. The same occurs when rotating counter clockwise, except with '--' instead of '++'.
Parts:
- ESP32 Dev Board
- Rotary Encoder
Code:
#define enc1 32
#define enc2 33
#define button 25
#define led 2;
int oldPos1;
int newPos1;
int newPos2;
void setup() {
pinMode(enc1, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(enc1),encRead,CHANGE);
pinMode(enc2, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(enc2),encRead,CHANGE);
Serial.begin(115200);
oldPos1 = digitalRead(enc1);
}
void encRead(){
newPos1 = digitalRead(enc1);
newPos2 = digitalRead(enc2);
if(oldPos1 != newPos1){
if (newPos1 == newPos2){
Serial.println("+");
}
else{
Serial.println("-");
}
}
oldPos1 = newPos1;
}
void loop() {
delay(10);
}
attachInterrupt