#include <Keypad.h>
#include <LiquidCrystal.h>
LiquidCrystal LCD(13, 12, 4, 2, A0, A1);
char teclas[4][3] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte Pin_Filas[4] = {11, 10, 9, 8};
byte Pin_Col[3] = {7, 6, 5};
Keypad Teclado = Keypad(makeKeymap(teclas), Pin_Filas, Pin_Col, 4, 3);
class Control_luz {
char clave[6], Tecla, TeclaAnterior;
int PinL;
bool reingresar;
public:
Control_luz(int SPWM) {
PinL = SPWM;
pinMode(PinL, OUTPUT);
TeclaAnterior = '\0';
reingresar = false;
}
void menu(void) {
delay(500);
LCD.setCursor(0, 0); LCD.print("1: Continuo");
LCD.setCursor(0, 1); LCD.print("2: Pulsado");
Serial.println("1: Continuo");
Serial.println("2: Pulsado");
Tecla = Teclado.getKey();
while (Teclado.getKey());
switch (Tecla) {
case '1': continuo(); break;
case '2': Pulsado(); break;
}
}
void continuo(void) {
do {
reingresar = false;
Serial.print("Tiempo [Seg]:");
int tiempo = leer("Tiempo[Seg]:");
if (reingresar) continue;
Serial.print("Intensidad [%]:");
int intensidad = leer("Intensidad[%]");
if (reingresar) continue;
intensidad = map(intensidad, 0, 100, 0, 255);
analogWrite(PinL, intensidad);
LCD.clear();
for (int t = 0; t < tiempo; t++) {
delay(1000); Serial.print(t + 1);
LCD.setCursor(2, 0); LCD.print("Tiempo [Seg]:");
LCD.setCursor(4, 1); LCD.print(t + 1);
LCD.setCursor(7, 1); LCD.print("/");
LCD.setCursor(9, 1); LCD.print(tiempo);
}
LCD.clear();
analogWrite(PinL, 0);
} while (reingresar);
}
void Pulsado(void) {
do {
reingresar = false;
Serial.print("Tiempo en alto [Seg]:");
int tiempoL = leer("Tiempo H[Seg]:");
if (reingresar) continue;
Serial.print("Tiempo bajo [Seg]:");
int tiempoB = leer("Tiempo L [Seg]:");
if (reingresar) continue;
Serial.print("Intensidad [%]:");
int intensidad = leer("Intensidad[%]");
if (reingresar) continue;
Serial.print("Repeticiones [R]:");
int repeticiones = leer("Repeticiones[R]");
if (reingresar) continue;
intensidad = map(intensidad, 0, 100, 0, 255);
for (int r = 0; r < repeticiones; r++) {
LCD.clear();
analogWrite(PinL, intensidad);
for (int t = 0; t < tiempoL; t++) {
delay(1000); Serial.print(t + 1);
LCD.setCursor(4, 0); LCD.print("Seg:");
LCD.setCursor(9, 0); LCD.print(t + 1);
}
analogWrite(PinL, 0);
for (int t = 0; t < tiempoB; t++) {
delay(1000);
}
}
} while (reingresar);
}
int leer(char *txt) {
int i = 0;
LCD.clear();
LCD.print(txt);
while (i < 6) {
Tecla = Teclado.getKey();
if (Tecla) {
while (Teclado.getKey());
if (Tecla == '*') {
if (TeclaAnterior == '*') {
reingresar = true;
return 0;
} else if (i > 0) {
i--;
LCD.setCursor(strlen(txt) + i, 0);
LCD.print(" ");
LCD.setCursor(strlen(txt) + i, 0);
}
} else if (Tecla == '#') {
break;
} else {
clave[i] = Tecla;
LCD.print(Tecla);
Serial.print(Tecla);
i++;
}
TeclaAnterior = Tecla;
}
}
clave[i] = '\0';
return atoi(clave);
}
};
Control_luz CL(3);
void setup() {
Serial.begin(9600);
LCD.begin(16, 2);
LCD.clear();
}
void loop() {
CL.menu();
}