#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Encoder.h>
#include <EEPROM.h>
#include "LiquidMenu_config.h"
#include <LiquidMenu.h>
// Configuración del LCD I2C
LiquidCrystal_I2C lcd(0x27, 16, 2); // Ajusta la dirección I2C si es necesario
// Configuración del encoder rotativo
Encoder encoder(2, 3); // Pines para el encoder
const int buttonPin = 4; // Pin para el botón del encoder
int buttonState = HIGH;
int lastButtonState = HIGH;
long oldPosition = -999;
// Variables para almacenar estados y valores
int bomba1State = 0; // 0: OFF, 1: ON
int bomba1OnValue = 0;
int bomba1OffValue = 0;
int bomba2Tiempo = 0;
int bomba2Velocidad = 0;
// Identificadores de EEPROM
const int EEPROM_BOMBA1_STATE = 0;
const int EEPROM_BOMBA1_ON_VALUE = 1;
const int EEPROM_BOMBA1_OFF_VALUE = 2;
const int EEPROM_BOMBA2_TIEMPO = 3;
const int EEPROM_BOMBA2_VELOCIDAD = 4;
// Definición de los menús
LiquidLine line1(0, 0, "Hola Mundo");
LiquidScreen mainScreen(line1);
LiquidLine line2_1(0, 0, "Bomba 1");
LiquidLine line2_2(0, 1, "Bomba 2");
LiquidScreen selectBombaScreen(line2_1, line2_2);
LiquidLine bomba1Line1(0, 0, "Bomba 1 ON");
LiquidLine bomba1Line2(0, 1, "Bomba 1 OFF");
LiquidScreen bomba1Screen(bomba1Line1, bomba1Line2);
LiquidLine bomba1OnLine(0, 0, "Valor ON: ", bomba1OnValue);
LiquidScreen bomba1OnScreen(bomba1OnLine);
LiquidLine bomba1OffLine(0, 0, "Valor OFF: ", bomba1OffValue);
LiquidScreen bomba1OffScreen(bomba1OffLine);
LiquidLine bomba2Line1(0, 0, "Tiempo: ", bomba2Tiempo);
LiquidLine bomba2Line2(0, 1, "Velocidad: ", bomba2Velocidad);
LiquidScreen bomba2Screen(bomba2Line1, bomba2Line2);
// Definición del menú
LiquidMenu menu(lcd);
// Funciones para manejar la navegación y la lógica del menú
void onButtonPress() {
if (menu.get_currentScreen() == &mainScreen) {
menu.change_screen(&selectBombaScreen);
} else if (menu.get_currentScreen() == &selectBombaScreen) {
if (menu.get_focusedLine() == &line2_1) {
menu.change_screen(&bomba1Screen);
} else if (menu.get_focusedLine() == &line2_2) {
menu.change_screen(&bomba2Screen);
}
} else if (menu.get_currentScreen() == &bomba1Screen) {
if (menu.get_focusedLine() == &bomba1Line1) {
menu.change_screen(&bomba1OnScreen);
} else if (menu.get_focusedLine() == &bomba1Line2) {
menu.change_screen(&bomba1OffScreen);
}
} else if (menu.get_currentScreen() == &bomba1OnScreen || menu.get_currentScreen() == &bomba1OffScreen) {
menu.change_screen(&bomba1Screen);
} else if (menu.get_currentScreen() == &bomba2Screen) {
// Aquí se podrían añadir más interacciones si fuera necesario
}
}
void setup() {
Serial.begin(9600); // Para depuración
lcd.init(); // Asegúrate de usar el método correcto para inicializar el LCD
lcd.backlight();
lcd.clear(); // Asegúrate de que la pantalla esté limpia
// Inicializar EEPROM
EEPROM.get(EEPROM_BOMBA1_STATE, bomba1State);
EEPROM.get(EEPROM_BOMBA1_ON_VALUE, bomba1OnValue);
EEPROM.get(EEPROM_BOMBA1_OFF_VALUE, bomba1OffValue);
EEPROM.get(EEPROM_BOMBA2_TIEMPO, bomba2Tiempo);
EEPROM.get(EEPROM_BOMBA2_VELOCIDAD, bomba2Velocidad);
pinMode(buttonPin, INPUT_PULLUP);
menu.init();
menu.add_screen(mainScreen);
menu.add_screen(selectBombaScreen);
menu.add_screen(bomba1Screen);
menu.add_screen(bomba1OnScreen);
menu.add_screen(bomba1OffScreen);
menu.add_screen(bomba2Screen);
lcd.setCursor(0, 0);
lcd.print("Setup completo");
delay(2000);
lcd.clear();
}
void loop() {
// Lectura del encoder
long newPosition = encoder.read() / 4;
if (newPosition != oldPosition) {
oldPosition = newPosition;
if (menu.get_currentScreen() == &bomba1OnScreen) {
bomba1OnValue = constrain(bomba1OnValue + (newPosition > oldPosition ? 1 : -1), 0, 10);
} else if (menu.get_currentScreen() == &bomba1OffScreen) {
bomba1OffValue = constrain(bomba1OffValue + (newPosition > oldPosition ? 1 : -1), 0, 10);
} else if (menu.get_currentScreen() == &bomba2Screen) {
if (menu.get_focusedLine() == &bomba2Line1) {
bomba2Tiempo = constrain(bomba2Tiempo + (newPosition > oldPosition ? 1 : -1), 0, 100);
} else if (menu.get_focusedLine() == &bomba2Line2) {
bomba2Velocidad = constrain(bomba2Velocidad + (newPosition > oldPosition ? 1 : -1), 0, 100);
}
}
menu.update();
}
// Lectura del botón
buttonState = digitalRead(buttonPin);
if (buttonState == LOW && lastButtonState == HIGH) {
onButtonPress();
}
lastButtonState = buttonState;
menu.update();
}
void saveSettingsToEEPROM() {
EEPROM.put(EEPROM_BOMBA1_STATE, bomba1State);
EEPROM.put(EEPROM_BOMBA1_ON_VALUE, bomba1OnValue);
EEPROM.put(EEPROM_BOMBA1_OFF_VALUE, bomba1OffValue);
EEPROM.put(EEPROM_BOMBA2_TIEMPO, bomba2Tiempo);
EEPROM.put(EEPROM_BOMBA2_VELOCIDAD, bomba2Velocidad);
}
// Llamar esta función antes de apagar el sistema o cuando se cambie algún valor importante
void onSystemShutdown() {
saveSettingsToEEPROM();
}