#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <RTClib.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET 4
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
RTC_DS3231 rtc;
const int STATUS_LED_PIN = 10;
const int RELAY_PIN = 11;
const int BELL_DURATION = 4000; // ms
struct breakTime {
char breakDesc;
int startHour;
int startMinute;
int startSecond;
int endHour;
int endMinute;
int endSecond;
char breakDays;
// bool onSunday = false;
// bool onMonday = false;
// bool onTuesday = false;
// bool onWednesday = false;
// bool onThursday = false;
// bool onFriday = false;
// bool onSaturday = false;
// bool onWeekdays = false;
// bool onWeekends = false;
};
struct breakTime breakTimes[3] = {
{"First Break", 10, 0, 0, 10, 30, 0, "0111110"},
{"Lunch Break", 12, 0, 0, 13, 0, 0, "0111110"},
{"Second Break", 16, 15, 0, 16, 40, 0, "0111110"}//,
// {"", 0, 0, 0, 0, 0, 0, "0000000"},
// {"", 0, 0, 0, 0, 0, 0, "0000000"},
// {"", 0, 0, 0, 0, 0, 0, "0000000"}
};
breakTime currentBreak;
breakTime nextBreak;
bool currentBreakBool = false;
void setup() {
// put your setup code here, to run once
Serial.begin(115200);
Serial.println("Serial Initialized");
Wire.begin();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(14, 26);
display.println("Break Alarm");
pinMode(STATUS_LED_PIN, OUTPUT);
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(STATUS_LED_PIN, LOW);
digitalWrite(RELAY_PIN, LOW);
if (!rtc.begin()) {
display.println(" Couldn't find RTC");
Serial.println("Couldn't find RTC");
while (1);
} else {
display.println(" Found RTC");
Serial.println("Found RTC");
}
display.display();
if (rtc.lostPower()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
breakTime nB = {"", 0, 0, 0, 0, 0, 0, "0000000"};
nextBreak = nB;
currentBreak = nB;
delay(2000);
//display.clearDisplay();
}
void loop() {
// put your main code here, to run repeatedly
display.clearDisplay();
DateTime now = rtc.now();
showCurrentTime(now);
delay(1000);
if (nextBreak.startHour == 0) {
nextBreak = getNextBreak(now);
}
showNextBreakTime(nextBreak);
// Serial.print(nextBreak.startHour);
// Serial.print(" ");
// Serial.print(nextBreak.startMinute);
// Serial.print(" ");
// Serial.println(nextBreak.startSecond);
long currentSeconds = (long)now.hour() * 3600 + (long)now.minute() * 60 + (long)now.second();
long nextBreakStartSeconds = (long)nextBreak.startHour * 3600 + (long)nextBreak.startMinute * 60 + (long)nextBreak.startSecond;
long nextBreakEndSeconds = (long)nextBreak.endHour * 3600 + (long)nextBreak.endMinute * 60 + (long)nextBreak.endSecond;
if (currentSeconds == nextBreakStartSeconds) {
// next break is starting
bell((String)nextBreak.breakDesc + " Start");
digitalWrite(STATUS_LED_PIN, HIGH);
} else if (currentSeconds == nextBreakEndSeconds) {
// current break is ending
bell((String)nextBreak.breakDesc + " End");
digitalWrite(STATUS_LED_PIN, LOW);
nextBreak = getNextBreak(now);
} else if (currentSeconds > nextBreakStartSeconds && currentSeconds < nextBreakEndSeconds) {
// current break in progress
digitalWrite(STATUS_LED_PIN, HIGH);
}
display.display();
}
breakTime getNextBreak(DateTime now) {
long currentSeconds = ((long)now.hour() * 3600 + (long)now.minute() * 60 + (long)now.second());
int size = sizeof(breakTimes) / sizeof(breakTimes[0]);
breakTime nB = {"", 99, 99, 99, 99, 99, 99, "0000000"};
for (int i = 0; i < size; i++) {
long breakStartSeconds = (long)breakTimes[i].startHour * 3600 + (long)breakTimes[i].startMinute * 60 + (long)breakTimes[i].startSecond;
long breakEndSeconds = (long)breakTimes[i].endHour * 3600 + (long)breakTimes[i].endMinute * 60 + (long)breakTimes[i].endSecond;
long timeToBreak = breakStartSeconds - currentSeconds;
long timeToEndBreak = breakEndSeconds - currentSeconds;
// Serial.print("Time to break: ");
// Serial.println(timeToBreak);
if (timeToBreak > 0) {
// the break is coming up
// Serial.print(nB.endHour);
// Serial.print(" ");
// Serial.print(nB.endMinute);
// Serial.print(" ");
// Serial.println(nB.endSecond);
long nextBreakStartSeconds = (long)nB.endHour * 3600 + (long)nB.endMinute * 60 + (long)nB.endSecond;
long timeToNextBreak = nextBreakStartSeconds - currentSeconds;
//Serial.print("Time to current next break: ");
//Serial.println(timeToNextBreak);
if (timeToBreak < timeToNextBreak) {
// the current break is earlier than the stored next break
nB = breakTimes[i];
}
} else if (timeToBreak < 0 && timeToEndBreak > 0) {
// currently in a break
nextBreak = breakTimes[i];
break;
}
}
Serial.print("nB startHour: ");
Serial.println(nB.startHour);
return nB;
}
void bell(String bellName) {
//display.clearDisplay();
display.setCursor(0, 0);
display.setTextSize(3);
display.setTextColor(WHITE);
display.println(bellName);
//display.display();
digitalWrite(RELAY_PIN, HIGH);
delay(BELL_DURATION);
digitalWrite(RELAY_PIN, LOW);
}
void showCurrentTime(DateTime now)
{
char day_format[] = "DDD";
char date_format[] = "MMM DD, YYYY";
char time_format[] = "hh:mm:ss ap";
String _day = now.toString(day_format);
String _date = now.toString(date_format);
String _time = now.toString(time_format);
//display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.print(_day);
display.print(" ");
display.println(_date);
display.print(" ");
display.println(_time);
//display.display();
}
void showNextBreakTime(breakTime bt)
{
// Serial.print(bt.startHour);
// Serial.print(" ");
// Serial.print(bt.startMinute);
// Serial.print(" ");
// Serial.println(bt.startSecond);
if (bt.startHour == 99) {
display.setCursor(40, 30);
display.println("No breaks");
display.print(" ");
display.println("remain today");
Serial.println("No more breaks.");
//display.display();
return;
}
// String h;
// String m;
// String s;
String ampm = "am";
// h = String(bt.startHour);
// m = bt.startMinute;
// s = bt.startSecond;
Serial.println(bt.startHour);
// Serial.println(h);
if (bt.startHour > 12) { ampm = "pm"; }
// if (bt.startHour > 12) { h = bt.startHour - 12; }
// if (bt.startHour < 10) { h = "0" + h; }
// if (bt.startMinute < 10) { m = "0" + m; }
// if (bt.startSecond < 10) { s = "0" + s; }
// Serial.print(h);
// Serial.print(" : ");
// Serial.print(m);
// Serial.print(" : ");
// Serial.println(s);
char *timeStr;
sprintf(timeStr, "%d:%d:%d", bt.startHour, bt.startMinute, bt.startSecond);
//String _time = (String)hour + ":" + (String)bt.startMinute + ":" + (String)bt.startSecond + " " + ampm;
//String _time = h + ":" + m + ":" + s + " " + ampm;
Serial.println(timeStr);
//display.clearDisplay();
//display.setTextSize(1);
//display.setTextColor(WHITE);
display.setCursor(32, 20);
display.println("Next Break:");
//display.print(" ");
display.print(timeStr);
//display.display();
}