#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define nextBtn 12
#define selectBtn 3
struct Relay {
const char* name;
int pin;
};
Relay relays[] = {
{"Relay1", 13},
{"Relay2", 11},
};
unsigned long lastPress = 0;
const unsigned long pressDelay = 200;
const char* menuItems[] = {"Relay-1", "Relay-2"};
const char* subMenuItems[] = {"Relay-1 State", "Relay-2 State"};
int menulength = 1;
int currentitem = 0;
int currentSubitem = 0;
bool onSub = false;
void setup() {
pinMode(nextBtn, INPUT_PULLUP);
pinMode(selectBtn, INPUT_PULLUP);
digitalWrite(relays[0].pin, 1);
digitalWrite(relays[1].pin, 0);
lcd.init();
lcd.backlight();
lcd.clear();
drawmenu();
}
void loop() {
unsigned long currentMillis = millis();
if (digitalRead(nextBtn) == LOW && onSub == false) {
if (currentMillis - lastPress >= pressDelay) {
lastPress = currentMillis;
if (currentitem < menulength) {
currentitem++;
drawmenu();
}
else {
currentitem = 0;
drawmenu();
}
}
}
if (digitalRead(selectBtn) == LOW) {
if (currentMillis - lastPress >= pressDelay) {
lastPress = currentMillis;
if (onSub == false) {
drawSubMenu();
onSub = true;
}
else {
drawmenu();
onSub = false;
}
}
}
}
void drawmenu() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(">");
lcd.setCursor(1, 0);
lcd.print(menuItems[currentitem]);
if (currentitem < menulength) {
lcd.setCursor(1, 1);
lcd.print(menuItems[currentitem+1]);
}
}
void drawSubMenu() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(subMenuItems[currentitem]);
lcd.setCursor(0, 1);
lcd.print(relayCheck(relays[currentitem].pin));
}
const char* relayCheck(int relayPin) {
return digitalRead(relayPin) ? "ON" : "OFF";
}