/*
 * Modder:  @red9030
 * Title:   RTC+ESP32 Clock + LCD1602 I²C Arduino version
 * Reference: https://github.com/PaulStoffregen/Time/blob/master/examples/TimeRTC/
 *           
 *  _   _     _   _ 
 * |_| |_| . |_| |_|
 * |_| |_| . |_| |_|
 * 
 * This example code is in the public domain
 */


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

/*
 *****************************************************
 *    LIBRERIAS
 *****************************************************
*/
#include <TimeLib.h>
#include <Wire.h>
#include <DS1307RTC.h>  // a basic DS1307 library that returns time as a time_t
#include <LiquidCrystal_I2C.h>
//#include "RTClib.h"

/*
 *****************************************************
 *    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


/*
 *****************************************************
 *    INICIO
 *****************************************************
*/
void setup() {
  Serial.begin(115200);
  while (!Serial) ; // wait until Arduino Serial Monitor opens
	setTime(19,14,59,14,9,2013);  //hr,min,sec,day,month,yr
	RTC.set(now());

  setSyncProvider(RTC.get);   // the function to get the time from the RTC

  if(timeStatus()!= timeSet){
     Serial.println(timeStatus());
     Serial.println(timeSet);
     Serial.println("Unable to sync with the RTC");
  }else{
     Serial.println("RTC has set the system time");      
  }
  
  //LCD.begin(lcdColumnas,lcdFilas);     // initialize the lcd serial                 
  LCD.init();       // initialize the lcd i2c
  LCD.backlight();  // set up the LCD's 
  LCD.clear();
}

/*
 *****************************************************
 *    REPETICIÓN
 *****************************************************
*/
void loop() {
 if (timeStatus() == timeSet) {
    digitalClockDisplay();
  } else {
    Serial.println(timeStatus());
    Serial.println(timeSet);
    Serial.println("The time has not been set.  Please run the Time");
    Serial.println("TimeRTCSet example, or DS1307RTC SetTime example.");
    Serial.println();
    delay(4000);
  }
  delay(1000);
}
/*
 *****************************************************
 *    FUNCIONES
 *****************************************************
*/
void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year());
  Serial.print(" Epoch: ");
  Serial.print(now());
  Serial.println();

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

}

void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}
GND5VSDASCLSQWRTCDS1307+