Page 1 of 1

Adding second encoder

Posted: Fri Jun 12, 2020 6:30 am
by Crispi
Newbie question.

I have one EC11 encoder connected to the ESP32. It works as expected. When I try to add another, the send one doesn't work. I've looked over my code but I cannot see why. I'm new to the ESP32 and hoped maybe someone could share some light on why only 1 encoder works. Code for second encoder is just first encoder copied and added some identifiers.

I use the bleGamepad library so I can use it wirelessly.

Thanks.

Code: Select all

#include <BleGamepad.h> 
#include <Rotary.h>

BleGamepad bleGamepad;

Rotary r1 = Rotary(22, 23); // first encoder
Rotary r2 = Rotary(2, 4); // second encoder

int previousButton1State = HIGH; // one button currently

void setup() {
  pinMode(21, INPUT_PULLUP); // code for sinlge button
  r1.begin(true); // first encoder
  r2.begin(true); // second encoder
  bleGamepad.begin(); // start wirelessly 
  // Serial.begin(115200); // troubleshooting if needed
}

void loop() {
  if(bleGamepad.isConnected()) {

    int currentButton1State = digitalRead(21);

    if (currentButton1State != previousButton1State)
    {
      if(currentButton1State == LOW)
      {
        bleGamepad.press(BUTTON_1);
      }
      else
      {
        bleGamepad.release(BUTTON_1);
      }
    }
    previousButton1State = currentButton1State;
  }

// ENCODER1 -----------------------
    unsigned char result1 = r1.process();
  if (result1 == DIR_CCW) {

    bleGamepad.press(BUTTON_11); // presses button 11
    delay(100);					// wait and release
    bleGamepad.release(BUTTON_11); // release
    
  } else if (result1 == DIR_CW){

    bleGamepad.press(BUTTON_12);
    delay(100);
    bleGamepad.release(BUTTON_12);

// ENCODER2 -------------------------

    unsigned char result2 = r2.process();
  if (result2 == DIR_CCW) {

    bleGamepad.press(BUTTON_13);
    delay(100);
    bleGamepad.release(BUTTON_13);
    
  } else if (result2 == DIR_CW){

    bleGamepad.press(BUTTON_14);
    delay(100);
    bleGamepad.release(BUTTON_14);
    
  }
  }
  }