Save date on EEPROM

Hello, i am trying to do a software that works like:
There is a timer counting on the arduino, in the hardware, there is a switch, and every time that someone turns on the switch, Arduino would store the time that the switch was turned on/off.

I already wrote this:

#include <Time.h>
//#include <TimeAlarms.h>

const int buttonPin = 3;
int buttonState = 1;
int coco = 0;

int ledPin = 8;

void setup()
{
 Serial.begin(9600);
 pinMode(ledPin, OUTPUT);
 setTime(8,29,0,24,8,2015); // set time to Saturday 8:29:00am Jan 1 2011
            // called once after 10 seconds 
}

void  loop(){  
 digitalClockDisplay();

}



void digitalClockDisplay()
{

buttonState = digitalRead(buttonPin);

if(buttonState != coco){
coco = 1 - coco;

Serial.print(hour());
 printDigits(minute());
 printDigits(second());
 Serial.print("  ");
 Serial.print(day());
 PrintDigits(month());
 PrintDigits(year());
 Serial.println(); 
}
 
 if(buttonState != 0){
  digitalWrite(ledPin, HIGH);
  
}
if(buttonState != 1){
  digitalWrite(ledPin, LOW);
}
 

}

void printDigits(int digits)
{
 Serial.print(":");
 if(digits < 10)
   Serial.print('0');
 Serial.print(digits);
}

void PrintDigits(int digits)
{
 
Serial.print("/");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
 
}

But i dont know how to do the arduino store the information.
Thanks

Please edit your post, select the code, and put it between [code][/code] tags.

You can do that by hitting the “Code” icon above the posting area. It is the first icon, with the symbol: </>

Read this before posting a programming question

Take a close look at these statements and see if you can simplify them:

 if(buttonState != 0){
  digitalWrite(ledPin, HIGH);
}
if(buttonState != 1){
  digitalWrite(ledPin, LOW);
}

Also, did you really mean:

coco = 1 - coco;      // This is a little unusual

or did you mean:

coco = coco - 1;

If you meant the latter, a more common way to write this is:

coco--;

But i dont know how to do the arduino store the information.

You are using 18 characters to show the date. Do you want to use 18 bytes to store the data in EEPROM? If so, you'll be able to store about 50 date/time stamps in EEPROM. Stored as bytes, you could store 166 date/time values.

But why? What use can you possibly make of the data?

But how can i store the data in bytes??
And i am going to use just to see when the button was turned on or off.

The Time library has maketime and breaktime functions. You could use maketime to get an unsigned long ( 4 bytes ) that represents the date as a number of seconds since 1970. You could save this unsigned long in EEPROM easily. And when you want to convert it back to a date, use breaktime.

But how can i store the data in bytes??

What range of values are hour(), minute(), etc. returning? Why do you assume that they won't fit in a byte?

And i am going to use just to see when the button was turned on or off.

How? You'll need another sketch to get the data out of EEPROM, or changes to the current one.

Yes, i am going to have another sketch to read the EEPROM.
I tried to understand the maketime and breaktime functions, but 2 things:
1, i dont know if it works, because on the arduino site it says that this is the older version.
and 2, i dont understood how to use this functions.

I am going to try the maketime and breaktime functions, thanks

There is another way to use the time library without working through those functions which you may find more intuitive. A call to now() will give you "unix time"--an unsigned long ( 4 bytes ) that represents the date as a number of seconds since 1970.

uint32_t timeStamp = now();

You will save and retreive this number.

You can then use the usual calls to time/day/year with reference to this value for example

hour(timeStamp);
minute(timeStamp);
second(timeStamp);
day(timeStamp);
month(timeStamp);
year(timeStamp);

cattledog, this way worked for me, but there is just one problem, the EEPROM just stores in each byte values between 0 and 255, and the value in seconds is something like, 1440404940 seconds, so, how can i store this “big” value in the EEPROM.

I did this:

CODE TO WRITE IN EEPROM:

#include <EEPROM.h>

#include <Time.h>
//#include <TimeAlarms.h>

const int buttonPin = 3;
int buttonState = 1;
int coco = 0;

int memoria = 1;

int ledPin = 8;


void setup()
{
  Serial.begin(19200);
  pinMode(ledPin, OUTPUT);
  setTime(8,29,0,24,8,2015);  


}

void  loop(){  

  ClockDisplay();
  
}

void ClockDisplay()
{
uint32_t timeStamp = now();
buttonState = digitalRead(buttonPin);

if(buttonState != coco){
 coco = 1 - coco;
 EEPROM.write(memoria, timeStamp);
 Serial.print(memoria);
 Serial.print("    ");
 Serial.println(timeStamp);
 memoria = memoria + 1;
 }
  
   
   
   if(buttonState != 0){
 digitalWrite(ledPin, HIGH);  
}

  if(buttonState != 1){
   digitalWrite(ledPin,LOW); 
  }
   
  }

CODE TO READ EEPROM:

#include <EEPROM.h>    // incluir a biblioteca 

// inicia lendo apartir do primeiro byte (endereço 0) da EEPROM
int address = 0;    // endereço de escrita na memória
int value;    // valor lido da EEPROM    

void setup()
{
  Serial.begin(9600);    // inicializa o canal de comunicação serial
}

void loop()
{
  // lê o byte no endereço atual da EEPROM
  value = EEPROM.read(address);
  
// envia o valor lido para o computador pela porta serial
  Serial.print(address);    
  Serial.print("\t");
  Serial.print(value, DEC);
  Serial.println();
  
  // avança para o próximo endereço da EEPROM
  address = address + 1;
  
  // quando atingir o endereço 512 retorna 
  // para o endereço 0
  if (address == 512)
    address = 0;
    
  delay(500);
}

There is a new standard eeprom library included in the ide version 1.6.2 and later which does what you need to break the unsigned long timeStamp variable into 4 bytes for storage and retreival. The functions are eeprom.put and eeprom.get. There are examples with the library.

I was reading and it says, when i use the EEPROM.get, to read the data, i need to put: EEPROM.get(address, object), how i put the object that i want to read?
If I use another Sketch(one sketch to write and one to read), how am i going to read the data, because, normally using the EEPROM.read, you just need to put the address.

Work with the two library sketch examples eeprom_put and eeprom_get. Study them. See how they declare the object to be put and the object to get.

You can simplify them and comment out the parts relating to the struct. For now, just work with the float example. See if you can combine the two sketches into one where the first part of the sketch stores the value, and the second part of the sketch reads it. You are really just calling EEPROM.put() and EEPROM.get() in the same sketch. Both functions take the same (address,object) input.

Then change the float to an unsigned long (or uint32_t) and use the timeStamp.