Rotary encoder stops working in combination with U8G2 lib

I'm trying to make a menu, using a rotary encoder. Everything works fine on the serial monitor, but when I add the "U8G2.sendBuffer()" function, the encoder stops working.

I've added an example code that demonstrates the issue:

If the pushbutton is zero, the encoders works perfect.

If it's 1, some U8G2 lines are called and the encoder no longer works.

If it's 2, the same U8G2 lines are called, but the sendbuffer() has been deleted. Now everything works again.

So it's really the sendbuffer() that causes my troubles. Anyone who has a solution/work around?

The library I use are: <MD_REncoder.h> and <U8G2lib.h>.

#include <MD_REncoder.h>
#include <Arduino.h>
#include <U8g2lib.h>


//Rotary encoder & pushbutton
MD_REncoder menu = MD_REncoder(16,17);
int buttonPin = 21;
int buttonState = 0;
int lastButtonState = 0;
int x;

//Display
U8G2_SH1106_128X64_NONAME_F_HW_I2C display1(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);

//Encoder values
int button = 0;
int value = 0;
int value2 = 0;
int value3 = 0;

void setup() {
  
  menu.begin(); //Encoder
  pinMode(buttonPin, INPUT);
  Serial.begin(115200);
  display1.begin();
  display1.setFont(u8g2_font_fub35_tf);
}

void loop() {


//Button code

   buttonState = digitalRead(buttonPin);
   if (buttonState != lastButtonState) {
    if(buttonState == LOW){
      button ++;
      if (button > 2) { button = 0;}
      Serial.print("button= ");
      Serial.println(button);
    }
    delay (50);
   }
  lastButtonState = buttonState;

if(button == 0){         


//Encoder rotation

    x = menu.read();
    if(x){
    if (x==DIR_CW){
      value ++;
      Serial.print("Value: ");
      Serial.println(value);
    } else {
      value --;
      Serial.print("Value: ");
      Serial.println(value);
    }}


}


 if(button == 1){  

    x = menu.read();
    if(x){
    if (x==DIR_CW){
      value2 ++;
      Serial.print("Value2: ");
      Serial.println(value2);
    } else {
      value2 --;
      Serial.print("Value2: ");
      Serial.println(value2);
    }}
   
    display1.clearBuffer();
    display1.setCursor(50,50);
    display1.print(value2);
    display1.sendBuffer();

}

 if(button == 2){  

    x = menu.read();
    if(x){
    if (x==DIR_CW){
      value3 ++;
      Serial.print("Value3: ");
      Serial.println(value3);
    } else {
      value3 --;
      Serial.print("Value3: ");
      Serial.println(value3);
    }}
   
    display1.clearBuffer();
    display1.setCursor(50,50);
    display1.print(value3);


}
}

debug_encoder.ino (1.86 KB)

sendBuffer() actually transfers data to the display. Do the examples that come with the library work? You are using i2c to communicate - what does the i2cbusscanner sketch tell you? Is the chip detected?

The problem is not the display. The problem is that as soon as the sendbuffer() function is added to the code, my encoder rotation is no longer read. I've used both libraries separate without problems. But now that I need them simultaneously I'm having troubles with the encoder rotation not being detected. I don't think this is what you are implying, but just to be certain: the encoder does not use I2C.

This is what I'm trying to show with the example code.

case 1 (button = 1): only encoder reading --> works fine

case 2 (button = 2): encoder reading + send something to display --> displays works, but encoder readings fails

case 3 (button = 3): same code as case 2, but without the sendbuffer() function --> encoder works fine (display obviously doesnt update). Just to show that it is the sendbuffer() function that blocks my encoder readings.

I hope this makes any sense. Thank you for the quick response.

I did not say the encoder uses i2c. I said the display does. If the Wire library hangs trying to do i2c communication, your entire sketch will lock up. It doesn't sound like that is the problem.

What Arduino are you using?

I'm using an adafruit Huzzah32, I'm now trying to attach interupts to the pins of the encoder. This seems to be going somewhere. I think the communication to the display is clogging up everything.

Aren't pins 16 and 17 the Rx/Tx pins for the chip? pinout That will mess up your Serial communications and possibly mess up your encoder once you start using Serial.print()...

They are indeed the Rx/Tx pins. But I'm bound to them because of the custom PCB my project uses. Things to improve in V2.0. Anyway, I've rewritten the code, the encoder is now using interrupts and everything is working as it should. Both encoder and displays.

Thank you for the help/tips!