#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Encoder.h>
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 20 chars and 4 line display
Encoder myEnc(2, 3);
long oldPosition = -999;
int buttonPin = 4; // button input pin
int buttonState = 0; // variable to hold button state
// Menu Items
const char* menuItems[] = {"Option 1", "Option 2", "Option 3"};
int currentMenuItem = 0; // current selected menu item
void setup()
{
lcd.init(); // initialize the lcd
lcd.backlight(); // turn on backlight
pinMode(buttonPin, INPUT); // set the button pin as input
}
void loop()
{
long newPosition = myEnc.read()/4;
if (newPosition != oldPosition) {
oldPosition = newPosition;
currentMenuItem = constrain(newPosition, 0, 2); // limit menu item selection between 0-2
lcd.clear();
lcd.setCursor(0,0); // go to the top left corner
lcd.print(menuItems[currentMenuItem]); // print the current menu item
}
buttonState = digitalRead(buttonPin); // read the button state
if (buttonState == HIGH) { // if button is pressed
lcd.setCursor(0,1); // go to the second line
lcd.print("Selected: ");
lcd.print(menuItems[currentMenuItem]); // print the selected menu item
}
}