#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Encoder.h>
LiquidCrystal_I2C lcd(0x3F, 16, 2); // I2C address and 16x2 display
Encoder encoder(6, 7); // Rotary encoder pins
enum MenuOption {
COUNTDOWN,
SENSOR_DATA,
LED_CONTROL,
COUNTER,
NUM_OPTIONS
};
int selectedOption = COUNTDOWN;
void setup() {
lcd.init();
lcd.backlight();
lcd.clear();
lcd.print("Select an option:");
lcd.setCursor(0, 1);
lcd.print("> Countdown");
encoder.write(0); // Set the initial encoder value
pinMode(8, INPUT_PULLUP); // Button pin
}
void loop() {
int encoderValue = encoder.read();
int newOption = map(encoderValue, -20, 20, 0, NUM_OPTIONS - 1);
newOption = constrain(newOption, 0, NUM_OPTIONS - 1);
if (newOption != selectedOption) {
// Update the LCD to highlight the selected option
lcd.setCursor(0, selectedOption);
lcd.print(" ");
lcd.setCursor(0, newOption);
lcd.print(">");
selectedOption = newOption;
}
if (digitalRead(8) == LOW) { // Button press
delay(100); // Debounce
handleSelection();
}
}
void handleSelection() {
lcd.clear();
switch (selectedOption) {
case COUNTDOWN:
lcd.print("Countdown Timer");
// Implement your countdown timer logic here
break;
case SENSOR_DATA:
lcd.print("Sensor Data");
// Implement your sensor data display logic here
break;
case LED_CONTROL:
lcd.print("LED Control");
// Implement your LED control logic here
break;
case COUNTER:
lcd.print("Rotary Encoder");
// Implement your rotary encoder counter logic here
break;
}
}