/*
ESP32 Beispiel für ROTORY ENCODER mit I2C-Display
Verwendung: Drehen Sie den ROTORY ENCODER, um den Wert zu steuern,
Klicken Sie auf den Knopf und COUNTER wird in WERT gespeichert
*/
//===============================================
#include <LiquidCrystal_I2C.h>
// Stellen Sie die LCD-Anzahl der Spalten und Zeilen ein
int lcdColumns = 20;
int lcdRows = 4;
// LCD-Adresse, Anzahl der Spalten und Zeilen einstellen
// Wenn Sie Ihre Anzeigeadresse nicht kennen, führen Sie einen I2C-Scanner-Sketch aus
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);
#define ENCODER_CLK 2
#define ENCODER_DT 4
#define ENCODER_SW 5
// Rotary Encoder Inputs
#define Clock 2 //Clock pin connected to D9
#define Data 4 //Data pin connected to D8
#define Push 5 //Push button pin connected to D10
int WERT ;
int counter = 0; //Use this variable to store "steps"
int currentStateClock; //Store the status of the clock pin (HIGH or LOW)
int lastStateClock; //Store the PREVIOUS status of the clock pin (HIGH or LOW)
String currentDir =""; //Use this to print text
unsigned long lastButtonPress = 0; //Use this to store if the push button was pressed or not
void setup() {
lcd.init();
lcd.backlight();
/* Set encoder pins as inputs with pullups. If you use the Encoder Module, you don't need
* pullups for Clock and Data, only for the push button.*/
pinMode(Clock,INPUT_PULLUP);
pinMode(Data,INPUT_PULLUP);
pinMode(Push, INPUT_PULLUP);
// Setup Serial Monitor
Serial.begin(9600);
// Read the initial state of Clock pin (it could be HIGH or LOW)
lastStateClock = digitalRead(Clock);
}
void loop() {
// Read the current state of CLK
currentStateClock = digitalRead(Clock);
// If last and current state of Clock are different, then "pulse occurred"
// React to only 1 state change to avoid double count
if (currentStateClock != lastStateClock && currentStateClock == 1){
// If the Data state is different than the Clock state then
// the encoder is rotating "CCW" so we decrement
if (digitalRead(Data) != currentStateClock) {
counter --;
currentDir ="Counterclockwise";
} else {
// Encoder is rotating CW so increment
counter ++;
currentDir ="Clockwise";
}
//(WERT) = (counter); //COUNTER wird in WERT gespeichert
lcd.clear();
//lcd.setCursor(0, 0);
//lcd.print("Direction: ");
//lcd.setCursor(0, 1);
//lcd.print(currentDir);
lcd.setCursor(0, 0);
lcd.print("Counter: ");
lcd.setCursor(10, 0);
lcd.println(counter);
lcd.setCursor(0, 1);
lcd.print("WERT: ");
lcd.setCursor(10, 1);
lcd.println(WERT);
Serial.print("Direction: ");
Serial.print(currentDir);
Serial.print(" | Counter: ");
Serial.println(counter);
}
// Wir speichern den letzten Taktzustand für die nächste Schleife
lastStateClock = currentStateClock;
// Lesen Sie den Zustand des Tasters
int btnState = digitalRead(Push);
// Wenn wir ein LOW-Signal erkennen, wird die Taste gedrückt
if (btnState == LOW) {
// wenn seit dem letzten LOW-Impuls 50 ms vergangen sind, bedeutet dies, dass die
//Taste wurde gedrückt, losgelassen und erneut gedrückt
if (millis() - lastButtonPress > 50) {
Serial.println("Button pressed!");
(WERT) = (counter); //COUNTER wird in WERT gespeichert
lcd.setCursor(0, 1);
lcd.print("WERT: ");
lcd.setCursor(10, 1);
lcd.println(WERT);
}
// Erinnere dich an das letzte Tastendruckereignis
lastButtonPress = millis();
}
// Setzen Sie eine leichte Verzögerung ein, um den Messwert zu entprellen
delay(1);
}