#include <U8g2lib.h>
#include <Encoder.h>
#include <Bounce2.h>
// Pins für den Rotary Encoder
Encoder myEnc(2, 3);
// Pin für den Push-Button
const int buttonPin = 8;
Bounce2::Button button;
int ButtonPushed = 0;
long oldPosition = 0;
int _Menue = 1;
const int _MenueSlides = 4; // Grenze für die Zahl (1-4)
const int stepsPerIncrement = 4; // Anzahl der Encoder-Schritte pro Zähleränderung
// Display-Setup
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
void setup() {
Serial.begin(9600);
Serial.println("Encoder mit Push-Button Test:");
// Konfiguration des Push-Buttons
button.attach(buttonPin, INPUT_PULLUP);
button.interval(10); // Entprell-Intervall in Millisekunden
// Initialisiere die Startposition
myEnc.write(0);
// Initialisiere das Display
u8g2.begin();
u8g2.enableUTF8Print(); // enable UTF8 support for the Arduino print() function
}
void loop() {
updateEncoder();
menuSelection();
Button ();
//delay(10); // Vermeidung übermäßiger CPU-Auslastung
}
void Button (){
// Aktualisiere den Button-Status
button.update();
// Überprüfe, ob der Button gedrückt wurde
if (button.fell()) {
ButtonPushed = ButtonPushed +1;
Serial.print("Button: ");
Serial.println(ButtonPushed);
if (ButtonPushed == 2)
ButtonPushed = 0;
}
}
void updateEncoder() {
long newPosition = myEnc.read();
if ((newPosition - oldPosition) >= stepsPerIncrement || (newPosition - oldPosition) <= -stepsPerIncrement) {
if (newPosition > oldPosition) {
_Menue++;
} else {
_Menue--;
}
// Begrenzung des Zählers zwischen 1 und _MenueSlides
if (_Menue > _MenueSlides) _Menue = 1;
if (_Menue < 1) _Menue = _MenueSlides;
oldPosition = newPosition; // Aktualisiere die alte Position
Serial.print("Counter: ");
Serial.println(_Menue);
}
}
void menuSelection(){ // Auswahl des Menüs
if (_Menue == 1){
displayMain1();
while (ButtonPushed == 1){ // Menü fixieren wenn knopf gedrückt
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_ncenB14_tr); // choose a suitable font
u8g2.setCursor(0, 30); // position the cursor
u8g2.print("Button Pushed"); // print the current number
u8g2.sendBuffer(); // transfer internal memory to the display
Button ();
}
}
else if (_Menue == 2) {
displayMain2();
}
else if (_Menue == 3) {
displayMain3();
}
else if (_Menue == 4) {
displayMain4();
}
}
void displayMain1() {
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_ncenB14_tr); // choose a suitable font
u8g2.setCursor(50, 30); // position the cursor
u8g2.print("Menu 1"); // print the current number
u8g2.sendBuffer(); // transfer internal memory to the display
}
void displayMain2() {
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_ncenB14_tr); // choose a suitable font
u8g2.setCursor(50, 30); // position the cursor
u8g2.print("Menu 2"); // print the current number
u8g2.sendBuffer(); // transfer internal memory to the display
}
void displayMain3() {
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_ncenB14_tr); // choose a suitable font
u8g2.setCursor(50, 30); // position the cursor
u8g2.print("Menu 3"); // print the current number
u8g2.sendBuffer(); // transfer internal memory to the display
}
void displayMain4() {
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_ncenB14_tr); // choose a suitable font
u8g2.setCursor(50, 30); // position the cursor
u8g2.print("Menu 4"); // print the current number
u8g2.sendBuffer(); // transfer internal memory to the display
}
void displayNumber() {
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_ncenB14_tr); // choose a suitable font
u8g2.setCursor(50, 30); // position the cursor
u8g2.print(_Menue); // print the current number
u8g2.sendBuffer(); // transfer internal memory to the display
}