RTC Arduino Programming

I forgot I was doing this project… Anyways, I finally completed the Arduino code for my RTC clock. The code implements 2 main libraries, the RTCDS1307 library and LiquidCrystalI2C library. I used these extensively to help me create my code, which can be found below.

Click for Arduino Code

#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>

#include <LiquidCrystal_I2C.h>

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);

int dayofweek = 1;
int alt = 0;
int dodayofweek = 0;

void setup() {

Serial.begin(9600);
while (!Serial) ; // wait for serial
delay(200);
Serial.println(“DS1307RTC Read Test”);
Serial.println(“——————-“);

// initialize the LCD
lcd.begin();

// Turn on the blacklight and print a message.
lcd.backlight();
lcd.clear();

}

void loop() {
tmElements_t tm;

if (RTC.read(tm)) {
Serial.print(“Ok, Time = “);
print2digits(tm.Hour);
Serial.write(‘:’);
print2digits(tm.Minute);
Serial.write(‘:’);
print2digits(tm.Second);
Serial.print(“, Date (D/M/Y) = “);
Serial.print(tm.Day);
Serial.write(‘/’);
Serial.print(tm.Month);
Serial.write(‘/’);
Serial.print(tmYearToCalendar(tm.Year));
Serial.println();

//LCD Code
lcd.setCursor(5 ,1);
if((tm.Hour) < 10) {
lcd.print(“0”);
}
lcd.print(tm.Hour);
lcd.print(“:”);
if((tm.Minute) < 10) {
lcd.print(“0”);
}
lcd.print(tm.Minute);
lcd.setCursor(0, 0);
if (dayofweek == 1) {
lcd.print(“Mon”);
}
if (dayofweek == 2) {
lcd.print(“Tus”);
}
if (dayofweek == 3) {
lcd.print(“Wed”);
}
if (dayofweek == 4) {
lcd.print(“Thu”);
}
if (dayofweek == 5) {
lcd.print(“Fri”);
}
if (dayofweek == 6) {
lcd.print(“Sat”);
}
if (dayofweek == 7) {
lcd.print(“Sun”);
}
if (dayofweek > 7) {
lcd.print(“Err”);
}
if (dayofweek < 0) {
lcd.print(“Err”);
}

lcd.setCursor(6, 0);
if((tm.Day) < 10) {
lcd.print(“0”);
}
lcd.print(tm.Day);
lcd.print(“/”);
if((tm.Month) < 10) {
lcd.print(“0”);
}
lcd.print(tm.Month);
lcd.print(“/”);
lcd.print(tmYearToCalendar(tm.Year));
lcd.print(” “);

// LCD Code End

// IF HOUR IS PAST MIDNIGHT SET DO DAY OF WEEK TO 1
if ((tm.Hour) > 0) {
dodayofweek = 1;
}

// IF HOUR IS AT MIDNIGHT SET DO DAY OF WEEK TO 0
if ((tm.Hour) == 0) {
dodayofweek = 0;
}

if (dodayofweek == 1) {
dayofweek = dayofweek + 1;
}
if (dayofweek > 7) {
dayofweek = 1;
}

} else {
if (RTC.chipPresent()) {
Serial.println(“The DS1307 is stopped. Please run the SetTime”);
Serial.println(“example to initialize the time and begin running.”);
Serial.println();
} else {
Serial.println(“DS1307 read error! Please check the circuitry.”);
Serial.println();
}
delay(9000);
}

delay(500);

lcd.setCursor(7, 1);
lcd.print(” “);

delay(500);
}

void print2digits(int number) {
if (number >= 0 && number < 10) {
Serial.write(‘0’);
}
Serial.print(number);
}

[collapse]

I tested the code throughout the programming, adding features and fixing bigs as they arrose. Here are some images: