Hi
I am having issues with a rotary encoder. Thought I had it sussed out, but tonight is is playing up again.
Its one of those standard cheapy rotary modules.
Don't really want to post all the code... its huge, complicated and now a bit messy.
If I strip out everything, it still doesn't work correctly.
Connected to an ESP32. Button works fine. Held high with 10k, triggers low.
Encoder buttons A and B held high to 3.3v with 10k.
So, I set the interrupt to look for a FALLING signal.
#define rotary_encoder_A_pin 1
#define rotary_encoder_B_pin 3
#define rotary_encoder_button 39
int rotary_state;
int rotary_laststate;
int timevalue;
int oldtimevalue;
----------------------------
In setup I have:
pinMode(rotary_encoder_button, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(rotary_encoder_A_pin), encodermoved, FALLING);
attachInterrupt(digitalPinToInterrupt(rotary_encoder_B_pin), encodermoved, FALLING);
----------------------------
Then a subroutine for the interrupt:
void encodermoved() {
rotary_state = digitalRead(rotary_encoder_A_pin);
if (rotary_state != rotary_laststate) {
if (digitalRead(rotary_encoder_B_pin) != rotary_state) {
timevalue --; if (timevalue < 2) {
timevalue = 120;
}
} else {
timevalue ++; if (timevalue > 120) {
timevalue = 2;
}
}
}
rotary_laststate = rotary_state;
}
------------------------
Finally in the main loop:
if (oldtimevalue != timevalue) {
Serial.println(int(timevalue/2)); // Encoder counts in 2's
oldtimevalue = timevalue;
}
It just doesn't seem to want to count. If you change the interrupt to CHANGE, it does count in both directions, but then gets stuck on certain values (possibly a rotary encoder issue?) and constantly reports the timevalue/2 (so it must be stuck between readings).
Tried several encoders and same result
It counts in 2's, so I have to divide the result.
Couldn't find a library that played nice with the ESP32. Maybe it's the pins I am using?
Weirdly. if I put noInterrupts(); in my main loop, inside a routine that isn't called.... the whole ESP32 hangs.
That routine is only called at a later time when I wanted to disable the rotary encoder.
Not sure why it hangs the code when it's not even called.