RTC-based interval with timespan

Hi all.

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

}

Do you need alarm1 at all?

Would simply checking cur.minute to see if it was 0 or 30 be simpler?

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

RTC_DS3231 rtc;

const int interval = 30;  //interval in min;

void setup() {
  Serial.begin(9600);
  if (!rtc.begin()) {
    Serial.println(F("Couldn't find RTC"));
    while (1);
  }
  pinMode(SS, OUTPUT);
  //init.SD
}

void loop() {
  // open file for writing

  checkinterval();
  //do things;
}


void checkinterval() {
static bool isDone = false;
  DateTime cur = rtc.now();
  if (((cur.hour() * 60 + cur.minute()) % interval) == 0)  {
    if (isDone) {
      Serial.print(F("interval"));
      isDone = false;
    }
  }
  else isDone = true;
}

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.

This code of yours does not compile.
There are 2 errors.

1 Serial.print("interval!”); the second " is different from the first.

  1. int x = 30 //interval in min; Missing;

and after correcting these typos, there is this "logic" error.

if(cur.hour()==alarm1.hour() && cur.minute() == alarm1.minute() == true)

What should equal true?

if(cur.hour()==alarm1.hour() && cur.minute() == alarm1.minute() ?? ???== true)

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