Guardar Fecha en la EEPROM

Eeprom.get y EEPROM.put resuelven tu problema con almacenar y recuperar las fechas

Te he creado una ejemplo que almacena la hora en EEPROM cuando presionas un boton conectado al pin 2.
Mientras muestra en el arranque el valor de la hora guardada y

#include <Time.h>               // https://www.pjrc.com/teensy/td_libs_Time.html
#include <TimeLib.h>
#include <EEPROM.h>
#define pinBoton  2
bool estado, estadoAnt = true;

time_t t;

time_t tmConvert_t(byte hh, byte mm, byte ss, byte DD, byte MM, int YYYY) {
  
  setTime(hh,mm,ss,DD,MM,YYYY); // fijo la hora interna
  tmElements_t tmSet;
  tmSet.Year = YYYY - 1970;
  tmSet.Month = MM;
  tmSet.Day = DD;
  tmSet.Hour = hh;
  tmSet.Minute = mm;
  tmSet.Second = ss;
  return makeTime(tmSet); 
}

void print_t(time_t temp) {
  char buffer[30];

  sprintf(buffer, "%02d:%02d:%02d %02d/%02d/%02d", hour(temp), minute(temp), second(temp), day(temp), month(temp), year(temp));
  Serial.println(buffer);
}

void setup()   {   

  pinMode(pinBoton, INPUT_PULLUP);
  int eeAddress = 0; //EEPROM address to start reading from
  
  Serial.begin(115200);
  while (!Serial) {
  ; // wait for serial port to connect. Needed for Leonardo only
  }

  EEPROM.get( eeAddress, t ); 
  Serial.println("Leido de la EEPROM");
  print_t(t);  // imprimo lo leido de a EEPROM


  time_t s_tm = tmConvert_t(13,05,00,14,11,2017);
  
  Serial.println("Hora fijada");
  print_t(s_tm);

}

void loop()  {   

    time_t t = now();

 
    bool estado = digitalRead(pinBoton);

    if (!estado && estadoAnt) {
        int eeAddress = 0;   //Location we want the data to be put.
        //One simple call, with the address first and the object second.
        EEPROM.put(eeAddress, t);
        Serial.println();
        Serial.println("Hora guardada.");
        print_t(t);
    }
    estadoAnt = estado;
    print_t(t);
    delay(1000);     
}

Debería ser una guia mas que suficiente con que comenzar

1 Like