DS1307 RTC question

Hi All, I've got a DS1307 RTC module hooked up but I'm confused about something. I uploaded the following code in order to set the date and time. I opened the Serial Monitor to look at the values and I see the clock values. Great. However....if I close the Serial Monitor window, then reopen it, I see that the clock values start over. I thought that once I set the clock it would run contiguously. Even on battery power (if none provided from the Arduino).

What am I doing wrong?

// Maurice Ribble
// 4-17-2008
// http://www.glacialwanderer.com/hobbyrobotics

// This code tests the DS1307 Real Time clock on the Arduino board.
// The ds1307 works in binary coded decimal or BCD.  You can look up
// bcd in google if you aren't familior with it.  There can output
// a square wave, but I don't expose that in this code.  See the
// ds1307 for it's full capabilities.

#include "Wire.h"
#define DS1307_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) );
}

// Stops the DS1307, but it has the side effect of setting seconds to 0
// Probably only want to use this for testing
/*void stopDs1307()
{
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.send(0);
  Wire.send(0x80);
  Wire.endTransmission();
}*/

// 1) Sets the date and time on the ds1307
// 2) Starts the clock
// 3) Sets hour mode to 24 hour clock
// Assumes you're passing in valid numbers

// UNCOMMENT THE FOLLOWING TO SET THE DATE/TIME

/*
void setDateDs1307(byte second,        // 0-59
                   byte minute,        // 0-59
                   byte hour,          // 1-23
                   byte dayOfWeek,     // 1-7
                   byte dayOfMonth,    // 1-28/29/30/31
                   byte month,         // 1-12
                   byte year)          // 0-99
{
   Wire.beginTransmission(DS1307_I2C_ADDRESS);
   Wire.send(0);
   Wire.send(decToBcd(second));    // 0 to bit 7 starts the clock
   Wire.send(decToBcd(minute));
   Wire.send(decToBcd(hour));      // If you want 12 hour am/pm you need to set
                                   // bit 6 (also need to change readDateDs1307)
   Wire.send(decToBcd(dayOfWeek));
   Wire.send(decToBcd(dayOfMonth));
   Wire.send(decToBcd(month));
   Wire.send(decToBcd(year));
   Wire.endTransmission();
  
}

*/

// Gets the date and time from the ds1307
void getDateDs1307(byte *second,
          byte *minute,
          byte *hour,
          byte *dayOfWeek,
          byte *dayOfMonth,
          byte *month,
          byte *year)
{
  // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.send(0);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

  // A few of these need masks because certain bits are control bits
  *second     = bcdToDec(Wire.receive() & 0x7f);
  *minute     = bcdToDec(Wire.receive());
  *hour       = bcdToDec(Wire.receive() & 0x3f);  // Need to change this if 12 hour am/pm
  *dayOfWeek  = bcdToDec(Wire.receive());
  *dayOfMonth = bcdToDec(Wire.receive());
  *month      = bcdToDec(Wire.receive());
  *year       = bcdToDec(Wire.receive());
}

void setup()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  Wire.begin();
  Serial.begin(9600);

  // Change these values to what you want to set your clock to.
  // You probably only want to set your clock once and then remove
  // the setDateDs1307 call.

 // UNCOMMENT THE FOLLOWING TO SET THE DATE/TIME

 /* second = 0;
  minute = 48;
  hour = 21;
  dayOfWeek = 1;
  dayOfMonth = 7;
  month = 3;
  year = 10;
  setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
  */
}

void loop()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;

  getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  Serial.print(hour, DEC);
  Serial.print(":");
  Serial.print(minute, DEC);
  Serial.print(":");
  Serial.print(second, DEC);
  Serial.print("  ");
  Serial.print(month, DEC);
  Serial.print("/");
  Serial.print(dayOfMonth, DEC);
  Serial.print("/");
  Serial.print(year, DEC);
  Serial.print("  Day_of_week:");
  Serial.println(dayOfWeek, DEC);

  delay(1000);
}

Are you sure the battery is in properly? Did you build this board yourself, and if so, are you sure you wired everything correctly?

I checked the battery, took it out, put it back in. Made sure it was good (checked on multimeter). The Arduino is a pre-built unit. I put a stack of headers on the RTC so I could plug it into a breadboard. I have SDA going to analog pin 4, SCL going to analog pin 5, GND to ground and 5V to 5V.

You may be getting the uploading/run sequence for the sketch wrong, it can be a little confusing. You need to upload the sketch twice. First you edit the sketch to include the time setting statements and then upload it. Then do not open the serial monitor, but rather edit the sketch to comment out the clock setting statements and upload again. After that, the time should stay correct between power off, resetting, serial monitor on and off, etc.

PS: To make it easier in the future you may want to have two sketches saved on your PC. Call one Set_RTC and the other Run_RTC. The first will have the time setting commands active and the second will have the time setting commands commented out. Then you only run the Set_RTC sketch when you wish to reset the time and run Run_RTC when you just want to know the time.

Lefty

okay..figured it out. I had not run the program twice (2nd time, commenting out the setup), so that's why the time was resetting.

Thank you retrolefty. I just read your comment!

Nice it's working for you. I apologize for being too lazy to look at the code ;D! Congratulations :).