Serial Not Available from RTC

I have an Arduino Mega 2560 R3, and I am trying to use an RTC 1307 and the example code works fine, but when I put it in to my main program, it never passes the if(Serial.available).

Here is the example code I am running that works.

/*
 * 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");  
 
  //setTime(10, 22, 0, 14, 3, 2013);
  //RTC.set(now());    
}

void loop()
{
  
  if(Serial.available())
  {
     time_t t = processSyncMessage();
//     if(t >0)
 //    {
   // 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;
}

Here are the places that I have inserted code from the example in my main program.

setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if(timeStatus()!= timeSet) 
     lcd.print(F("Unable to sync with the RTC"));
  else
     lcd.print(F("RTC has set the system time"));
 if (Serial.available())
  {
   time_t t = processSyncMessage();
   setTime(t);

I’ll attach my whole main program as well.

I have been chasing my tail on this one for too long now, any help and insight would be greatly appreciated.

main4T.txt (19.9 KB)

I cannot get the code to compile due to missing libraries and don't have time to chase them down and install them but maybe the if (Serial.available()) should be if (Serial.available()>0)

Riva: I cannot get the code to compile due to missing libraries and don't have time to chase them down and install them but maybe the if (Serial.available()) should be if (Serial.available()>0)

Thanks for trying. I tried changing it to Serial.available > 0 and there was no change.

DS1307 does not have Serial interface. It uses I2C interface.

The Serial in your code refers to getting time from something PC based (network?) to set up the time in the DS1307.

CrossRoads:
DS1307 does not have Serial interface.
It uses I2C interface.

The Serial in your code refers to getting time from something PC based (network?) to set up the time in the DS1307.

That makes sense, but why does the example work though?

After CrossRoads' comment, I decided to not use the rtc library that I was, and do everything manually via the wire lib. Its working great now! :)