Help with coding for a clock

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
fruitkid101
 
Posts: 17
Joined: Mon Mar 08, 2010 2:49 am

Help with coding for a clock

Post by fruitkid101 »

Hey so I'm building an lcd clock with arduino. I'm using the ds 1307 breakout board for time keeping. Here's my problem, say I set the clock at 11:00 in the morning, the clock will keep time, but if it shuts off, then when I turn it on again, the clock will start again at 11:00 instead of keeping time. Here's my code, help?

#include "LiquidCrystal.h"
#include <Wire.h>
#include "RTClib.h"

#define LCD_I2C_ADDR 0
#define LCD_COLUMNS 16
#define LCD_ROWS 2

#define DATE_ROW 0
#define DATE_COL 0

#define TIME_ROW 0
#define TIME_COL 8

#define BANNER_ROW 1
#define BANNER_COL 1

RTC_Millis RTC;
LiquidCrystal lcd(LCD_I2C_ADDR);
RTC_DS1307 rtc;

void setup () {
Serial.begin(57600);

Wire.begin();

// initialize the LCD
lcd.begin(LCD_COLUMNS, LCD_ROWS);
lcd.setBacklight(HIGH);
lcd.clear();
// following line sets the RTC to the date & time this sketch was compiled

RTC.begin(DateTime(__DATE__,__TIME__));

lcd.setCursor(BANNER_COL, BANNER_ROW);
lcd.print("Cruz's BackPack");
}

void print_padded_digit(int value) {
if (value < 10) {
lcd.print('0');
}
lcd.print(value, DEC);
}

//! Print the month, day, and time to the LCD.
/*!
Prints the date in mm/dd format and the time in HH:MM:SS to the LCD.
\param now the current date and time
*/
void print_date_time(DateTime *now) {
lcd.setCursor(DATE_COL, DATE_ROW);
print_padded_digit(now->month());
lcd.print('/');
print_padded_digit(now->day());

lcd.setCursor(TIME_COL, TIME_ROW);
print_padded_digit(now->hour());
lcd.print(':');
print_padded_digit(now->minute());
lcd.print(':');
print_padded_digit(now->second());
}

void loop () {
DateTime now = RTC.now();

Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();

Serial.print(" seconds since 1970: ");
Serial.println(now.unixtime());

// calculate a date which is 7 days and 30 seconds into the future
DateTime future (now.unixtime() + 7 * 86400L + 30);

Serial.print(" now + 7d + 30s: ");
Serial.print(future.year(), DEC);
Serial.print('/');
Serial.print(future.month(), DEC);
Serial.print('/');
Serial.print(future.day(), DEC);
Serial.print(' ');
Serial.print(future.hour(), DEC);
Serial.print(':');
Serial.print(future.minute(), DEC);
Serial.print(':');
Serial.print(future.second(), DEC);
Serial.println();

Serial.println();
delay(1000);



print_date_time(&now);


}

User avatar
pburgess
 
Posts: 4161
Joined: Sun Oct 26, 2008 2:29 am

Re: Help with coding for a clock

Post by pburgess »

Issue is right here, in the setup function:

Code: Select all

RTC.begin(DateTime(__DATE__,__TIME__));
This sets the clock to the time the sketch was originally compiled, even if you apply power to the Arduino much later (hours, days, whatever).

The easiest approach is to have two sketches. One simply sets the time using the above approach...when you compile and upload this to the board, the time is set (to within a few seconds). The second sketch, your main clock program, does not include this function call...it just displays the time as read from the RTC.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”