jremington:
The program you did not post is probably setting the time, each time it runs.
So I just ran a quick test.
Compiled and uploaded the code with the set clock in it. That set the clock.
Commented the line of code out, and compiled and uploaded it again.
Did not remove power during this. The clock is running correctly so now, it is time to remove the power from the rtc module, with a hot battery on it…
The program freezes at this case it is showing 2:39:25 on the screen. Now reconnect the power
Jumped to 2:40:16 which is o.k.
NOW hit reset on the arduino…
And it works.
What I forgot to do was to comment the line out and RECOMPILE and download. Which meant when the arduino was powered down the original code that set the time was still in it…
Thanks for the slap up the side of the head.
My next task for this program is creating a simple edit routine so that I can set the time from the display keypad. And the next feature is being able to set and respond to two daily alarms
Once that is done it will be a nice skeleton for a control project. In this case it will be my cat feeder
.
Here is the code. to set it, I uncomment a line, change the values, compile and download it. What I need to do and forgot to do, is to comment the line out, recompile and download it.
Thank you so much for slapping me up the side of the head… 
#include "Wire.h"
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
#define DS3231_I2C_ADDRESS 0x68
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return( (val/16*10) + (val%16) );
}
void setup()
{
Wire.begin();
lcd.begin(16,2);
//lcd.print("hello world >");
Serial.begin(9600);
pinMode(10, OUTPUT);
digitalWrite(10,LOW); // turn the backlight off
digitalWrite(10,HIGH); // turn the backlight on
// set the initial time here:
// DS3231 seconds, minutes, hours, day of week, date, month, year
//setDS3231time(20,21,14,1,24,5,15); <<<<<<<<<<<<<<<<<< uncomment to set the time.
}
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
dayOfMonth, byte month, byte year)
{
// sets time and date data to DS3231
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set next input to start at the seconds register
Wire.write(decToBcd(second)); // set seconds
Wire.write(decToBcd(minute)); // set minutes
Wire.write(decToBcd(hour)); // set hours
Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
Wire.write(decToBcd(month)); // set month
Wire.write(decToBcd(year)); // set year (0 to 99)
Wire.endTransmission();
}
void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year,
char *amp)
{
byte ampm=0;
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set DS3231 register pointer to 00h
Wire.endTransmission();
Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
// request seven bytes of data from DS3231 starting from register 00h
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f);
if(*hour >12)
{
*hour=*hour -12;
*amp='P';
}
else
{
*amp='A';
}
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
void displayTime()
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
char ampm;
// retrieve data from DS3231
readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
&year,&m);
// send it to the serial monitor
lcd.setCursor(0,0) ; // column,row rather than standard...
lcd.print(hour, DEC);
Serial.print(hour, DEC);
// convert the byte variable to a decimal number when displayed
Serial.print(":");
lcd.print(":");
if (minute<10)
{
Serial.print("0");
lcd.print("0");
}
Serial.print(minute, DEC);
Serial.print(":");
lcd.print(minute, DEC);
lcd.print(":");
if (second<10)
{
Serial.print("0");
lcd.print("0");
}
Serial.print(second, DEC);
Serial.print(" ");
Serial.print(ampm);
Serial.print("M");
Serial.print(" ");
lcd.print(second, DEC);
lcd.print(" ");
lcd.print(ampm);
lcd.print("M");
lcd.print(" ");
lcd.setCursor (0,1);
switch(dayOfWeek){
case 1:
{
Serial.print("Sunday ");
lcd.print("Sun. ");
break;
}
case 2:
{
Serial.print("Monday ");
lcd.print("Mon. ");
break;
}
case 3:
{
Serial.print("Tuesday ");
lcd.print("Tue. ");
break;
}
case 4:
{
Serial.print("Wednesday");
lcd.print("Wed. ");
break;
}
case 5:
{
Serial.print("Thursday ");
lcd.print("Thur. ");
break;
}
case 6:
{
Serial.print("Friday ");
lcd.print("Fri. ");
break;
}
case 7:
{
Serial.print("Saturday ");
lcd.print("Sat. ");
break;
}
}
if(month < 10)
{
Serial.print(" ");
}
Serial.print(month, DEC);
Serial.print("/");
Serial.print(dayOfMonth, DEC);
Serial.print("/");
Serial.print(year+2000, DEC);
Serial.println();
if (month < 10)
{
lcd.print(" ");
}
lcd.print(month, DEC);
lcd.print("/");
lcd.print(dayOfMonth, DEC);
lcd.print("/");
lcd.print(year+2000, DEC);
}
void loop()
{
displayTime(); // display the real-time clock data on the Serial Monitor,
delay(1000); // every second
}