Go Down

Topic: DS1307 time to int? (Read 977 times) previous topic - next topic

ajax409

OK so I am trying to log voltage and amperage to a SD card. I originally logged this data only with a sample number every five seconds. The code worked great however I really wanted to have the actual time of day logged. The first setup used an uno with an Ethernet shield. Since I couldn't use the DS1307 on SPI and the SD card on the Ethernet shield at the same time I purchased a stand alone SD card writer and decided to re-write my code. I got all the separate pieces working individually but as I try and put it all together I have run into an issue when I try and save the time to an int. Below is my code:

Code: [Select]

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

RTC_DS1307 RTC; //DS1307 Real Time Clock

// int voltage_pin = 2;
// int amperage_pin = 1;

void setup()
{
Serial.begin(57600);  //Start serial comms
Wire.begin();
RTC.begin();

if (! RTC.isrunning()) {
  Serial.println("RTC is NOT Running!");
}
}

void loop()
{
  DateTime now = RTC.now();
  Serial.println(now.year(), DEC);
  int tyear = (now.year);
  Serial.println(tyear);
  delay(5000);
 
}



Basically I hope to take the int and write it to a string and save it to the log file. I added the serial println as a little debugging.

I open the serial monitor and get the following

2012
10

Basically I can see that the RTC is hooked up correctly and it shows the right year. What I cant figure out is how to store it to an int.  I have tried all kinds of things and I all ways get 10.

I would really appreciate any help.

marco_c

There uis s difference in how you are getting the value now.year() and (now.year). If the first version works, then use that.

Code: [Select]
  Serial.println(now.year(), DEC);
  int tyear = (now.year);
Arduino libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com

ajax409

Thank you very much for the response but even with the code as you suggest I get the same thing.

marco_c

Code: [Select]
int tyear = now.year();

does not work? Can you show the output you get?
Arduino libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com

ajax409

Thank you so much!

This is my first real experience with a RTC and that did the trick!

I must not understand the RTCLib.h and how it works, again that you very much for the help!

ajax409

Just in case anyone else looks up the thread this code now works as expected!

Code: [Select]

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

RTC_DS1307 RTC; //DS1307 Real Time Clock

// int voltage_pin = 2;
// int amperage_pin = 1;

void setup()
{
Serial.begin(57600);  //Start serial comms
Wire.begin();
RTC.begin();

if (! RTC.isrunning()) {
  Serial.println("RTC is NOT Running!");
}
}

void loop()
{
  DateTime now = RTC.now();
  Serial.println(now.year(), DEC);
  int tyear = now.year();
  Serial.println(tyear);
  delay(5000);
 
}


Thank you marco_c very much for helping me with the syntax!

Go Up