Using SSD1306 OLED and buttons (u8g2 + ezButton)

Hi!
Sadly I am stuck at getting my project to work. I tried to simplyfy my problem as much as I coud for this post:
I want to use an SSD1306 OLED display (with u8g2 libary) and a button (with ezButton libary for deboucing with the internal pull-up resistor).
Using the ezButton libary works fine. Whenever I press the button attatched to pin 7 on my Arduino Nano the serialmonitor prints a message if the button is pressed. BUT: as soon as I use the u8g2 libary (using full buffer mode) the button does not work properly. The button presses are not recognized.
The SSD1306 libary itself works fine - I see the text I want on the screen.
Does anybody know, what I can to to get my button work? Why are these two libaries not compatible? Is there a better debouncing libary for buttons than ezButton?
This is the code:

#include <ezButton.h>
#include <U8g2lib.h>
  U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);   // All Boards without Reset of the Display
#include <Wire.h>

ezButton b_mode(7); // button connected to D7
byte mode = 1; // set menu for startup

void setup() {
Serial.begin(9600);
u8g2.begin();
b_mode.setDebounceTime(50); // [ms]
}

void loop() {
  
  b_mode.loop();
  int b_mode_state = b_mode.getState();

  if(b_mode.isPressed()) {
  Serial.println("mode pressed");
  }



u8g2.clearBuffer();					// clear the internal memory
u8g2.setFont(u8g2_font_6x10_tr);
u8g2.drawStr(12,10,"TEST");
u8g2.sendBuffer();					// transfer internal memory to the display

}

Your code is using software i2c when it should be using hardware i2c. That will really slow things down.

Your code is continually re-drawing the screen over and over, even when nothing has changed.

Result: the Nano is wasting 99% of its time updating the screen and missing button presses.

Thanks for your quick reply!
So you would recommend to set the changing the following: (SW to HW)

[code]
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* clock=/ SCL, / data=/ SDA, / reset=*/ U8X8_PIN_NONE); // All Boards without Reset of the Display
[\code]
Am I right? Or how would you solve the problem? I do not get it just yet?

edit:
just tested it now. while it does improve the situation it still does not work fully. not every button press is recognized and sometimes a press is recognized 3 times...

Ok that's great. You fixed one of the two problems I pointed out.

Would you mind telling me how I could solve this problem? I tried adding a delay after sending to the buffer but still it did not solve the button problem.
Additionally I do want to update the display quite frequently as it is ment to be used as a scale (loadcells with hx711).

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.