#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Encoder.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 16
#define LCD_LINES 2
#define SDA 17
#define SCL 16
#define ENCODER_CLK 13
#define ENCODER_DT 14
#define ENCODER_SW 27
LiquidCrystal_I2C lcd( I2C_ADDR, LCD_COLUMNS, LCD_LINES); // Set the I2C address and LCD dimensions
Encoder encoder(ENCODER_CLK, ENCODER_DT); // Connect rotary encoder pins to A0 and A1
int menuIndex = 1;
int lastClk = HIGH;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32-S2!");
//This call for the wire lib, allows the esp32 to establish connection with the pins of the lcd that receives data
Wire.begin(SDA,SCL);
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
pinMode(ENCODER_CLK, INPUT);
pinMode(ENCODER_DT, INPUT);
attachInterrupt(digitalPinToInterrupt(ENCODER_CLK), readEncoder, FALLING);
lcd.print("Arduino Setup");
delay(2000);
lcd.clear();
}
void readEncoder() {
int dtValue = digitalRead(ENCODER_DT);
if (dtValue == HIGH) {
Serial.println("Rotated clockwise ⏩");
}
if (dtValue == LOW) {
Serial.println("Rotated counterclockwise ⏪");
}
}
void loop() {
lcd.setCursor(0, 0);
lcd.print("Menu Option: ");
lcd.setCursor(0, 1);
//This needs to be improved to better track the values of the encoder
int newClk = digitalRead(ENCODER_CLK);
if (newClk != lastClk) {
// There was a change on the CLK pin
lastClk = newClk;
int dtValue = digitalRead(ENCODER_DT);
if (newClk == LOW && dtValue == HIGH) {
Serial.println("Rotated clockwise ⏩");
menuIndex++;
}
if (newClk == LOW && dtValue == LOW) {
Serial.println("Rotated counterclockwise ⏪");
menuIndex--;
}
}
//
switch (menuIndex) {
case 1:
lcd.print("Option 1");
break;
case 2:
lcd.print("Option 2");
break;
case 3:
lcd.print("Option 3");
break;
// Add more cases for additional options as needed
}
int encoderValue = encoder.read();
if (encoderValue == HIGH ) {
menuIndex = 1;
} else if (encoderValue == LOW) {
menuIndex = 2; // Adjust based on the number of menu options
} else {
menuIndex = 3;
}
delay(10); // Adjust the delay based on your application
}