#include <Wire.h>
// LCD
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(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 boton = 13; // SW - Pulsador
// VARIBLES
int option = 1; // Menu
int optionLinea = 1; // Menu
int subOption = 1;
bool sound = true;
bool lcdClear = false;
bool menu = false;
bool subMenu = false;
bool botonPulsado = false;
bool botonAnterior = false;
bool playLoop = true;
byte logo[8] = {
0b11111,
0b00000,
0b01111,
0b00000,
0b00111,
0b00000,
0b00001,
0b00000
};
byte up[8] = {
0b00100,
0b01010,
0b10001,
0b00100,
0b01010,
0b10001,
0b00000,
0b00000
};
byte down[8] = {
0b00000,
0b00000,
0b10001,
0b01010,
0b00100,
0b10001,
0b01010,
0b00100
};
byte icon[8] = {
0b00000,
0b01111,
0b10101,
0b11111,
0b11111,
0b11111,
0b11111,
0b00000
};
byte sonido[8] = {
0b00000,
0b00010,
0b00110,
0b11110,
0b11110,
0b00110,
0b00010,
0b00000
};
byte ok[8] = {
0b00000,
0b00010,
0b10100,
0b01000,
0b00000,
0b00000,
0b00000,
0b00000
};
byte xOff[8] = {
0b00000,
0b00000,
0b00000,
0b00000,
0b01010,
0b00100,
0b01010,
0b00000
};
byte prod1[8] = {
0b00000,
0b00111,
0b00001,
0b11101,
0b11101,
0b11100,
0b11100,
0b00000
};
byte prod[8] = {
0b11111,
0b10001,
0b10001,
0b10001,
0b10001,
0b10001,
0b10001,
0b11111
};
void setup()
{
Serial.begin(9600);
// Configurar los pines como entradas
pinMode(pinX, INPUT);
pinMode(pinY, INPUT);
pinMode(boton, INPUT_PULLUP);
//Inicializar la comunicación I2C
Wire.begin(21, 22);
lcd.init(); // initialize the lcd
lcd.backlight(); // Turn on the LCD backlight
lcd.clear(); // Clear the display
lcd.createChar(0,up); // Firebase
lcd.createChar(1,down);
lcd.createChar(2,logo);
lcd.createChar(3,icon);
lcd.createChar(4,sonido);
lcd.createChar(5,ok);
lcd.createChar(6,xOff);
lcd.createChar(7,prod);
// Mensaje de inicio
lcd.setCursor(6,1);
lcd.print("MOND");
lcd.setCursor(0,3);
lcd.print("Iniciando Linea >>>");
//delay(2000);
lcd.clear();
}
void informacionLCD() {
if (menu != lcdClear) { lcdClear = menu; lcd.clear(); }
lcd.setCursor(19,0); // WIFI
lcd.write(byte(2));
if (menu)
{
lcd.setCursor(8,0); // LINEA
lcd.print("MENU");
lcd.setCursor(10,1);
lcd.write(byte(0)); // FLECHA ARRIBA
lcd.setCursor(10,3);
lcd.write(byte(1)); // FLECHA ABAJO
lcd.setCursor(0,2);
lcd.print("<"); // FLECHA IZQ
lcd.setCursor(19,2);
lcd.print(">"); // FLECHA DER
switch (option) {
case 1:
if (subMenu) {
lcd.setCursor(2,2); // LINEA
lcd.print(" Linea: * " + String(optionLinea) + " ");
}
else {
lcd.setCursor(2,2); // LINEA
lcd.print("* Linea: " + String(optionLinea) + " ");
}
break;
case 2:
if (subMenu) {
lcd.setCursor(2,2); // ESTADO LOOP
lcd.print(" Estado: * ");
lcd.setCursor(14,2);
lcd.print(playLoop ? "ON " : "OFF ");
}
else {
lcd.setCursor(2,2); // ESTADO LOOP
lcd.print("* Estado: ");
lcd.setCursor(14,2);
lcd.print(playLoop ? "ON " : "OFF ");
}
break;
case 3:
if (subMenu) {
lcd.setCursor(2,2); // ESTADO LOOP
lcd.print(" Sonido: * ");
lcd.setCursor(14,2);
lcd.print(sound ? "ON " : "OFF ");
}
else {
lcd.setCursor(2,2); // SOUND
lcd.print("* Sonido: ");
lcd.setCursor(14,2);
lcd.print(sound ? "ON " : "OFF ");
}
break;
case 4:
if (subMenu) {
lcd.setCursor(2,2); // Reset WIFI
lcd.print(" WIFI: * Reset ");
}
else {
lcd.setCursor(2,2); // Reset WIFI
lcd.print("* WIFI: ");
}
break;
case 5:
if (subMenu) {
lcd.setCursor(2,2); // Reset WIFI
lcd.print(" Exit: * OK ");
}
else {
lcd.setCursor(2,2); // Reset WIFI
lcd.print("* Exit: ");
}
break;
}
}
else
{
lcd.setCursor(0,0);
lcd.write(byte(4));
lcd.setCursor(1,0);
lcd.write(byte(5));
lcd.setCursor(3,0);
lcd.write(byte(3));
lcd.setCursor(4,0); // SD ESTADO
//lcd.print("x");
lcd.write(byte(6));
lcd.setCursor(8,0); // HORARIO
lcd.print("12:56:30");
lcd.setCursor(17,0);
lcd.write(byte(0));
/*
lcd.setCursor(0,0); // LINEA
lcd.print("L:");
lcd.setCursor(2,0);
lcd.print(optionLinea);
lcd.setCursor(4,0); // ESTADO LOOP
lcd.print("E:");
lcd.setCursor(6,0);
lcd.print(playLoop ? "ON " : "OFF");
lcd.setCursor(10,0);
lcd.write(byte(3));
lcd.setCursor(11,0); // SD ESTADO
lcd.print(":ON");
*/
/*
lcd.setCursor(16,0); // FIREBASE
lcd.print("F");
lcd.setCursor(17,0);
lcd.write(byte(0));
*/
//lcd.setCursor(0,1); // CONTADOR
//lcd.print("C:0000");
lcd.setCursor(0,1);
lcd.write(byte(7));
lcd.setCursor(0,3); // DETECTANDO
lcd.print("Detectando...");
//lcd.setCursor(0,3);
//lcd.print(" ");
}
}
void loop()
{
informacionLCD();
int x = analogRead(pinX); // STICK JOYSTICK Leer los valores de los ejes X y Y del joystick
int y = analogRead(pinY);
int mappedX = map(x, 0, 4095, -10, 10); // Mapear los valores de los ejes X y Y
int mappedY = map(y, 0, 4095, 10, -10);
botonPulsado = digitalRead(boton) == LOW; // Estado del pulsador SW
if (botonPulsado && !botonAnterior ) {
menu = !menu;
botonAnterior = !botonAnterior;
}
if (menu) {
// Lógica del menú arriba y abajo
if (!subMenu) {
if (mappedY > 9 ) option = (option >= 5) ? 1 : option + 1;
if (mappedY < -9 ) option = (option <= 1) ? 5 : option -1;
if (mappedX > 9) subMenu = true;
}
if (subMenu) {
if (mappedX < -9) subMenu = false;
}
// Seteo el subMenu y la opcion entes de salir del menu.
if (botonPulsado) {
subMenu = false;
option = 1;
}
// Opciontes del Menu.
switch (option) {
case 1: // Option 1 - Cambio de linea
if (subMenu) {
if (mappedY > 9 ) optionLinea = (optionLinea >= 3) ? 1 : optionLinea + 1;
if (mappedY < -9 ) optionLinea = (optionLinea <= 1) ? 3 : optionLinea - 1;
}
break;
case 2: // option 2 - Estado.
if (subMenu) {
if (abs(mappedY) > 9 ) playLoop = !playLoop;
}
break;
case 3: // option 3 - Sonido.
if (subMenu) {
if (abs(mappedY) > 9 ) sound = !sound;
}
break;
case 4: // reseteo red wifi.
if (subMenu) {
//if (abs(mappedY) > 9) handleWiFiChange();
}
break;
case 5:
if (subMenu) {
if (abs(mappedY) > 9){
menu = false;
botonAnterior = false;
}
}
break;
}
}
delay(100); // Debounce para evitar rebotes
}