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)