This is what I made 1 year ago .But to succeeded I changed time libraries . Now with a fresh version of arduino ,this cod doesn't run . I lost my changed time library and I cant remember the changes that I have made
In this program a counter starts from zero. you can add hours and mins by pressing pins or you can erase timer.
Also there is a backup ,every hour, in case of power loss.
#include <DateTime.h>
#include <LiquidCrystal.h>
#include <EEPROM.h>
/* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)./. */
int resetPin = 10; // pin 10 resets the time
int addHourPin = 9;
int addMinPin = 8;
long restoreTime = 0;
int restoreDay = 0;
int eepromTime = 0;
int eepromDay = 0;
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);//create object to control an LCD
void setup(){
lcd.begin(16, 4); // set up the LCD's number of columns and rows
digitalWrite(resetPin,HIGH); // this line enables pull-up
digitalWrite(addHourPin,HIGH);// this line enables pull-up
digitalWrite(addMinPin,HIGH);// this line enables pull-up
pinMode(13,OUTPUT); // we flash the LED each second
pinMode(resetPin, INPUT); // a button on this pin resets the time
pinMode(addHourPin, INPUT);//add hours to counter
pinMode(addMinPin, INPUT);//add hours to counter
DateTime.sync( 0 ); // set time to zero
/*Restoring Time after every power failure*/
restoreDay = EEPROM.read(10); //Read days passed
restoreTime = EEPROM.read(0); //read hours passed
if (restoreDay !=0) {
restoreTime = (restoreTime*3600+(restoreDay-1)*86400); //add days and hours to datetima counter
DateTime.sync(restoreTime);
}
delay (30);
}
void loop(){
/* --------- Prints time in hours, minutes and seconds ----------- */
DateTime.available(); // needed to refresh the clock time
/* GENERAL reset pin. Everithing resets to zero when presed*/
if(digitalRead(resetPin) == HIGH){
for (int u = 0; u < 20; u++)
EEPROM.write(u, 0); // write a 0 to all 512 bytes of the EEPROM
DateTime.sync( 0 ); // reset time to zero if button pressed
}
time_t timeNow = DateTime.now();
time_t customTime = timeNow;
if(digitalRead(addHourPin) == HIGH){
customTime += (86400 * 1); //add 3600 sec for 1 time(for test,adds a day)
DateTime.sync(customTime); // increase hour by one (1 day)
}
if(digitalRead(addMinPin) == HIGH){
customTime += (60 * 1);
delay (100);
DateTime.sync(customTime); // increase min by one
}
//... BACKUP TIME EVERY 1 HOUR...//
if ((DateTime.Minute) == 1 && (DateTime.Second)==10 ) {
eepromTime = DateTime.Hour;
EEPROM.write(10,eepromDay);
eepromDay = DateTime.Day ;
EEPROM.write(0,eepromTime);
}
// lcd PRINT
lcd.setCursor(0,0);
lcd.print("Hr:min:sec");
lcd.setCursor(12,0);
lcd.print(DateTime.Hour,DEC); // prints total hours
lcd.print(":");
lcd.print(DateTime.Minute,DEC); // and minutes since reset
lcd.print(":");
lcd.print(DateTime.Second,DEC); // and sec since reset
lcd.print(" "); // clears second digit every minute
lcd.setCursor(0,1);
lcd.print("Project Day");
lcd.setCursor(13,1);
lcd.print(DateTime.Day,DEC); // prints total days
lcd.print(" ");
/*test perposes lines*/
lcd.setCursor(0,3);
lcd.print("epromDay ");
lcd.print(eepromDay);
lcd.print(" time ");
lcd.print( eepromTime );
lcd.print(" ");
digitalWrite(13, LOW); //light the LED every second
delay (10);
digitalWrite(13, HIGH); //
}