RTC help

Im having problems with my RTC.

I have copied the code into my sketch and it compiles without any errors. but when the time displays on the serial monitor it goes from 17:18 - 17:20 and then back again. any ides what im doing wrong?

/*
  Digital out 38 is connected to a pump. If the temperature changes the pump stops. 
  
    
  created 14 March. 2012
  modified ............
  by   
  This example code is in the public domain.
  
  */
 
// These constants won't change.  They're used to give names
 // to the pins used:
 #include <Time.h>  
 #include <Wire.h>  
 #include <DS1307RTC.h>  // a basic DS1307 library that returns time as a time_t
 #include <math.h>
 #define ThermistorPIN 0   
 const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
 const int analogOutPin = 9; // Analog output pin that the LED is attached to
 const int OutPin = 38;

 int sensorValue = 0;        // value read from the pot
 int outputValue = 0;        // value output to the PWM (analog out)
 float vcc = 4.99;                       // only used for display purposes, if used
                                        // set to the measured Vcc.
float pad = 4703;                       // balance/pad resistor value, set this to
                                        // the measured resistance of your pad resistor
float thermr = 4400;                   // thermistor nominal resistance

float Thermistor(int RawADC) {
  long Resistance;  
  float Temp;  // Dual-Purpose variable to save space.

  Resistance=((1024 * pad / RawADC) - pad); 
  Temp = log(Resistance); // Saving the Log(resistance) so not to calculate  it 4 times later
  Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));
  Temp = Temp - 290.2;  // Convert Kelvin to Celsius                      
  
  // BEGIN- Remove these lines for the function not to display anything
  //Serial.print("ADC: "); 
  //Serial.print(RawADC); 
  //Serial.print("/1024");                           // Print out RAW ADC Number
  //Serial.print(", vcc: ");
  //Serial.print(vcc,2);
  //Serial.print(", pad: ");
  //Serial.print(pad/1000,3);
  //Serial.print(" Kohms, Volts: "); 
  //Serial.print(((RawADC*vcc)/1024.0),3);   
 // Serial.print(", Resistance: "); 
  //Serial.print(Resistance);
  //Serial.print(" ohms, ");
   
 
  // END- Remove these lines for the function not to display anything

  // Uncomment this line for the function to return Fahrenheit instead.
  //temp = (Temp * 9.0)/ 5.0 + 32.0;                  // Convert to Fahrenheit
  return Temp;                                      // Return the Temperature
}




void setup() {
   
   Serial.begin(9600);          // initialize serial communications at 9600 bps:
   setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if(timeStatus()!= timeSet) 
     Serial.println("Unable to sync with the RTC");
  else
     Serial.println("RTC has set the system time");    
   

   pinMode(OutPin, OUTPUT);     // Set up the digital pin 7 to an OUTPUT
   
}
 


void loop() {
   // read the analog in value:
  // sensorValue = analogRead(analogInPin);            
   // map it to the range of the analog out:
  // outputValue = map(sensorValue, 0, 1023, 0, 255);  
   // change the analog out value:
  // analogWrite(analogOutPin, outputValue);           
 digitalClockDisplay();  
   delay(10000);
}
void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year()); 
  Serial.println(); 
}
  void printDigits(int digits){
 
  Serial.print(":");  // utility function for digital clock display: prints preceding colon and leading 0
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);


float temp;
  temp=Thermistor(analogRead(ThermistorPIN));       // read ADC and  convert it to Celsius
 
  // print the results to the serial monitor:
  // Serial.print("sensor = " );                       
  // Serial.print(sensorValue);      
   Serial.print("\t output = ");      
   Serial.println(outputValue);   
   Serial.print("Celsius: "); 
   Serial.print(temp,2);
 // if the analog value is high enough, turn heater off:
   if (temp > 25
   ) {
     digitalWrite( OutPin, LOW);
   } 
  else {
     digitalWrite( OutPin,HIGH) ; 
  }     
      if (temp > 25
   ) {
     digitalWrite( analogOutPin, 0);
   } 
  else {
     digitalWrite( analogOutPin, 255) ; 
}
  
  
  //temp = (temp * 9.0)/ 5.0 + 32.0;                  // converts to  Fahrenheit
  //Serial.print(", Fahrenheit: "); 
  //Serial.print(temp,1);                             // display  Fahrenheit
  // wait 10 milliseconds before the next loop
   // for the analog-to-digital converter to settle
   // after the last reading:
   delay(10000);                     
}

Thanks

Dan

maybe your RTC needs to be set first or needs a new battery?

Why do you have those ten-second delays in there?

10 second delay was for the temperature control, i have a relay that cuts in if the temp gets below 25 degrees, if it is fluctuating between 25/24.9 the heater will cut in and out every second. i normally set this to a higher value.

How do i set the RTC up first?

I have tested the battery and it is fine.

RTC is plugged into comms pin 20/21 which are SDA/SCL. supply is from gnd and 3.3v on the arduino.

The DS1307 is a 5V device, is it not?

Putting massive delays right in the middle of your main control loop is far from advisable. Why not maintain a counter instead? Or implement hysteresis, so that the relay doesn't turn on until temp drops below 24, and doesn't turn off until temps rises above 26?

i have a relay that cuts in if the temp gets below 25 degrees, if it is fluctuating between 25/24.9 the heater will cut in and out every second. i normally set this to a higher value.

use 2 temperatures to prevent this behaviour.

pseudocode:

int state = LOW; // sort of OFF

void loop()
{
  float temp = readTemperature();

  if (temp > 25) state = LOW;
  if (temp < 23) state = HIGH;

  setHeater(state);
}

ok ill give the temp change a go, have removed the delay with no affect.

I have 3 other wires coming out of the RTC PCB, these are:-

SQU
Ds
Batt

What is SQU and DS, and do i have to connect these three to the Arduino?

Thanks

from my head:

SQU = square wave signal, probably one pulse per second
Batt = backup battery
Ds = ??

do you have a link of the PCB or an image?

http://www.ebay.co.uk/itm/Arduino-Tiny-I2C-RTC-DS1307-AT24C32-Real-Time-Clock-module-board-AVR-ARM-PIC-/250913326552?pt=LH_DefaultDomain_0&hash=item3a6b9985d8

using the following code i get this:-

/*
 * TimeRTCSet.pde
 * example code illustrating Time library with Real Time Clock.
 *
 * RTC clock is set in response to serial port time message 
 * A Processing example sketch to set the time is inclided in the download
 */

#include <Time.h>  
#include <Wire.h>  
#include <DS1307RTC.h>  // a basic DS1307 library that returns time as a time_t


void setup()  {
  Serial.begin(9600);
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if(timeStatus()!= timeSet) 
     Serial.println("Unable to sync with the RTC");
  else
     Serial.println("RTC has set the system time");      
}

void loop()
{
  if(Serial.available())
  {
     time_t t = processSyncMessage();
     if(t >0)
     {
        RTC.set(t);   // set the RTC and the system time to the received value
        setTime(t);          
     }
  }
   digitalClockDisplay();  
   delay(1000);
}

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year()); 
  Serial.println(); 
}

void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

/*  code to process time sync messages from the serial port   */
#define TIME_MSG_LEN  11   // time sync to PC is HEADER followed by unix time_t as ten ascii digits
#define TIME_HEADER  'T'   // Header tag for serial time sync message

time_t processSyncMessage() {
  // return the time if a valid sync message is received on the serial port.
  while(Serial.available() >=  TIME_MSG_LEN ){  // time message consists of a header and ten ascii digits
    char c = Serial.read() ; 
    Serial.print(c);  
    if( c == TIME_HEADER ) {       
      time_t pctime = 0;
      for(int i=0; i < TIME_MSG_LEN -1; i++){   
        c = Serial.read();          
        if( c >= '0' && c <= '9'){   
          pctime = (10 * pctime) + (c - '0') ; // convert digits to a number    
        }
      }   
      return pctime; 
    }  
  }
  return 0;
}

17:23:02 20 8 2035
17:23:03 20 8 2035
17:23:04 20 8 2035
17:23:05 20 8 2035
17:23:06 20 8 2035
17:23:07 20 8 2035
17:23:08 20 8 2035
17:18:09 12 6 2041
17:18:10 12 6 2041
17:18:11 12 6 2041
17:18:12 12 6 2041
17:18:13 12 6 2041
17:18:14 12 6 2041
17:18:15 12 6 2041
17:18:16 12 6 2041
17:18:17 12 6 2041

It counts up in seconds with the wrong time and then resets for some reason?

So i finally managed to work out how to set the time using the following code and the serial monitor input:-

/* 
 * TimeSerial.pde
 * example code illustrating Time library set through serial port messages.
 *
 * Messages consist of the letter T followed by ten digit time (as seconds since Jan 1 1970)
 * you can send the text on the next line using Serial Monitor to set the clock to noon Jan 1 2010
 T1262347200  
 T1332509000 T13:23:20 23 3 2012
 T1332272300 19:38:20 20 3 2012
 *
 * A Processing example sketch to automatically send the messages is inclided in the download
 */ 
 
#include <Time.h>  

#define TIME_MSG_LEN  11   // time sync to PC is HEADER followed by unix time_t as ten ascii digits
#define TIME_HEADER  'T'   // Header tag for serial time sync message
#define TIME_REQUEST  7    // ASCII bell character requests a time sync message 

void setup()  {
  Serial.begin(9600);
  setSyncProvider( requestSync);  //set function to call when sync required
  Serial.println("Waiting for sync message");
}

void loop(){    
  if(Serial.available() ) 
  {
    processSyncMessage();
  }
  if(timeStatus()!= timeNotSet)   
  {
    digitalWrite(13,timeStatus() == timeSet); // on if synced, off if needs refresh  
    digitalClockDisplay();  
  }
  delay(1000);
}

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year()); 
  Serial.println(); 
}

void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

void processSyncMessage() {
  // if time sync available from serial port, update time and return true
  while(Serial.available() >=  TIME_MSG_LEN ){  // time message consists of a header and ten ascii digits
    char c = Serial.read() ; 
    Serial.print(c);  
    if( c == TIME_HEADER ) {       
      time_t pctime = 0;
      for(int i=0; i < TIME_MSG_LEN -1; i++){   
        c = Serial.read();          
        if( c >= '0' && c <= '9'){   
          pctime = (10 * pctime) + (c - '0') ; // convert digits to a number    
        }
      }   
      setTime(pctime);   // Sync Arduino clock to the time received on the serial port
    }  
  }
}

time_t requestSync()
{
  Serial.print(TIME_REQUEST);  
  return 0; // the time will be sent later in response to serial mesg
}

The problem now is that the time goes tits......as shown below.....

19:43:16 20 3 2012
19:43:17 20 3 2012
19:43:18 20 3 2012
19:43:19 20 3 2012
7197:437:20 720 73 72012
7197:437:21 720 73 72012
7197:437:22 720 73 72012
7197:437:23 720 73 72012
7197:437:24 720 73 72012

After 5 mins or 300 seconds exactly! is there a problem with my dodgy ebay RTC? or am i missing something?

do i need to input the time in to memory or something?

Thanks

Dan