Problem With Time Alarm

I was wondering if anyone could help me, if it’s possible to get a time alarm function to carry over midnight. Say if I want it to turn on at 19:00 (7:00pm) and turn off at 07:00 (7:00am).

The code is stripped from my complete code. It does work, as long as I don’t go past midnight.

#include <Wire.h>
#include “RTClib.h”
#include <Time.h>
#include <TimeAlarms.h>

RTC_DS1307 RTC;

int lastSecond;

// RefugiumLed Variables //
// Start Time //

// Fan Variables //
// Start Time //
int sfHour = 23;
int sfMinute = 05;
int sfSecond = 0;
// End Time //
int efHour = 23;
int efMinute = 18;
int efSecond = 0;
// Fan Pin //
int Fan = 22;

time_t rtcUnixtime()
{
return RTC.now().unixtime();
}

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

setSyncProvider(rtcUnixtime); // Sync Time library code w/RTC.
setSyncInterval(86400); // Sync once every 24 hrs.

// Set the led pinmode //

pinMode(Fan, OUTPUT);

// Notify if the RTC isn’t running //
if (! RTC.isrunning())
{
Serial.println(“RTC is NOT running”);
}

// Get time from RTC //
DateTime current = RTC.now();
DateTime compiled = DateTime(DATE, TIME);

if (current.unixtime() < compiled.unixtime())
{
Serial.println(“RTC is older than compile time! Updating”);

RTC.adjust(DateTime(DATE, TIME));

current = compiled;
}

DateTime sf = DateTime(current.year(), current.month(), current.day(), sfHour, sfMinute, sfSecond);
DateTime ef = DateTime(current.year(), current.month(), current.day(), efHour, efMinute, efSecond);

// Sets the RefugiumLed light on|off as required.
digitalWrite(Fan, (sf.unixtime() <= current.unixtime() && current.unixtime() <= ef.unixtime() ? HIGH : LOW));

Alarm.alarmRepeat(sfHour, sfMinute, sfSecond, FanOn);
Alarm.alarmRepeat(efHour, efMinute, efSecond, FanOff);
}

void loop()
{
static char buf = (char)calloc(20, sizeof(char*));

DateTime now = RTC.now();

if (lastSecond != now.second())
{
lastSecond = now.second();

// Use standard date/time format MM/DD/YYYY HH24:MI:SS.
sprintf(buf, “%02d/%02d/%04d %02d:%02d:%02d”, now.month(), now.day(), now.year(), now.hour(), now.minute(), now.second());

Serial.println(buf);

}

int minutes = (now.hour() * 60) + now.minute();

Alarm.delay(1000);
}

void FanOn()
{
Serial.println(“Turning Fan On”);
digitalWrite(Fan, HIGH);
}

void FanOff()
{
Serial.println(“Turning Fan Off”);
digitalWrite(Fan, LOW);
}

New_Time_Alarm_Test.ino (2.68 KB)

If you have a real-time clock, why do you need the TimeAlarms class? It is easy to determine, in loop(), if it is the correct time to do something.

What does happen if you "go past midnight"?

I'm not the smartest when it comes to programming. I do have a tiny rtc 12c module. What I'm wanting to do is have a light and cooling fan come on from 7:00pm to 7:00am. If I put any time past midnight, it won't turn on, but if I put 11:59 it turns on and shuts off at 11:59.

if (rtc.Hour() >= 7 && rtc.Hour() < 19) {//lights off} else {//lights on}

YMMV

Is your RTC set to 12 hour time? What time comes two seconds after 11:59:59?

I believe it's military time. 24 hour format.

What time comes two seconds after 11:59:59?

00:00:01.

weasel440: I believe it's military time. 24 hour format. 00:00:01.

That is not what comes 2 seconds after 11:59:59, in 24 hour format. It is what comes 2 seconds later in 12 hour format.

You need to fix your RTC to use 24 hour format.

Military time would go to 23:59:59.

Military time would go to 23:59:59.

You are correct. I had a brain fart. It is 23:59:59 not 11:59:59 as PaulS asked & I answered.

I asked about one time. You responded with an answer, but no proof how you got the answer, about a different time. Try again.

I asked about one time. You responded with an answer, but no proof how you got the answer, about a different time. Try again.

Don't understand. I don't usually use "military" time for anything else. I was just thinking midnight and when you asked 11:59:59. I forgot about the "military" time. The time I'm going by is the serial monitor.

So yes it's 23:59:59. My mistake. Then 2 seconds it goes to 00:00:01

Too many words, and not enough looking at the actual code.

    DateTime sf = DateTime(current.year(), current.month(), current.day(), sfHour, sfMinute, sfSecond);
    DateTime ef = DateTime(current.year(), current.month(), current.day(), efHour, efMinute, efSecond);
    
    // Sets the RefugiumLed light on|off as required.
    digitalWrite(Fan, (sf.unixtime() <= current.unixtime() && current.unixtime() <= ef.unixtime() ? HIGH : LOW));

Anyone else see anything wrong there?

odometer: Anyone else see anything wrong there?

Yup.

Hint: Groundhog Day

Anyone?

You are setting the end time to today morning instead of tomorrow morning.

07:00 should be morning, I thought. If you are referring to what i have in the first sketch, I was testing it. To make sure it worked. Turned on / off at set time. But if I set off time past 23:59:59 it won't turn on.

But if I set off time past 23:59:59 it won't turn on.

How can you possibly set the off time later than 23:59:59? ANY time after that is sometime the next day.

I think I got it. I'm testing it now and waiting to see if it turns back on at set time. It turned on at 19:00 and I set it to turn off at 24:25. It did shut off.

I think I got it. I'm testing it now and waiting to see if it turns back on at set time. It turned on at 19:00 and I set it to turn off at 24:25. It did shut off.

What? Why not set the time to 23:50:00, the on time to 23:55:00 and the off time to 00:05:10, and be done with the testing in 10 minutes?