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)