#include <Wire.h>
// LCD
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C display(0x27, 20, 4);
int lcd_Columns = 20; // Number of columns in display
int lcd_Rows = 4; // Number of Rows in display
// Definir los pines a los que está conectado el joystick
const int pinX = 33; // VRx - Eje X
const int pinY = 32; // VRy - Eje Y
const int SW = 13; // SW - Pulsador
int mappedX = 0;
int mappedY = 0;
// VARIBLES
unsigned long dataMillisMenu = 0;
int option = 1;
bool sound = true;
bool lcdClear = false;
bool menu = false;
bool subMenu = false;
bool buttonPress = false;
//---LCD Character----------------------------------------------------------------------------------------------------//
byte wifi0[8] = {
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b11000,
0b00000
};
byte wifi1[8] = {
0b00000,
0b00000,
0b11110,
0b00000,
0b11100,
0b00000,
0b11000,
0b00000
};
byte wifi2[8] = {
0b11111,
0b00000,
0b11110,
0b00000,
0b11100,
0b00000,
0b11000,
0b00000
};
byte down[8] = {
0b00000,
0b00000,
0b10001,
0b01010,
0b00100,
0b10001,
0b01010,
0b00100
};
byte iconSound[8] = {
0b00000,
0b00010,
0b00110,
0b11110,
0b11110,
0b00110,
0b00010,
0b00000
};
byte iconSd[8] = {
0b00000,
0b01111,
0b10101,
0b11111,
0b11111,
0b11111,
0b11111,
0b00000
};
byte iconOk[8] = {
0b00000,
0b00010,
0b10100,
0b01000,
0b00000,
0b00000,
0b00000,
0b00000
};
byte iconX[8] = {
0b00000,
0b00000,
0b00000,
0b00000,
0b01010,
0b00100,
0b01010,
0b00000
};
// Display lcd print.
void displayPrint(int posicion, int linea, String texto) {
display.setCursor(posicion, linea); // Ubicamos el cursor en la posicion y linea deseada
display.print(texto); // Escribe primera linea del cartel
}
// Display lcd write for character.
void displayWrite(int posicion, int linea, int numeroByte) {
display.setCursor(posicion, linea);
display.write(byte(numeroByte));
}
// Informacion Lcd dentro del Menu.
void lcdInfoMenu() {
displayPrint(8,0,"MENU"); // MENU
displayWrite(10,3,5); // Flecha abajo
displayPrint(0,2,"<"); // Flecha izq
displayPrint(19,2,">"); // Flecha der
switch (option) {
case 1:
// Sonido
if (subMenu) {
displayPrint(2,2," Sonido: * "); // Se utilizan espacios para centrar la informacion y limpiar la pantalla.
displayPrint(14,2,sound ? "ON " : "OFF ");
}
else {
displayPrint(2,2,"* Sonido: ");
displayPrint(14,2,sound ? "ON " : "OFF ");
}
break;
case 2:
// WIFI Reset
(subMenu) ? displayPrint(2,2," WIFI: * Reset ") : displayPrint(2,2,"* WIFI: ");
break;
}
}
// Informacion Lcd.
void lcdInfoNormal() {
displayPrint(0,1," <<< Error SD >>> ");
// Informacion Level Wifi.
displayWrite(0,0,2);
// Sonido
displayWrite(2,0,6);
displayWrite(3,0,(sound) ? 3 : 4);
// Sd estado
//bool boolSD = SD.begin(SS);
displayWrite(5,0,7);
//displayWrite(6,0,(boolSD ? 3 : 4));
// Horario
displayPrint(12,0,"13:27:50");
// Firebase
//if (Firebase.ready()) displayWrite(17,0, byte(4));
// Contador
//displayPrint(0,3,"# ");
displayPrint(0,3,"# 102 ");
// Productos
displayPrint(0,2,"Lectura");
}
// Informacion en el display.
void displayLcdInfo() {
// Limpiar display.
if (menu != lcdClear) { lcdClear = menu; display.clear(); }
// Informacion si estoy o no en el menu.
(!menu) ? lcdInfoNormal() : lcdInfoMenu();
}
// Manejar opciones del menu.
void handleMenuNavigation(int mappedX, int mappedY) {
if (!subMenu) {
if (mappedY > 9) option = (option >= 2) ? 1 : option + 1;
if (mappedY < -9) option = (option <= 1) ? 2 : option - 1;
if (mappedX > 9) subMenu = true;
}
if (subMenu && (mappedX < -9)) subMenu = false;
}
// Manejar opciones del submenu.
void handleMenuOptions() {
if (buttonPress) { subMenu = false; option = 1; }
switch (option) {
case 1: // Sonido.
if (subMenu) {
if (abs(mappedY) > 9) sound = !sound;
}
break;
case 2: // Reset Wifi.
if (subMenu) {
if (abs(mappedY) > 9) {
// Reconexion de Wifi en un hilo separado.
Serial.println("Reinicio del wifi");
subMenu = false;
menu = false;
}
}
break;
}
}
void setup()
{
Serial.begin(9600);
// Configurar los pines como entradas
pinMode(pinX, INPUT);
pinMode(pinY, INPUT);
pinMode(SW, INPUT_PULLUP);
//Inicializar la comunicación I2C
Wire.begin(21, 22);
display.init(); // initialize the lcd
display.backlight(); // Turn on the LCD backlight
display.clear(); // Clear the display
// Display character WIFI y FIREBASE.
display.createChar(0,wifi0); // Wifi.
display.createChar(1,wifi1);
display.createChar(2,wifi2);
display.createChar(3,iconOk);
display.createChar(4,iconX);
display.createChar(5,down);
display.createChar(6,iconSound); // Icono sonido.
display.createChar(7,iconSd);
// Mensaje de inicio
display.setCursor(6,1);
display.print("MOND");
display.setCursor(0,3);
display.print("Iniciando Linea >>>");
//delay(2000);
display.clear();
}
//---Funcion MENU------------------------------------------------------------------------------------------//
void menuLoopPrincial() {
// probar con otros valores menor a 300ms.
if ( (millis() - dataMillisMenu) > 300 || dataMillisMenu == 0 ){
dataMillisMenu = millis();
int x = analogRead(pinX); // STICK JOYSTICK Leer los valores de los ejes X y Y del joystick
int y = analogRead(pinY);
mappedX = map(x, 0, 4095, -10, 10); // Mapear los valores de los ejes X y Y
mappedY = map(y, 0, 4095, 10, -10);
buttonPress = digitalRead(SW) == LOW; // Estado del pulsador SW
if (buttonPress) menu = !menu;
if (menu) {
// Lógica del menú arriba y abajo
handleMenuNavigation(mappedX, mappedY);
// Seteo el subMenu y la opcion entes de salir del menu.
handleMenuOptions();
}
}
}
// Loop princiap
void loop()
{
displayLcdInfo();
menuLoopPrincial();
}