Usage of RTC: set interval for data logging

Hello everybody,

I have built a weather station. Five sensors, with tft display, and a SD Shield with RTC (DS1307). Everything is working fine so far.
Now I would like to log the data every hour to the SD card. I have read a lot of tutorials and datasheets/ examples hoping to figure out how to do so. Unfortunately my code does not work.
My question is how do I extract the hours and minutes seperatley from the RTC to create a conditional statement? I.e. " .. if hour +1 ; saveData..".
Could you please point me I the right direction! With code examples or links where I could read about it?

Thank you very much!
Cheers, Mona :slight_smile:

MonaRieff:
Everything is working fine so far.

I suspect that is not true but nobody will know because they can't see your code, and don't know what "everything" really means.

You might try something like

   GetClock();
  if (minute == 0 && second==0)
  {
    do stuff
  }

where GetClock is your srtn for reading the RTC. If it proves unreliable, you might try

if (minute == 0 && second<2)

The DS1307 library has a method 'read' that you can use.

bool DS1307RTC::read(tmElements_t &tm)

So you can use something like

#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>

void setup()
{
  Serial.begin(9600);
  while (!Serial) ; // wait for serial
  delay(200);
  Serial.println("DS1307RTC Read Test");
  Serial.println("-------------------");
}

void loop()
{
  static int seconds = -1;
  tmElements_t tm;

  if (RTC.read(tm))
  {
    // check if whole hour (minute component) and if not done yet
    if (tm.Minute == 0 && seconds == -1)
    {
      // get seconds component from tm to indicate that this was executed
      seconds = tm.Second;
      // do logging
      ...
      ...
    }
    else
    {
      // when minute component is no longer zero, reset seconds to -1 so we are ready for the next hour
      seconds = -1;
    }
  }
}

The above if fully based on the ReadTest.ino example that comes with the library and shows how to use the components of the tm structure.

Not tested, not compiled; just to give you the idea.

With a DS3231 RTC, you can set a separate alarm output to signal every minute, hour, day, or just at a particular time.

Just save the hour to a variable, and compare hour() to the last value saved, if it's diferent, save the data.

#include <Time.h>

byte hours = 0;

void setup() {
  Serial.begin(9600);
  //setSyncProvider(getTime);
}

void loop() {
 if (hour() != hours) {
    //whatever
    hours = hour();
    }
}

Hello! Thank you very much for your suggestions and your examples. Helped me alot to understand the process.
Your "tm.Elements" method sterretje did not work out for me... I used a different library. Thanks though.
Arank I modified your idea. I know it could have been solved more elaborate than listing every hour... not fancy but it works.

printTime();

int hours = 1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24; 

clock.getTime(); 
if (clock.hour = hours && clock.minute == 0 && clock.second == 0)
{
  Serial.print(",Works");
  hours = clock.hour;
}

So thanks again! Mona

Are the hours reall 1-24, or are they 0-23? My clocks go to 0 after 23:59

mixographer:
My clocks go to 0 after 23:59

I believe they would all do that. I use mine to test for midnight:

 if (hour == 0 && minute == 0 && second <2)
int hours = 1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24;

I don't know what you intended with this statement but all it does is set "hours" to 31.

+edit - and the RTC libraries I've used all have hours from 0 to 23, minutes and seconds from 0 to 59.

Pete