Seeedstudios RTC not storing time?

Still being very new at this I cannot figure out why my RTC is not storing the time when power is unhooked. I do have the required backup battery in place, but when I go to serial monitor it will always start back to the original time and date. Any and all help is greatly appreciated.

#include <Wire.h>
#include "DS1307.h"
#include "DHT.h"
#include <Servo.h>

Servo myservo;
#define DHTPIN A0
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
int pos = 0;
int led = 2;
int led2 = 4;
DS1307 clock;//define a object of DS1307 class
void setup()
{
Serial.begin(9600);
clock.begin();
clock.fillByYMD(2013,8,8);//Jan 19,2013
clock.fillByHMS(21,24,30);//15:28 30"
clock.fillDayOfWeek(THU);//Saturday
clock.setTime();//write time to the RTC chip

Serial.println("DHTxx test!");

dht.begin();
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
myservo.attach(6);
}
void loop()
{
printTime();
}
/Function: Display time on the serial monitor/
void printTime()
{
clock.getTime();
Serial.print(clock.hour, DEC);
Serial.print(":");
Serial.print(clock.minute, DEC);
Serial.print(":");
Serial.print(clock.second, DEC);
Serial.print(" ");
Serial.print(clock.month, DEC);
Serial.print("/");
Serial.print(clock.dayOfMonth, DEC);
Serial.print("/");
Serial.print(clock.year+2000, DEC);
Serial.print(" ");
Serial.print(clock.dayOfMonth);
Serial.print("*");
switch (clock.dayOfWeek)// Friendly printout the weekday
{
case MON:
Serial.print("MON");
break;
case TUE:
Serial.print("TUE");
break;
case WED:
Serial.print("WED");
break;
case THU:
Serial.print("THU");
break;
case FRI:
Serial.print("FRI");
break;
case SAT:
Serial.print("SAT");
break;
case SUN:
Serial.print("SUN");
break;
}
Serial.println(" ");
float h = dht.readHumidity();
float t = dht.readTemperature();

// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(t) || isnan(h)) {
Serial.println("Failed to read from DHT");
} else {
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C");
}
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led2, LOW); // turn the LED off by making the voltage LOW
delay(1000);
digitalWrite(led2, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(500);

for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}

  1. Please use code tags - see instructions at the top of the forum.
   clock.fillByYMD(2013,8,8);//Jan 19,2013
   clock.fillByHMS(21,24,30);//15:28 30"
   clock.fillDayOfWeek(THU);//Saturday
   clock.setTime();//write time to the RTC chip

I'm not familiar with the library you are using - what's this doing??

Looks to me like you keep setting the time every time your sketch starts. Use one of the example programs to set the time via serial then you shouldn't need to again unless the battery goes dead.

Sorry for not using the code tag option, I will in the future. I'm assuming the lines of code you referred to set up the clock standard? I followed the instructions from the link below. Thanks for the quick response.

http://www.seeedstudio.com/wiki/Grove_-_RTC

Jimmy60 now it makes sense, do I remove that from my code after the initial upload so it doesn't reset it?

The 1307 has a flag that is set when the clock is set.

Have a look at the library documentation to see if you have a function that checks this (isrunning...), and if NOT set then set the time, otherwise don't.

I don't understand your response. I did look for some sort of documentation to explain this in laymen terms but came up with nothing. Do I write the sketch, then disconnect the RTC...rewrite the sketch to not include the setting lines and reload it? just a little confused, I am really new to this and reading and stumbling my way through this. Thanks for the help.

Do I write the sketch, then disconnect the RTC...rewrite the sketch to not include the setting lines and reload it?

That's one way.

Another is to ask the clock if the time is set. If it isn't, set it. If it is, don't set it.

dannable:
The 1307 has a flag that is set when the clock is set.

Have a look at the library documentation to see if you have a function that checks this (isrunning...), and if NOT set then set the time, otherwise don't.

Do you mean the oscillator bit? AFAIK there is no guarantee of the state of an RTC chip when the backup battery
is first connected (its SRAM), you have to program it the first time you use it after Vbat is applied.