#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
// ================== BUTTONY ==================
#define BTN_SET 2
#define BTN_UP 3
#define BTN_DOWN 4
// ================== REŽIMY ==================
uint8_t workMode = 0; // 0 = AUTO, 1 = MAN
int poolTemp = 25; // výchozí hodnota
// ================== MENU STAV ==================
uint8_t menuLevel = 0; // 0 = home, 1 = list, 2 = edit
uint8_t menuGroup = 0; // 0 = AUTO, 1 = MAN, 2 = SET
uint8_t menuIndex = 0;
// ================== MAN OVLÁDÁNÍ ==================
bool stavPump = false;
bool stavHeat = false;
bool stavFilt = false;
bool stpwAction = false; // AUTO/MAN po stopkách
int manTime = 0;
// ================== AUTO MENU ==================
const char* const autMnu[] = {
"Pool temp",
"Filtration",
"Exit MENU"
};
const uint8_t autMnuSize = sizeof(autMnu) / sizeof(autMnu[0]);
// ================== MAN MENU ==================
const char* const manMnu[] = {
"Pump",
"Heating",
"Filtration",
"Stopwatch",
"Stpw action",
"STOP all",
"Exit MENU"
};
const uint8_t manMnuSize = sizeof(manMnu) / sizeof(manMnu[0]);
// ================== SET MENU ==================
const char* const setMnu[] = {
"WIFI",
"TimeDat",
"Daylight",
"Heat circ",
"Pool circ",
"Disp sleep",
"Filt duration",
"RCHK time",
"Firmware upgrade",
"Buzzer frq",
"About SUNREG",
"Exit MENU"
};
const uint8_t setMnuSize = sizeof(setMnu) / sizeof(setMnu[0]);
// ================== AKTUÁLNÍ MENU POINTER ==================
const char* const* currentMenu = nullptr;
uint8_t currentMenuSize = 0;
unsigned long lastPress = 0;
// =======================================================
// LCD FUNKCE
// =======================================================
void homeScreen() {
lcd.clear();
if (workMode == 0) {
lcd.print("POOL 99.9C AUTO");
lcd.setCursor(0, 1);
lcd.print("HEAT 99.9C 27C");
} else {
lcd.print("POOL 88.8C MAN");
lcd.setCursor(0,1);
lcd.print("Qon Qoff Qoff");
}
}
// =======================================================
// MAN MENU – ZOBRAZ AKTIVNÍ POLOŽKU + ON/OFF
// =======================================================
void showManMenu() {
lcd.clear();
// horní řádek (aktivní)
lcd.setCursor(0,0);
lcd.print("");
lcd.print(manMnu[menuIndex]);
// pouze Pump/Heating/Filtration
lcd.setCursor(13,0);
if (menuIndex == 0) lcd.print(stavPump ? "on " : "off");
if (menuIndex == 1) lcd.print(stavHeat ? "on " : "off");
if (menuIndex == 2) lcd.print(stavFilt ? "on " : "off");
lcd.setCursor(12,0);
if (menuIndex == 4) lcd.print(stpwAction ? " MAN" : "AUTO");
// další položka
lcd.setCursor(0,1);
lcd.print("-");
//lcd.print(manMnu[(menuIndex + 1) % manMnuSize]);
if (menuIndex < manMnuSize - 1) {
lcd.print(manMnu[menuIndex + 1]);
} else {
lcd.print(" "); // poslední → prázdno
}
}
// =======================================================
// AUTO nebo SET menu – klasický výpis
// =======================================================
void showAnyMenu() {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("");
lcd.print(currentMenu[menuIndex]);
lcd.setCursor(0,1);
lcd.print("-");
//lcd.print(currentMenu[(menuIndex + 1) % currentMenuSize]);
if (menuIndex < currentMenuSize - 1) {
lcd.print(currentMenu[menuIndex + 1]);
} else {
lcd.print(" "); // za Exit MENU je prázdno
}
}
// =======================================================
// PŘEPÍNÁNÍ ON/OFF V MAN MENU
// =======================================================
void toggleManItem(uint8_t idx) {
if (idx == 0) stavPump = !stavPump;
if (idx == 1) stavHeat = !stavHeat;
if (idx == 2) stavFilt = !stavFilt;
if (idx == 4) stpwAction = !stpwAction;
}
// =======================================================
// VSTUP DO MENU PODLE REŽIMU
// =======================================================
void enterMenu() {
menuLevel = 1;
menuIndex = 0; //resetovat pozici pri vstupu
if (workMode == 0) {
menuGroup = 0;
currentMenu = autMnu;
currentMenuSize = autMnuSize;
showAnyMenu();
} else {
menuGroup = 1;
currentMenu = manMnu;
currentMenuSize = manMnuSize;
showManMenu();
}
}
void enterSetMenu() {
menuGroup = 2;
currentMenu = setMnu;
currentMenuSize = setMnuSize;
menuLevel = 1;
showAnyMenu();
}
// =======================================================
// BUTTON HANDLER
// =======================================================
void buttons() {
if (millis() - lastPress < 230) return;
// ---------------- SET ----------------
if (!digitalRead(BTN_SET)) {
lastPress = millis();
// HOME → vstup
if (menuLevel == 0) {
enterMenu();
return;
}
// LIST → rozhodnutí
if (menuLevel == 1) {
// EXIT
if (strcmp(currentMenu[menuIndex], "Exit MENU") == 0) {
menuLevel = 0;
homeScreen();
return;
}
// AUTO menu – Pool temp editace
if (menuGroup == 0 && menuIndex == 0) { // Pool temp je první položka
menuLevel = 2;
showPoolTempEdit();
return;
}
// MAN menu přehazovačky
if (menuGroup == 1 && (menuIndex == 0 || menuIndex == 1 || menuIndex == 2 || menuIndex == 4)) {
toggleManItem(menuIndex);
showManMenu();
return;
}
// ostatní → vstup do editace
menuLevel = 2;
lcd.clear();
lcd.print("Value edit...");
if (millis() - lastPress < 230) return;
return;
}
// EDIT → návrat
if (menuLevel == 2) {
// pokud editujeme Pool temp
if (menuGroup == 0 && menuIndex == 0) {
menuLevel = 1;
showAnyMenu();
return;
}
// ostatní původní chování
menuLevel = 1;
if (menuGroup == 1) showManMenu();
else showAnyMenu();
return;
}
}
// ---------------- UP ----------------
if (!digitalRead(BTN_UP)) {
lastPress = millis();
if (menuLevel == 2 && menuGroup == 0 && menuIndex == 0) {
if (poolTemp < 32) poolTemp++;
showPoolTempEdit();
return;
}
if (menuLevel == 0) {
if (workMode != 0) {
workMode = 0;
homeScreen();
}
return;
}
//menuIndex = (menuIndex + currentMenuSize - 1) % currentMenuSize;
if (menuIndex > 0) menuIndex--;
if (menuGroup == 1) showManMenu();
else showAnyMenu();
return;
}
// ---------------- DOWN ----------------
if (!digitalRead(BTN_DOWN)) {
lastPress = millis();
if (menuLevel == 0) {
if (workMode != 1) {
workMode = 1;
homeScreen();
}
return;
}
if (menuLevel == 2 && menuGroup == 0 && menuIndex == 0) {
if (poolTemp > 15) poolTemp--;
showPoolTempEdit();
return;
}
//menuIndex = (menuIndex + 1) % currentMenuSize;
if (menuIndex < currentMenuSize - 1) menuIndex++;
if (menuGroup == 1) showManMenu();
else showAnyMenu();
return;
}
}
//nastavovani teploty
void showPoolTempEdit() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Pool temp");
lcd.setCursor(0, 1);
lcd.print("Set value: ");
lcd.setCursor(12, 1);
lcd.print(poolTemp);
lcd.print("C");
}
// =======================================================
// SETUP & LOOP
// =======================================================
void setup() {
lcd.begin(16,2);
lcd.backlight();
pinMode(BTN_SET, INPUT_PULLUP);
pinMode(BTN_UP, INPUT_PULLUP);
pinMode(BTN_DOWN, INPUT_PULLUP);
homeScreen();
}
void loop() {
buttons();
}