The new Raspberry Pi B Model B+ is here - Pick one up in the store and check our detailed guide!

Re: Wrong return values - DS1307 RTC code question

by rbright on Mon Dec 10, 2012 11:06 pm

To follow on from my previous post I've found that in RTClib.h reference to RTC_millis which defines static function DateTime now(); which in turn is explained for those more experienced in coding in C++ in RTClib.cpp lots of good late night reading if your so inclined :)

In RTClib.h is also a note which explains a problem I've noticed as follows:
// note in RTClib.h says: RTC using the internal millis() clock, has to be initialized before use NOTE: this clock won't be correct once the millis() timer rolls over (>49d?)
My seconds count crazy on an LCD after 59 09 19 29 39 up to 99 then back on track to 10. This does not happen on the Serial Monitor

To get over the problem of the RTC not being set precisely I found this way to do it from Visual Basic & arduino sketch
http://combustory.com/wiki/index.php/RT ... ck#Summary.

Following for those who may be interested is the original code at the top of this post modified to display both on Serial Monitor & 2x16 LCD
Enjoy

/*
The circuit:
* LCD RS pin to digital pin 8 (J3/1 or PB0)
* LCD Enable pin to digital pin 9 (J3/2 or PB1)
* LCD D4 pin to digital pin 4 (J1/5 or PD4)
* LCD D5 pin to digital pin 5
* LCD D6 pin to digital pin 6
* LCD D7 pin to digital pin 7 (J1/8 or PD7)
* LCD R/W pin to ground
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
*/

// include the library code:
#include <LiquidCrystal.h>

// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 RTC;

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

void setup() {

// set up the LCD's number of columns and rows.
lcd.begin(16, 2);

// Print a message to the LCD.
lcd.setCursor (0,0);
lcd.print("About to Start...");
delay(1000); // time to see display

Serial.begin(57600); // for Serial Monitor
Wire.begin();
RTC.begin();

if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled - load off PC Time
// RTC.adjust(DateTime(__DATE__, __TIME__)); // only use if you need to reload time from PC with
// slight error of maybe 2 minutes
}
} // end of setup

void loop() {

DateTime now = RTC.now();

// Display Date
lcd.setCursor(0,0); // lcd.setCursor(col, row)
lcd.print("DATE: ");
Serial.print("DATE: ");
Serial.print(now.day(), DEC); // to serial port
lcd.print(now.day(), DEC); // to lcd
Serial.print('/');
lcd.print('/');
Serial.print(now.month(), DEC); // to serial port
lcd.print(now.month(), DEC); // to lcd
Serial.print('/');
lcd.print('/');
Serial.print(now.year(), DEC); // to serial port
lcd.print(now.year(), DEC); // to lcd

// Display Time
lcd.setCursor(0,1); // lcd.setCursor(col, row)
lcd.print("TIME: ");
Serial.print(" TIME: ");
Serial.print(now.hour(), DEC); // to serial port
lcd.print(now.hour(), DEC); // to lcd
Serial.print(':');
lcd.print(':');
Serial.print(now.minute(), DEC); // to serial port
lcd.print(now.minute(), DEC); // to lcd
Serial.print(':');
lcd.print(':');
Serial.print(now.second(), DEC); // to serial port
lcd.print(now.second(), DEC); // to lcd

// note in RTClib.h says: RTC using the internal millis() clock, has to be initialized before use
// NOTE: this clock won't be correct once the millis() timer rolls over (>49d?)

Serial.println(); // provides line feed on Serial
delay(1000);
} // end of loop - that's all folks...
rbright
 
Posts: 3
Joined: Mon Dec 10, 2012 7:11 am