#include <Wire.h>
#include <RTClib.h>
RTC_DS1307 rtc;
//constexpr uint8_t bcd2bin(uint8_t val) { return val - 6 * (val >> 4); }
//constexpr uint8_t bin2bcd(uint8_t val) { return val + 6 * (val / 10); }
constexpr uint16_t MakeTimestamp16(const byte hour, const byte minute)
{
// Pack the 2 individual bytes into a single 2-byte data type in order of precedence.
return uint16_t(hour) << 8 | uint16_t(minute);
}
/**
constexpr uint32_t MakeTimestamp(const byte day, const byte hour, const byte minute, const byte second)
{
// Pack the 4 individual bytes into a single 4-byte data type in order of precedence.
return uint32_t(day) << 24 | uint32_t(hour) << 16 | uint32_t(minute) << 8 | uint32_t(second);
}
/**/
const byte motor1_start_hour = 0x00;
const byte motor1_start_minute = 0x15;
const byte motor1_stop_hour = 0x00;
const byte motor1_stop_minute = 0x50;
const byte motor2_start_hour = 0x22;
const byte motor2_start_minute = 0x30;
const byte motor2_stop_hour = 0x2;
const byte motor2_stop_minute = 0x45;
const uint16_t motor1_start_time = MakeTimestamp16(motor1_start_hour, motor1_start_minute);
const uint16_t motor1_stop_time = MakeTimestamp16(motor1_stop_hour, motor1_stop_minute);
const uint16_t motor2_start_time = MakeTimestamp16(motor2_start_hour, motor2_start_minute);
const uint16_t motor2_stop_time = MakeTimestamp16(motor2_stop_hour, motor2_stop_minute);
//const uint32_t motor1_start_time = MakeTimestamp(0, motor1_start_hour, motor1_start_minute, 0);
//const uint32_t motor1_stop_time = MakeTimestamp(0, motor1_stop_hour, motor1_stop_minute, 0);
//const uint32_t motor2_start_time = MakeTimestamp(0, motor2_start_hour, motor2_start_minute, 0);
//const uint32_t motor2_stop_time = MakeTimestamp(0, motor2_stop_hour, motor2_stop_minute, 0);
void setup()
{
Serial.begin(115200);
if (!rtc.begin())
{
Serial.println("Couldn't find RTC");
while (1);
}
if (!rtc.isrunning())
{
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
/**
if (rtc.begin())
{
DateTime now = rtc.now();
//int i = now().hour();
Serial.println(now.hour());
}
else
{
Serial.println("No RTC.");
delay(1000);
abort();
}
/**/
}
uint16_t NowHHMM()
{
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write((byte)1); // Minute register.
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 2);
const uint8_t mm = Wire.read(); // Minutes in BCD.
const uint8_t hh = Wire.read(); // Hours in BCD.
Serial.println(mm, HEX);
Serial.println(hh, HEX);
return MakeTimestamp16(hh, mm);
}
void loop()
{
const uint16_t timestamp = NowHHMM();
//char* buffer = "--:--";
//snprintf(buffer, sizeof(buffer), "%02X:%02X", (uint8_t)timestamp >> 8, (uint8_t)timestamp);
//Serial.println(buffer);
//const DateTime now = rtc.now();
//const uint32_t timestamp = MakeTimestamp(0, now.hour(), now.minute(), now.second());
bool motor1_on = WithinTimespan16(timestamp, motor1_start_time, motor1_stop_time);
bool motor2_on = WithinTimespan16(timestamp, motor2_start_time, motor2_stop_time);
/**
const uint32_t seconds_now = rtc.now().secondstime();
const DateTime now = rtc.now();
const DateTime begin1(now.year(), now.month(), now.day(), motor1_start_hour, motor1_start_hour);
const DateTime end1(now.year(), now.month(), now.day(), motor1_stop_hour, motor1_stop_hour);
const DateTime begin2(now.year(), now.month(), now.day(), motor2_start_hour, motor2_start_hour);
const DateTime end2(now.year(), now.month(), now.day(), motor2_stop_hour, motor2_stop_hour);
//uint32_t t = now.get();
/**
//UpdateMotor1(motor1_on);
//UpdateMotor2(motor2_on);
Serial.print("\nMotor 1: ");
PrintTimestamp16(Serial, motor1_start_time);
Serial.print(", ");
PrintTimestamp16(Serial, timestamp);
Serial.print(", ");
PrintTimestamp16(Serial, motor1_stop_time);
Serial.print(", on/off = ");
Serial.println(motor1_on);
Serial.print("Motor 2: ");
PrintTimestamp16(Serial, motor2_start_time);
Serial.print(", ");
PrintTimestamp16(Serial, timestamp);
Serial.print(", ");
PrintTimestamp16(Serial, motor2_stop_time);
Serial.print(", on/off = ");
Serial.println(motor2_on);
/**
Serial.print("\nnow: ");
Serial.print(now.day());
Serial.print(", ");
Serial.print(now.hour());
Serial.print(":");
Serial.print(now.minute());
Serial.print(":");
Serial.print(now.second());
Serial.print(", motor1 = ");
Serial.print(motor1_start_time);
Serial.print(", ");
Serial.print(motor1_stop_time);
Serial.print(", ");
Serial.println(motor1_on);
/**/
}
bool WithinTimespan16(const uint16_t& timestamp, const uint16_t& begin, const uint16_t& end)
{
if (end < begin)
{
// Crossing midnight.
Serial.println("Crossing midnight.");
return timestamp >= begin || timestamp <= end;
}
// Single day.
Serial.println("Single day.");
return timestamp >= begin && timestamp <= end;
}
/**
bool WithinTimespan(const uint32_t& timestamp, const uint32_t& begin, const uint32_t& end)
{
if (end < begin)
{
// Crossing midnight.
Serial.println("Crossing midnight.");
return timestamp >= begin || timestamp <= end;
}
// Single day.
Serial.println("Single day.");
return timestamp >= begin && timestamp <= end;
}
/**/
void PrintTimestamp16(Stream& stream, uint16_t timestamp)
{
stream.print(uint8_t(timestamp >> 8 & 0xff), HEX);
stream.print(":");
stream.print(uint8_t(timestamp >> 0 & 0xff), HEX);
}
/**
void PrintTimestamp(Stream& stream, uint32_t timestamp)
{
stream.print(uint8_t(timestamp >> 24 & 0xff));
stream.print(", ");
stream.print(uint8_t(timestamp >> 16 & 0xff));
stream.print(":");
stream.print(uint8_t(timestamp >> 8 & 0xff));
stream.print(":");
stream.print(uint8_t(timestamp >> 0 & 0xff));
}
/**/