Store Time to EEPROM

Hello! This will be my first time to store data to Arduino UNO's EEPROM. Is this possible?

The timer start when a fan is turned on. When it turns off, it stores the value to the EEPROM. And when it turns back on, it adds the stored time to the current time.

I don't know much about bytes and stuff, so...

Can I incorporate this:

#include <DateTime.h>

/* Useful Constants */
#define SECS_PER_MIN  (60UL)
#define SECS_PER_HOUR (3600UL)
#define SECS_PER_DAY  (SECS_PER_HOUR * 24L)
 
/* Useful Macros for getting elapsed time */
#define numberOfSeconds(_time_) (_time_ % SECS_PER_MIN)  
#define numberOfMinutes(_time_) ((_time_ / SECS_PER_MIN) % SECS_PER_MIN) 
#define numberOfHours(_time_) (( _time_% SECS_PER_DAY) / SECS_PER_HOUR)
#define elapsedDays(_time_) ( _time_ / SECS_PER_DAY)  

void setup(){
 Serial.begin (57600);
}

void loop(){  
  time(millis() / 1000);
  delay(1000);
}

void time(long val){  
 int days = elapsedDays(val);
 int hours = numberOfHours(val);
 int minutes = numberOfMinutes(val);
 int seconds = numberOfSeconds(val);

  // digital clock display of current time
  Serial.print(days,DEC);  
  printDigits(hours);  
  printDigits(minutes);
  printDigits(seconds);
  Serial.println();  
  
}

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

source: http://forum.arduino.cc/index.php/topic,42211.0.html

to this

#include <EEPROM.h>

char my2dArray[5][11]= // [11] required for null character termination of string literals
{
  "123456ABCD",
  "234561BCDA",
  "345612CDAB",
  "456123DABC",
  "561234ABCD"
};

void setup(){
  Serial.begin(9600);

  int address;

  Serial.println ("Writing data.....");

  for(int j=0; j<5; j++){

    Serial.println();

    for(int i=0; i<10; i++){

      Serial.print(my2dArray[j][i]);
      EEPROM.write(address= i+(j*10), my2dArray[j][i]);
    }
  }
  Serial.println();
  Serial.println();
  Serial.println ("Reading data.....");

  for(int j=0; j<5; j++){

    Serial.println();

    for(int i=0; i<10; i++){

      char value= EEPROM.read(address= i+(j*10));
      Serial.print(value);

    }
  }
}

void loop(){
}

source: help needed to store a string array in the EEPROM - Programming Questions - Arduino Forum

I didn't read the code, because all I want do say I get it from the subject. I think is a very bad idea store time in EEPROM. This kind of memory have a limited number of writes. If you write many times to it you can "kill it" very rapidly. Is better use some battery backed-up SRAM's.

Or, if you want it to be transportable to other systems like your PC for later analysis, consider using an SD card.

luisilva:
I didn't read the code, because all I want do say I get it from the subject. I think is a very bad idea store time in EEPROM. This kind of memory have a limited number of writes. If you write many times to it you can "kill it" very rapidly. Is better use some battery backed-up SRAM's.

about 100,000 read writes in each page.

You can create a circular buffer in EEPROM and dilute this effect easily by orders of magnitude..

about 100,000 read writes in each page

No each byte.

Thank you for the quick replies!

Hm, if I'll use an SD Shield can I still read the data after the arduino shuts down? Sorry, I'm new to this.

Grumpy_Mike:

about 100,000 read writes in each page

No each byte.

right, my understanding was one byte read/write was affecting its entire page, essentially.

so a 2K EEPROM is 2^9 read writes? (using wear leveling,to dilute the effects as mentioned) you can only bang on a single byte 100,000times, yeah?

arduinoTime:
Thank you for the quick replies!

Hm, if I'll use an SD Shield can I still read the data after the arduino shuts down? Sorry, I'm new to this.

Well not read via the Arduino no, not if it's off..... I was thinking more along the lines of swapping the "full" SD with an "empty" one and taking the "full" one to the PC by hand.

Yes, in effect there is no concept of Page in the EEPROM used on the arduino like there is on other non volatile storage, so the limit is on byte writes, you can read it as many times as you like, it is only writes that count.

Grumpy_Mike:
Yes, in effect there is no concept of Page in the EEPROM used on the arduino like there is on other non volatile storage, so the limit is on byte writes, you can read it as many times as you like, it is only writes that count.

cheers, thanks!

Well not read via the Arduino no, not if it's off

Sorry, Sir JimBoza, I don't quite understand..

I'll just rephrase my question because sometimes I really don't know what I'm saying :roll_eyes:
When I turn the arduino on and it reads that the fan is on, it will start the timer. When I turn the fan off [arduino is still on], it will store the time to the SD.
My question is-- if I turn the arduino off then turn it on, can I get the time from the SD, make it the start time, then save it again once the fan turns off? I don't need connection from a computer for the arduino to read the data in the SD card

My question is-- if I turn the arduino off then turn it on, can I get the time from the SD, make it the start time, then save it again once the fan turns off?

Yes.

arduinoTime:
My question is-- if I turn the arduino off then turn it on, can I get the time from the SD, make it the start time, then save it again once the fan turns off? I don't need connection from a computer for the arduino to read the data in the SD card

The data stays in the SD card once the Arduino powers off, if that's what you mean so yes you could read the previously stored time and use it in the next session.