Help Needed Building Clock

I just happen to be playing around with RTC chips and I use a 16x2 LCD all the time for my testing. I have a little breadboard that I have wired up with a Nano, 16x2 LCD and currently a PCF8563 RTC. A couple days ago it was a DS1307. I think next I will play with a DS1302.

I use a modified version of the Time library. It has most of the functions that I need. I added the strftime() function to make it easier to format time in different ways.

Here is a little sketch I wrote to check for clock drift. I wanted to know how much the Arduino time drifted when compared to the DS1307. I also check the time with time.gov.

/* The LCD is usually interfaced via 16 pins which are labelled as shown below:
    LCD Pin
    1. GND - Ground
    2. VDD - 3 - 5V
    3. VO  - Contrast
    4. RS  - Register Select - Command (0) or Character (1)
    5. RW  - Read/Write - Write (0) or Read (1)
    6. E   - Enable - Enable data transmit (0)
    7. DB0 - Data Bit 0
    8. DB1 - Data Bit 1
    9. DB2 - Data Bit 2
   10. DB3 - Data Bit 3
   11. DB4 - Data Bit 4 - used in 4 bit operation
   12. DB5 - Data Bit 5 - used in 4 bit operation
   13. DB6 - Data Bit 6 - used in 4 bit operation
   14. DB7 - Data Bit 7 - used in 4 bit operation
   15. BL1 - Backlight +
   16. BL2 - Backlight -
*/
//   I2C
//   A4 - Data  (SDA)
//   A5 - Clock (SCL)
                         //Connections to Arduino
                         //LCD        Ardunino
                         // 1. GND    N/A
                         // 2. VDD    N/A
                         // 3. VO     N/A  (Tap off a 5K - 10K pot across VCC and Ground)
#define LCD_RS       12  // 4. RS     D12
                         // 5. RW     GND
#define LCD_ENABLE   11  // 6. E      D13
                         // 7. DB0    None
                         // 8. DB1    None
                         // 9. DB2    None
                         //10. DB3    None
#define LCD_DB4       4  //11. DB4    D4
#define LCD_DB5       6  //12. DB5    D6
#define LCD_DB6       7  //13. DB6    D7
#define LCD_DB7       8  //14. DB7    D8
#define LCD_Backlight 9  //15. BL1    Emitter of 2N3904, Collector to VCC, Base to D6 via 10K resistor
                         //16. BL2    GND

//   I2C
//   A4 - Data  (SDA)
//   A5 - Clock (SCL)
#include <Wire.h>
#include <Time.h>
#include <DS1307.h>
#include <LiquidCrystal.h>
static time_t Start_t;
LiquidCrystal lcd(LCD_RS, LCD_ENABLE, LCD_DB4, LCD_DB5, LCD_DB6, LCD_DB7);
unsigned char LED_State;
void setup () {
  Serial.begin(9600);
//Serial.println("\nDate:" __DATE__ "\tTime:" __TIME__);
  Wire.begin();

  lcd.begin(16, 2);
  lcd.clear();
  pinMode(LCD_Backlight, OUTPUT); analogWrite(LCD_Backlight, 128); // Set the brightness of the backlight
  pinMode(13, OUTPUT);

  DS1307::SetFromCompileDateTime(__DATE__ __TIME__);
  time_t t = DS1307::getTime();
  setTime(t);

  Serial.println("\nFine tune the time.");
  Serial.println("Press (-) to decrease time by 1 second.");
  Serial.println("Press (+) to increase time by 1 second.");
  Serial.println("'X' when finished.");
  char c, szBuff[16];
  do {
    Start_t = secs();
    while (!Serial.available() &&
           Start_t+15 > secs()) {
      //DisplayDateTime();
      strftime(szBuff, sizeof(szBuff), "%m/%d %T");
      lcd.setCursor(0, 0); lcd.print(szBuff);
      Serial.print(szBuff); Serial.println("\t");
      delay(200);
    }
    c=Serial.read();  Serial.println((byte)c, HEX);
    switch (c) {
      case '+': adjustTime(1); break;
      case '-': adjustTime(-1); break;
    }  /* switch */
  } while (c!='x' && c!='X' && c!=-1);
  t = now();
  DS1307::setTime(t);
  setSecs(t);
  Start_t = t;
}

void loop() {
  tmElements_t DS1307_tm;
  time_t DS1307_t, Arduino_t;

  DS1307::getTime(DS1307_tm);
  DS1307_t = makeTime(DS1307_tm);

  Arduino_t = secs();

  DisplayDateTime();

  lcd.setCursor(0, 1);  lcd.print("                ");
  lcd.setCursor(0, 1);
  int iLapsed = DS1307_t - Start_t;
  int iDiff = DS1307_t - Arduino_t;
  lcd.print(iDiff);
  if (iDiff != 0) {
    lcd.print(", D:");  lcd.print(iLapsed / iDiff);
  }

  digitalWrite(13, (LED_State=!LED_State));   // Toggle
  delay(1000);
}

void DisplayDateTime(void) {
  char szBuffer[16];
  strftime(szBuffer, sizeof(szBuffer), "%m/%d %T");
  lcd.setCursor(0, 0);
  lcd.print(szBuffer);
}

I also added the 'secs()' function to the Arduino system. I proposed to the powers that be that it would be a good addition to go along with the millis() and micros() functions. I also added a way to set the seconds so you could count the number of seconds from a known starting point like 1970/1/1. I was testing that as well.

If you are interested, I can send the code to strftime() but it is quite large.

Let me know if you have any questions about the code. I may have an EE question for you.