Go Down

Topic: RTC Gurus please Help me (Read 2124 times) previous topic - next topic

Mikee

Hi,
I spend over 15 hours fighting to get my RTC module one like thishttp://www.emartee.com/product/42059/Tiny%20RTC%20DS1307%20Shield%20V2.0) to work. I checked at least 100 times connectors - set as on webpage, red 100 topics on this forum but still cant make it run. I'm using IDE 1.0.3 with this sketch:
Code: [Select]
// Date and time functions using just software, based on millis() & timer

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

RTC_Millis RTC;

void setup () {
    Serial.begin(57600);
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.begin(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(" 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 serial monitor comes out my time:
Code: [Select]
2013/3/3 20:40:36
seconds since 1970: 1362343236
now + 7d + 30s: 2013/3/10 20:41:6

2013/3/3 20:40:39
seconds since 1970: 1362343239
now + 7d + 30s: 2013/3/10 20:41:9

2013/3/3 20:40:42
seconds since 1970: 1362343242
now + 7d + 30s: 2013/3/10 20:41:12
but after I close serial monitor and open it later it starts on same time. When I upload this code with no RTC attached happens same thing - as RTC wouldn't be there anytime.
I changed the battery - same thing. Is there any code to check if RTC module is good? Can be this affected with many different libraries for DS 1307 i have in Libraries folder(I think I downloaded and tried all of them  :))
THANKS a lot I'm desperate...

majenko

What does this say to you?

Code: [Select]

void setup () {
    Serial.begin(57600);
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.begin(DateTime(__DATE__, __TIME__));
}

Mikee

But how can I modify this sketch to set the right time?

majenko

The whole point of the RTC is that you don't *need* to set the "right" time.  It remembers it.

I suggest two sketches.  One which does the initial setting of the time and storing it in the RTC, and the other "working" sketch which uses the time in the RTC.

The way you have it at the moment won't even remotely work.  Every time you connect to the serial port the Arduino resets.  That will reset the time in the RTC to the time the sketch was compiled.

Mikee

Alright, Can you give me clue what sketches to use, please?

majenko

Well, you could use a sketch of the Mona Lisa...

Or you could simply split your current sketch in two - remove the bit that sets the time from the existing sketch and put it in a new sketch.

Mikee

Well, this didn't help me a lot.I cant compile Mona Lisa :)(not declared in this scope :)), and I'm beginner, need more dummies like stuff. Sorry 

dannable

Compile and upload your program again. When you do this it replaces the tokens __DATE__ and  __TIME__ with the current date time.

Then without unplugging the Arduino, remark out the line

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

And compile and upload the sketch again.
Beginners guide to using the Seeedstudio SIM900 GPRS/GSM Shield

Mikee

Thanks Dannable,
I loaded code
Code: [Select]
#include <Wire.h>
#include "RTClib.h"

RTC_Millis RTC;

void setup () {
    Serial.begin(57600);
    // following line sets the RTC to the date & time this sketch was compiled
   RTC.begin(DateTime("Mar 04 2013","20:20:20"));
}

void loop () {
    DateTime now = RTC.now();
   
    Serial.print(now.year(), DEC);
.
.
.

serial print was ok like:
Code: [Select]
2013/3/4 20:20:20
seconds since 1970: 1362428420
now + 7d + 30s: 2013/3/11 20:20:50

After that I remarked RTC setup
Code: [Select]
void setup () {
    Serial.begin(57600);
    // following line sets the RTC to the date & time this sketch was compiled
   //RTC.begin(DateTime("Mar 04 2013","20:20:20"));
}

void loop () {

and the serial monitor shows
Code: [Select]
2106/2/6 6:28:16
seconds since 1970: 0
now + 7d + 30s: 2106/2/13 6:28:46




majenko

You still need an RTC.begin in your second version of the code to enable the RTC module.

Try replacing
Code: [Select]

   RTC.begin(DateTime("Mar 04 2013","20:20:20"));

with a simple
Code: [Select]

  RTC.begin();

in the second version.

Mikee

Hi majenko,
cant compile, I'm getting
Code: [Select]
sketch_mar04a.ino: In function 'void setup()':
sketch_mar04a:12: error: no matching function for call to 'RTC_Millis::begin()'
D:\Arduino\arduino-1.0.3\libraries\RTClib/RTClib.h:41: note: candidates are: static void RTC_Millis::begin(const DateTime&)

majenko

Can you post a link to the RTC library you are using?


majenko

Aha...  You're not actually using your RTC module at all.

Quote from: RTClib.h

// 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?)
class RTC_Millis {


You need to be using the RTC_DS1307 class instead.  You then have RTC.begin(); and RTC.adjust(DateTime(__DATE__, __TIME__));

The begin() call turns the module on, and the adjust(...) call sets the time and date in the RTC module itself - only ever needs doing once to set the time initially.

johncc


Its the one from adafruit tutorial on github:https://github.com/adafruit/RTClib


If you compare the  "ds1307" example with the "softrtc" example from the library, it will become clear...

You want to use the "ds1307" example.  :)

Cheers,
John

Go Up