DS-1307 reads OK on Serial.print, then reports 2/6/2106 on SD Card. How to call?

Using the ds1307 sketch from the driver example folder.
#include "SdFat.h"
#include "RTClib.h"

I have a sketch that checks the clock and reports the time and an analog value to the Serial port, and it tells the correct time. When I use the same statements to report to the SD logfile the time is wrong: 2/6/2106 6:28:16. I then decided to learn what I could playing with the DS1307 sketch. Modified the ds1307 sketch to wait a second, print the seconds again, wait 2+ more seconds and print the seconds value and they all 3 print the same value for seconds. I then tried various ways to "reread" the clock values,but any call I make crashes the compiler. I have tried restating the definition DateTime now = rtc.now(); but it doesn't like that because I am declaring the same thing twice. Using now = rtc.now(); crashes in compile.

So what actually assigns the time value to the variable and how to reread it? I do not see in the example what reassigns the value to the variable - where does the call to read the clock happen?

In my program, when I write the data to the SD card it write the reading OK but reports the time as the 2106 year, always the same. So I tried to reread the seconds value and it does not change within the same loop iteration. Next loop it is updated (~3 seconds later).
Sample output:
1/12/2017 12:3:40 40 40

You see that it prints the seconds value 3 times with delay, but it is always the same value for the same pass through the loop.

1484222620
1/12/2017 12:3:43 43 43
1484222623

Here is the code. I decided to test using the ds1307.ino sketch as here and make it delay and print the seconds 3 times: Serial.print(now.second(), DEC);

This is related to my actual problem where I print the time to the serial port and it is OK, then when I print it to the SD Card file it reports year 2106. The clock is running and works every time I load it.
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib.
// Prints the secondd field out 3 times. Does it update?
// Mod 1/12/2017 Bill Allen
//

#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;

void setup () {
while (!Serial); // for Leonardo/Micro/Zero

Serial.begin(115200);
// if (! rtc.begin()) {
rtc.begin();
// if (! rtc.isrunning()) {
// Serial.println("Couldn't find RTC");
// while (1);
// }

if (! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
// rtc.adjust(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.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print('/');
Serial.print(now.year(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.print(' ');
delay(1200);
Serial.print(now.second(), DEC);
Serial.print(' ');
delay(2200);

Serial.println(now.second(), DEC); // The consecutive 'seconds' all print the same value!
// SO, what command / statement actually does the update?

Serial.println(now.unixtime());

}

So what actually assigns the time value to the variable and how to reread it? I do not see in the example what reassigns the value to the variable - where does the call to read the clock happen?

You instantiate a DateTime object now. When you call now = rtc.now() it updates the values it contains.

Using now = rtc.now(); crashes in compile.

I don't know what you did, but this sketch compiles and runs with the updated seconds after the delay.

#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;

void setup () {
  while (!Serial); // for Leonardo/Micro/Zero

  Serial.begin(115200);
//  if (! rtc.begin()) {
    rtc.begin();
//  if (! rtc.isrunning()) {
//    Serial.println("Couldn't find RTC");
//    while (1);
//  }

  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    // rtc.adjust(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.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print('/');
    Serial.print(now.year(), DEC);
    Serial.print(" ");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.print(' ');
    delay(1200);
    now = rtc.now();
    Serial.print(now.second(), DEC);
    Serial.print(' ');
    delay(2200);
    now = rtc.now();
    Serial.println(now.second(), DEC);   // The consecutive 'seconds' all print the same value!
// SO, what command / statement actually does the update?

    Serial.println(now.unixtime());

}
    Serial.print(now.second(), DEC);
    Serial.print(' ');
    delay(1200);
    Serial.print(now.second(), DEC);
    Serial.print(' ');
    delay(2200);

    Serial.println(now.second(), DEC);   // The consecutive 'seconds' all print the same value!

I'd expect them to. The now instance hasn't changed. So, getting data from it should always result in the same data.

Hey, Bob, what's your name?
Hey, Bob, what's your name?
Hey, Bob, what's your name?

You wouldn't expect Bob, Mary, and Alice as the responses, would you? Unless Bob's a smart-ass like me.

Wow thanks. There had to be a way to re-read it but couldn't find it. I looked at the RTClib keywords, etc to try to see what options were but was not successful..

I would to see a syntax and options file - like the old DOS help ? command if I recall. That level of information for the functions and libraries.

Thanks!

I would to see a syntax and options file - like the old DOS help ? command if I recall. That level of information for the functions and libraries.

It's almost that easy. You just have to read the library files. They are in the same folder as your sketches. If you use a good source code text editor like Notepad++, the functions are shown in a functions list. Getting into the library files and working to understand what they do is a big step forward from just using the "magic" words.

Great info!

OK, Well, if you look at the sample I posted, you'll see that I had now = rtc.now(); commented out. Tried it before. Looking at it I figured that now was the variable to hold the now() data. When I put that in there, the compiler responds with:

Linking everything together...
"C:\Documents and Settings\Admin\Local Settings\Application Data\Arduino15\packages\arduino\tools\avr-gcc\4.9.2-atmel3.5.3-arduino2/bin/avr-gcc" -Wall -Os -g -flto -fuse-linker-plugin -Wl,--gc-sections -mmcu=atmega328p -o "C:\TEMP/ds1307.0.3.ino.elf" "C:\TEMP\sketch\ds1307.0.3.ino.cpp.o" "C:\TEMP\libraries\Wire\Wire.cpp.o" "C:\TEMP\libraries\Wire\utility\twi.c.o" "C:\TEMP\libraries\RTC_SD_Example\RTClib.cpp.o" "C:\TEMP/core\core.a" "-LC:\TEMP" -lm
collect2.exe: error: ld returned 5 exit status

Using library Wire at version 1.0 in folder: C:\Documents and Settings\Admin\Local Settings\Application Data\Arduino15\packages\arduino\hardware\avr\1.6.16\libraries\Wire
Using library RTC_SD_Example in folder: C:\Documents and Settings\Admin\My Documents\Arduino\libraries\RTC_SD_Example (legacy)
exit status 1
Error compiling for board Arduino/Genuino Uno

========================

I have had that "ld returned 5 exit status" a lot lately. Using XP Pro. Swapping versions of the IDE does not help. I did download a new ld.exe. I'll try the 8.1 version but 8.0 didn't help, afaict. I thought the new LD.exe fixed that. Probably many, many things to cause that.

Hey Cattledog, thanks - YES! your file does run fine. Not sure the difference in the file you posted and mine - printing them to parse... but YOUR's compiles and runs while MINE does not! I did compile and run mine without the additional now = rtc.now(); When I put the statements back in it crashes. But you's did not!

On the trail...

might be a fat hen causing the problem. Ask if you want to know what that is! haha off topic.

OK, Well, if you look at the sample I posted, you'll see that I had now = rtc.now(); commented out.

No, I don't see that in the version you posted. What you posted compiled for me and repeated the seconds in the delayed printouts like you indicated. Did the code I provided compile for you?

collect2.exe: error: ld returned 5 exit status

I understand that this is some sort of Windows XP error that I have seen addressed in other threads, where the way to fix it is to run Arduino in Windows 2000 compatibility mode.

OK, thinking about this I separated the DateTime now = rtc.now();
into:
DateTime now;
now = rtc.now();

Then it runs. It "lets" me make subsequent calls to now() without crashing the linker.
Makes me think that this may be good practice.

I did try running in Win2000 compatibility mode - no difference but I think that I will keep it in that mode, thanks!

The program I posted is the same as yours:

// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
// Prints the second field out 3 times. Does it update?
// Mod 1/12/2017 Bill Allen
// Crashes with ld returned 5 exit status. Library or what? Separated the declation and call of now() into 2 statements.

//
//

#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;

void setup () {
while (!Serial); // for Leonardo/Micro/Zero

Serial.begin(115200);
if (! rtc.begin()) {
// rtc.begin();
if (! rtc.isrunning()) {
Serial.println("Couldn't find RTC");
while (1);
}
}

if (! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
// rtc.adjust(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;
now = rtc.now(); // As a single statement, I cannot add: now = rtc.now; below.
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print('/');
Serial.print(now.year(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.print(' ');
delay(1200);
now = rtc.now();
Serial.print(now.second(), DEC);
Serial.print(' ');
delay(2200);
now = rtc.now();
Serial.println(now.second(), DEC); // Now it works - without crashing the linker.
Serial.println(now.unixtime());
// delay(1000);
}

Problem solved!

THANKS!! Breaking the declaration of now() into 2 lines did it, then I can put now = rtc.now in the file where it prints to the DS card and it works!

Problem solved!

I'm not so sure. From what I understand, the "collect2.exe: error: ld returned 5 exit status" can go away with random minor changes of adding or removing pieces of code. There is a github bug report here:

Mr.Google finds lots of references.