#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
const int buttonUpPin = 4;
const int buttonDownPin = 6;
const int buttonSelectPin = 5;
const int led1Pin = 2;
const int led2Pin = 3;
int currentOption = 0;
int totalOptions = 8;
void setup() {
lcd.begin(16, 2);
lcd.print("Select option:");
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
displayMenu();
}
void loop() {
if (digitalRead(buttonUpPin) == HIGH) {
navigateUp();
}
if (digitalRead(buttonDownPin) == HIGH) {
navigateDown();
}
if (digitalRead(buttonSelectPin) == HIGH) {
selectOption();
}
}
void navigateUp() {
currentOption = (currentOption - 1 + totalOptions) % totalOptions;
displayMenu();
delay(200); // Add a small delay to debounce the button
}
void navigateDown() {
currentOption = (currentOption + 1) % totalOptions;
displayMenu();
delay(200); // Add a small delay to debounce the button
}
void selectOption() {
lcd.clear();
lcd.print("Option selected:");
lcd.setCursor(0, 1);
lcd.print("-----------------");
delay(2000);
if (currentOption == 0) {
lcd.clear();
lcd.print("LED Control:");
lcd.setCursor(0, 1);
lcd.print("1: LED1 2: LED2");
int selectedLed = waitForLedSelection(); // Wait for LED selection
if (selectedLed == 1) {
lcd.clear();
lcd.print("LED1 selected");
digitalWrite(led1Pin, HIGH);
delay(2000);
digitalWrite(led1Pin, LOW);
} else if (selectedLed == 2) {
lcd.clear();
lcd.print("LED2 selected");
digitalWrite(led2Pin, HIGH);
delay(2000);
digitalWrite(led2Pin, LOW);
}
}
lcd.clear();
lcd.print("Select option:");
displayMenu();
}
int waitForLedSelection() {
while (true) {
if (digitalRead(buttonUpPin) == HIGH) {
return 1; // Selected LED1
}
if (digitalRead(buttonDownPin) == HIGH) {
return 2; // Selected LED2
}
if (digitalRead(buttonSelectPin) == HIGH) {
return 0; // No LED selected
}
}
}
void displayMenu() {
lcd.setCursor(0, 1);
lcd.print("Option " + String(currentOption + 1));
}