RTC Issue

Years ago I bought some Arduino stuffs and got sidetracked with other hobbies and shelved everything before really learning anything. Now I've found a little project and have dusted off the ol' starter kit. The project is this one: http://www.instructables.com/id/Arduino-Timer-With-OnOff-Set-Point/

Downloaded the Arduino software and connected everything per the first diagram and loaded the Set_Time_RTC.ino fine and everything worked, but the time was wrong. Googled a bunch and found a thread on this forum where another person found success with a "softrtc" file, which was missing from the library in the Instructables link, but that's sorted now and I can load the softrtc sketch and the correct date is shown via serial monitor. Sweet!

Second step is to get the time displayed on the LCD and it seems something in the Clock_with_RTC_LCD.ino file is redundantly trying to set the RTC, because it causes the correct time to go back to the wrong time. Can someone point me in a direction with where that's happening? Is it the two lines below "//--------Show Actual Time On LCD--------//"?

Here's the Clock_with_RTC_LCD.ino code from the link:

//NJarpa
//Simple clock with RTC 1307

#include <LiquidCrystal.h>   //Libraries
#include <RTClib.h>
#include <Wire.h>

LiquidCrystal lcd(2, 3, 4, 5, 6, 7); //Pines arduino a lcd

//LCD 1602
//2pin to RS, 3pin to EN, D4 to 4pin, D5 to 5pin, D6 to 6pin, D7 to 7pin
//Contrast=  10K potentiometer 1 to Vcc, 2(center)to VO, 3 to GND
//Backlight= K to GND(with 1K resistor), A to Vcc
//Vss to GND , Vdd to +5v
RTC_DS1307 RTC;                     // define the Real Time Clock object

//RTC 1307
//SDA to analog 4,  SCL to analog 5 

void setup(){
     Wire.begin();
     RTC.begin();
     lcd.begin(16, 2); // Configura lcd numero columnas y filas
     
     lcd.setCursor(0,0);  //Show "TIME" on the LCD
     lcd.print("TIME");  
}

void loop(){
//--------Show Actual Time On LCD--------//
DateTime now = RTC.now();        // Clock call
now = RTC.now();
lcd.setCursor(5,0);                 
if(now.hour() < 10)
{
lcd.print("0");
}
lcd.print(now.hour(), DEC); //Print hour
lcd.print(':');
if(now.minute() < 10)
{
lcd.print("0");
}
lcd.print(now.minute(), DEC); //Print min
lcd.print(':');
if(now.second() < 10)
{
lcd.print("0");
}
lcd.print(now.second(), DEC); //Print sec


}

it seems something in the Clock_with_RTC_LCD.ino file is redundantly trying to set the RTC

Nothing in that code is setting the RTC. "The wrong time" is meaningless without knowing what time it is showing, and what time it really is.

The link had me set the RTC with the following, which sets the time wrong. Wrong being something like +4hrs from the time on my computer.

#include <RTClib.h>

// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 RTC;
void setup () {
Serial.begin(57600);
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
// uncomment it & upload to set the time, date and start run the RTC!
RTC.adjust(DateTime(__DATE__, __TIME__));
}
}
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(" since 1970 = ");
Serial.print(now.unixtime());
Serial.print("s = ");
Serial.print(now.unixtime() / 86400L);
Serial.println("d");
// calculate a date which is 7 days and 3
DateTime future (now.unixtime() + 7 * 8);
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(3000);
}

And this is softrtc, which is what correctly referenced my computer time (+30sec):

// Date and time functions using just software, based on millis() & timer

#include <Arduino.h>
#include <Wire.h>         // this #include still required because the RTClib depends on it
#include "RTClib.h"

#if defined(ARDUINO_ARCH_SAMD)
// for Zero, output on USB Serial console, remove line below if using programming port to program the Zero!
   #define Serial SerialUSB
#endif

RTC_Millis rtc;

void setup () {
    Serial.begin(57600);
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.begin(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}

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(3000);
}

In the code you first posted.

These two lines do the same thing. Read the time from the RTC into the DateTime object called 'now' so you can get all the detailed parts out of the time like current hour, minute, second, etc. Just keep the 1st one.

DateTime now = RTC.now(); // Clock call
now = RTC.now();

Try putting this next line in setup or uncommenting the one there already and running the sketch once to set the RTC time the 1st time. Then you can comment it out for later use as the RTC should keep the time moving from this date and you don't want to keep putting it back.

rtc.adjust(DateTime(2017,12,22,0,0,00)); set with year,month,day,hour,min,second

If you run the sketch once with this line and then again without it you should see a new updated time from the RTC.

The link had me set the RTC with the following, which sets the time wrong. Wrong being something like +4hrs from the time on my computer.

if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
// uncomment it & upload to set the time, date and start run the RTC!
RTC.adjust(DateTime(__DATE__, __TIME__));
}

This code never actually sets the time. The RTC.adjust() function is within a conditional statement which is false and is not entered. The RTC is indeed running, it just does't have the correct time.

wzaggle:
In the code you first posted.

These two lines do the same thing. Read the time from the RTC into the DateTime object called 'now' so you can get all the detailed parts out of the time like current hour, minute, second, etc. Just keep the 1st one.

DateTime now = RTC.now(); // Clock call
now = RTC.now();

Try putting this next line in setup or uncommenting the one there already and running the sketch once to set the RTC time the 1st time. Then you can comment it out for later use as the RTC should keep the time moving from this date and you don't want to keep putting it back.

rtc.adjust(DateTime(2017,12,22,0,0,00)); set with year,month,day,hour,min,second

If you run the sketch once with this line and then again without it you should see a new updated time from the RTC.

Thanks very much for the reply!

Here's what I did...

Ran the clear eeprom sketch.

Ran softrtc.ino after uncommenting rtc.adjust and manually entering the date/time. The time I entered was correctly displayed on the serial monitor (note that softrtc.ino correctly referenced my computer time as it was).

Ran it again after re-commenting rtc.adjust (it went back to referencing my computer time +30sec).

Ran Clock_with_RTC_LCD.ino (from the first post) after commenting out the now = RTC.now(); line.

It did the same thing as if I had run the first program in post #2, which is where it's referencing some random time that about 5hrs 10min ahead of now.

cattledog:

if (! RTC.isrunning()) {

Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
// uncomment it & upload to set the time, date and start run the RTC!
RTC.adjust(DateTime(DATE, TIME));
}




This code never actually sets the time. The RTC.adjust() function is within a conditional statement which is false and is not entered. The RTC is indeed running, it just does't have the correct time.

Thanks and yes, the RCT doesn't seem to stop running. Apologies in advance, I'm not conveying the issue very well. I can run the sketch named "softrtc.ino" and it correctly references my computer time +30sec, which is great. The problem is when I try to run the second sketch(Clock_With_RTC_LCD), which is just supposed to get the time to display on an LCD, but when I run it, the time goes ahead 5hrs exactly as if I had run the first sketch(Set_Time_RTC.ino). I abandoned using that file because it was setting the wrong time and I found another thread on this forum where someone had success setting the RTC using softrtc.ino, so that's what I using before loading Clock_With_RTC_LCD.ino.

Another way to look at it could be to fix Set_Time_RTC.ino in the tutorial instead of abandoning it for the softrtc.ino file, because it seems like there's some connection between Set_Time_RTC.ino and Clock_With_RTC_LCD.ino since they both change the time to +5hrs. I'll try to compare the two files in post #2 side-by-side and see if I can spot whatever it is in the Set_Time_RTC.ino that's not referencing the time correctly. What I've been trying to do in this thread is figure out why Clock_With_RTC_LCD.ino changes the correctly set time to +5hrs.

Sorry, I'm rereading this and I still don't think I've explained it well. I know this would be much easier if I halfway understood the language and weren't having to reference other people's files.

I did it! First thing I did was work from the bottom up matching Set_Time_RTC.ino to softrtc.ino, uploading after a few changes to see what I had done that made the time change. I made too many changes at a time before uploading, so I didn't find it exactly, but I knew it wasn't in loop.

Next I started from the top of Clock_With_RTC_LCD.ino and started matching up a few things to what I remembered from matching the previous two. I made some changes that didn't do anything, then got an error at RTC.begin(), which I edited to match what I thought it paralleled from softrtc.ino, which was to change it to RTC.begin(DateTime(F(DATE), F(TIME)));

Uploaded and viola, time changed from +5 to current. This is a little eye opening. I want to change what the buttons do from the Instructables project and that's going to be significantly more difficult than this, which I've already spent three hours on.

The other sketch that seems to print the time off by 30sec is using the millis object which I don't think uses the RTC but instead the internal millis timer set to the time the sketch compiled (30 sec earlier). If you reset the arduino you will probably lose the time it takes to restart.

Your actual RTC is probably running but with the wrong time set. So the line that sets it inside the if never runs the adjust again.

Put this line outside and after that if (! RTC.isrunning()) code block. Set time values for maybe a few minutes ahead. Then reset the arduino to re-start the same sketch when the time you set actually arrives. Then you can comment out the line and re-download the new sketch without this line.

rtc.adjust(DateTime(2017,12,22,0,0,00)); set with year,month,day,hour,min,second

Typically you will want a way to actually set this time inside of your sketch. This would mean allowing for a way to enter the actual DateTime data from the sketch and then execute an adjust statement to set it when you need to. But you can add that later.