It's quite big, but ok... I've already added the loop in this version
#include <EEPROM.h>
#include <Keypad.h>
#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
RTC_DS1307 rtc;
String daysOfTheWeek[7] = { "Domingo", "Lunes", "Martes", "Miercoles", "Jueves", "Viernes", "Sabado" };
String monthsNames[12] = { "Ene", "Feb", "Mar", "Abr", "May", "Jun", "Jul", "Ago", "Sep", "Oct", "Nov", "Dic" };
const byte rowsCount = 4;
const byte columsCount = 4;
char keys[rowsCount][columsCount] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
const byte rowPins[rowsCount] = { 4, 5, 6, 7 };
const byte columnPins[columsCount] = { 8, 9, 10, 11 };
Keypad keypad = Keypad(makeKeymap(keys), rowPins, columnPins, rowsCount, columsCount);
int rele = 12;
int ledrojo = 3;
int ledverde = 2;
int encendido = 0;
int activo = 1;
void setup() {
Serial.begin(9600);
pinMode(rele, OUTPUT);
pinMode(ledverde, OUTPUT);
pinMode(ledrojo, OUTPUT);
lcd.init(); // initialize the lcd
//lcd.backlight();
#ifndef ESP8266
while (!Serial); // wait for serial port to connect. Needed for native USB
#endif
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running, let's set the time!");
// When time needs to be set on a new device, or after a power loss, the
// following line sets the RTC to the date & time this sketch was compiled
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
}
void loop() {
if (activo == 1) {
lcd.setCursor(15, 0);
lcd.print("*");
if (encendido == 1) {
digitalWrite(ledrojo, HIGH);
digitalWrite(ledverde, LOW);
digitalWrite(rele, HIGH);
} else {
digitalWrite(ledrojo, LOW);
digitalWrite(ledverde, HIGH);
digitalWrite(rele, LOW);
}
} else {
digitalWrite(ledrojo, LOW);
digitalWrite(ledverde, HIGH);
lcd.setCursor(15, 0);
lcd.print(" ");
}
DateTime now = rtc.now();
char buf1[] = "DD/MM/YYYY";
char buf2[] = "hh";
char buf3[] = "mm";
char buf4[] = "ss";
lcd.setCursor(0, 0);
lcd.print(now.toString(buf1));
lcd.setCursor(0, 1);
lcd.print(now.toString(buf2));
lcd.setCursor(2, 1);
lcd.print(":");
lcd.setCursor(3, 1);
lcd.print(now.toString(buf3));
lcd.setCursor(5, 1);
lcd.print(":");
lcd.setCursor(6, 1);
lcd.print(now.toString(buf4));
char key = keypad.getKey();
// if (key) {
// Serial.println(key);
// }
if (key == '*') {
lcd.backlight();
if (activo == 1) {
activo = 0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("PROGRAMA");
lcd.setCursor(0, 1);
lcd.print("DESACTIVADO");
delay(5000);
lcd.clear();
} else {
activo = 1;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("PROGRAMA");
lcd.setCursor(0, 1);
lcd.print("ACTIVADO");
delay(5000);
lcd.clear();
}
lcd.noBacklight();
}
if (key == 'A') {
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Hora inicio");
lcd.setCursor(0, 1);
lcd.print(EEPROM.read(0));
lcd.setCursor(2, 1);
lcd.print(":");
lcd.setCursor(3, 1);
lcd.print(EEPROM.read(1));
lcd.setCursor(0, 1);
char key = keypad.getKey();
for (int pointer = 0; pointer <= 3; pointer++) {
while (!key) {
lcd.blink();
}
if ((!isDigit(key)) || ((pointer == 0) && (key > 2))) {
} else {
lcd.print(key);
}
}
}
}