I’m trying to build a RTC based interval timer for my data logging installation.
I want to collect data every 30min so I ‘m trying to build a similar function as the millis interval method but with RTC to check and trigger something every 30min.
I’m using a DS1037 with RTClib.h.
Right now I noticed that my alarm1.minute() is equal to zero, so i'm declaring this interval time wrong. Can you explain what i'm missing. Is it to do with it going to the next hour?, and minutes restarting at 0?
Here is my code:
#include <SD.h>
#include <RTClib.h>
//RTC Var
RTC_DS3231 rtc;
DateTime cur;// = rtc.now(); //current time
DateTime alarm1; //interval
int x = 30 //interval in min;
void setup() {
//SERIAL SETUP
Serial.begin(9600);
// SETUP RTC MODULE
if (!rtc.begin()) {
Serial.println(F("Couldn't find RTC"));
while (1);
}
pinMode(SS, OUTPUT);
cur = rtc.now();
DateTime alarm1( cur + TimeSpan(0,0,x,0)); //registering my alarm time 30min after start of program.
}
void loop() {
// open file for writing
checkinterval();
//do things;
}
void checkinterval() {
cur = rtc.now();
Serial.print("checking interval");
if(cur.hour()==alarm1.hour() && cur.minute() == alarm1.minute() == true)
{
Serial.print("interval!”);
// trying to compare current time and see it’s the same as my alarm time
DateTime alarm1( cur + TimeSpan(0,0,x,0)); //rest timer 30min later.
}
}
I may not understand what you're doing, but the DS1307 doesn't have an alarm function. Your library and code appear to relate to the DS3231, which does have an alarm function, but the DS1307 does not.
You created a global variable alarm1 and did not define a value for it and then within setup() you created a local variable alarm1 and defined the time value within this variable.
In if you test the value of cur against the global variable which is always zero.
Inside if you create another local variable alarm1.
Try this code:
#include <SD.h>
#include <RTClib.h>
//RTC Var
RTC_DS3231 rtc;
DateTime cur;// = rtc.now(); //current time
DateTime alarm1; //interval
int x = 30; //interval in min;
void setup() {
//SERIAL SETUP
Serial.begin(9600);
// SETUP RTC MODULE
if (!rtc.begin()) {
Serial.println(F("Couldn't find RTC"));
while (1);
}
pinMode(SS, OUTPUT);
cur = rtc.now();
alarm1 = ( cur + TimeSpan(0,0,x,0)); //registering my alarm time 30min after start of program.
}
void loop() {
// open file for writing
checkinterval();
//do things;
}
void checkinterval() {
cur = rtc.now();
if(cur.hour()==alarm1.hour() && cur.minute() == alarm1.minute() )
{
Serial.print("interval!");
// trying to compare current time and see it’s the same as my alarm time
alarm1 = ( cur + TimeSpan(0,0,x,0)); //rest timer 30min later.
}
}