/*
 * Modder:  @red9030
 * Title:   RTC+ESP32 Clock + LCD1602 I²C Arduino version
 * Reference:
 *           
 *  _   _     _   _ 
 * |_| |_| . |_| |_|
 * |_| |_| . |_| |_|
 * 
 * This example code is in the public domain
 */

/*
 * FILAS    ----
 * COLUMNAS ||||
*/

/*
 *****************************************************
 *    LIBRERIAS
 *****************************************************
*/
//#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal_I2C.h>  //Librería para uso de pantallas de cristal liquido.

/*
 *****************************************************
 *    VARIABLES
 *****************************************************
*/

#define I2C_address 0x27    // 0x27 , 0x3F , 0x20 , 0x38
const int lcdFilas = 2;     //FIlas del LCD
const int lcdColumnas = 16; //Columnas del LCD

LiquidCrystal_I2C LCD(I2C_address, lcdColumnas, lcdFilas);  // I2C address 0x27 (from DIYables LCD), 16 column and 2 rows

RTC_DS1307 rtc;

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};


/*
 *****************************************************
 *    INICIO
 *****************************************************
*/
void setup() {
  Serial.begin(115200);
  //LCD.begin(lcdColumnas,lcdFilas);     // initialize the lcd serial                 
  LCD.init();       // initialize the lcd i2c
  LCD.backlight();  // set up the LCD's 
  LCD.clear();

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    abort();
  }

}

/*
 *****************************************************
 *    REPETICIÓN
 *****************************************************
*/
void loop() {
 prinTime();
}
/*
 *****************************************************
 *    FUNCIONES
 *****************************************************
*/

//Imprime el tiempo del modulo rtc por el monitor serial y la pantalla LCD
void prinTime(){
  DateTime now = rtc.now();
  int segundo=now.second();

  Serial.print("Current time: ");
  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.day(), DEC);
  Serial.print(" (");
  Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
  Serial.print(") ");
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  //Serial.print(" ");
  //Serial.print(typedef(segundo).name());
  Serial.println();

  LCD.setCursor(0, 0);
  LCD.println("Fecha: ");
  LCD.setCursor(7, 0);
  LCD.print(now.day(), DEC);
  LCD.print('/');
  LCD.print(now.month(),DEC);
  LCD.print('/');
  LCD.print(now.year(),DEC);
  LCD.setCursor(0, 1);
  LCD.println("Hora: ");
  LCD.setCursor(6, 1);
  LCD.print(now.hour(), DEC);
  LCD.print(':');
  LCD.print(now.minute(), DEC);
  LCD.print(':');
  LCD.print(now.second(), DEC);
  delay(500);
}

time_t time_provider()
{
    return rtc.now().unixtime();  
}
GND5VSDASCLSQWRTCDS1307+