#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal_I2C.h>
// Dirección I2C del LCD (puedes encontrarlo usando un escáner I2C)
#define LCD_ADDRESS 0x27
// Tamaño del LCD
#define LCD_COLUMNS 20
#define LCD_ROWS 4
// Inicializar el objeto LCD
LiquidCrystal_I2C lcd(LCD_ADDRESS, LCD_COLUMNS, LCD_ROWS);
// RTC_DS1307 rtc;
RTC_DS3231 rtc;
String daysOfTheWeek[7] = { "Domingo", "Lunes", "Martes", "Miercoles", "Jueves", "Viernes", "Sabado" };
String monthsNames[12] = { "Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre" };
void setup() {
lcd.begin(LCD_COLUMNS, LCD_ROWS);
Serial.begin(9600);
delay(1000);
if (!rtc.begin()) {
lcd.println(F("Couldn't find RTC"));
while (1);
}
// Si se ha perdido la corriente, fijar fecha y hora
if (rtc.lostPower()) {
// Fijar a fecha y hora de compilacion
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// Fijar a fecha y hora específica. En el ejemplo, 21 de Enero de 2016 a las 03:00:00
// rtc.adjust(DateTime(2016, 1, 21, 3, 0, 0));
}
}
void printDate(DateTime date)
{
lcd.print(date.year(), DEC);
lcd.print('/');
lcd.print(date.month(), DEC);
lcd.print('/');
lcd.print(date.day(), DEC);
lcd.print(" (");
lcd.print(daysOfTheWeek[date.dayOfTheWeek()]);
lcd.print(") ");
lcd.print(date.hour(), DEC);
lcd.print(':');
lcd.print(date.minute(), DEC);
lcd.print(':');
lcd.print(date.second(), DEC);
lcd.println();
}
void loop() {
// Obtener fecha actual y mostrar por Serial
DateTime now = rtc.now();
printDate(now);
delay(3000);
}