Pages: [1]   Go Down
Author Topic: DS1307 time to int?  (Read 795 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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.
Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1273
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
  Serial.println(now.year(), DEC);
  int tyear = (now.year);
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1273
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int tyear = now.year();
does not work? Can you show the output you get?
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
#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!
Logged

Pages: [1]   Go Up
Jump to: