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?
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.
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.