How do I create an if statement based on RTC time?

I'm looking to trigger events at certain times of the day using a DS3231 RTC attached to a Uno.

I know about the alarm function/interrupt built into the RTC but the limitation is there's only space for 2 and I'd like 4 or 5 throughout the day.

I'd like to be able to trigger events at times set in my code which will run in a loop continuously. They don't need to be milisecond accurate, I can't use millis as the drift is too much and a reset would then not have accurate time of day (if the RTC was not included)

It only needs to have 24hr functionality as I want the triggers at the same time every day. I don't need day of the week functionality.

I assumed I could write an if statement that went something like.

if (time >= 0600);
 event = true

I can't seem to find anywhere any resources where the RTC sets the Arduino time, then where I can compare the current time (or time variable) with my if statement.

A point in the right direction would be very much appreciated.

It depents on the library you use to use the RTC. If you use the RTClib you can just do:

#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 rtc;

void setup () {
  Wire.begin();
  rtc.begin();
}

void loop () {
    DateTime now = rtc.now();
   
   if(now.hour() == 6 && now.minutes() == 00){
     //do something
   }
}

Be aware it i called the WHOLE minute long. If you don't want that you need to save the state as well.

He means save the on/off state so that you can turn it off when you are finished with it, or else it will repeatedly trigger for a whole minute. You would also have to test the state to use that.

Brilliant thanks! :slight_smile:

I was using <DS3232RTC.h> and I think I got lost down a very deep rabbit hole of code, I knew it was simple but didn't know where I was going wrong.

I just wired it up to turn on a LED for now and it worked a treat.

#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 rtc;

void setup () {
  Serial.begin(57600);
  Wire.begin();
  rtc.begin();

  pinMode(2, OUTPUT);
}

void loop () {
    DateTime now = rtc.now();
   
   if(now.hour() == 20 && now.minute() == 12){
     digitalWrite(2, HIGH);
   }
//DateTime now = rtc.now();
    
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
    
    Serial.println();
    delay(3000);
   
}

For once trigger per day, I would rather

  static byte trigger = 1;

  if(now.hour() > 20 || (now.hour() == 20 && now.minute() >= 12)){
    if (trigger) {
      event();
      trigger = 0;
    }
  }
  else {
    trigger = 1;
  }

Yes, that is the state function we were talking about.

But can we use #include "RTClib.h" - RTC_DS1307 rtc;
with the DS3231 ?

RTC_DS1307 rtc;

creates an instance of object rtc and names it RTC_DS1307

you could call it V8_FORD and it would work the same, as long as you used V8_FORD everywhere the code says RTC_DS1307

RTC_DS1307 is just a label for the compiler to call a connection. any name works, and the IDE does not know what you really have under the hood.

Uhm, you might want to reread that...