-
Notifications
You must be signed in to change notification settings - Fork 0
/
AquariaTime.cpp
133 lines (111 loc) · 3.26 KB
/
AquariaTime.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* File: AquariaTime.cpp
* Author: butchman
*
* Created on June 1, 2014, 11:18 AM
*/
#include "AquariaTime.h"
// ---------------------Alarm functions -------------------//
// Turns the sunlight on
void SunlightOn() {
digitalWrite(LIGHT_PIN_1, HIGH);
digitalWrite(LIGHT_PIN_2, HIGH);
}
// Turns the sunlight off
void SunlightOff() {
digitalWrite(LIGHT_PIN_1, LOW);
digitalWrite(LIGHT_PIN_2, LOW);
}
// Turns the moonlight on
void MoonlightOn() {
analogWrite(LIGHT_PIN_3, 25);
analogWrite(LIGHT_PIN_4, 255);
analogWrite(LIGHT_PIN_5, 25);
}
// Turns the moonlight off
void MoonlightOff() {
analogWrite(LIGHT_PIN_3, 0);
analogWrite(LIGHT_PIN_4, 0);
analogWrite(LIGHT_PIN_5, 0);
}
void digitalClockDisplay() {
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(year());
Serial.println();
}
void printDigits(int digits) {
Serial.print(":");
if (digits < 10)
Serial.print('0');
Serial.print(digits);
}
void printDigitsDate(int digits) {
Serial.print("/");
if (digits < 10)
Serial.print('0');
Serial.print(digits);
}
// Daylight check for initial startup
boolean isDaytimeNow(int dhr, int dmin, int dsec, int mhr, int mmin, int msec) {
// startup lamp status checks
time_t startOfDay = timeToSeconds(dhr, dmin, dsec); // Hour,Min,Secs for start of day;
time_t startOfNight = timeToSeconds(mhr, mmin, msec); // Hour,Min,Secs for start of Night;
// Change the current timestamp to secs
time_t t = timeToSeconds(hour(), minute(), second());
// If its between startOfDay & StartOfNight return true
return (t >= startOfDay && t < startOfNight);
}
time_t timeToSeconds(time_t H, time_t M, time_t S) {
return (H * SECS_PER_HOUR) + (M * SECS_PER_MIN) + S;
}
const char *monthName[12] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
tmElements_t tm;
bool getTime(const char *str) {
int Hour, Min, Sec;
if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
tm.Hour = Hour;
tm.Minute = Min;
tm.Second = Sec;
return true;
}
bool getDate(const char *str) {
char Month[12];
int Day, Year;
uint8_t monthIndex;
if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
for (monthIndex = 0; monthIndex < 12; monthIndex++) {
if (strcmp(Month, monthName[monthIndex]) == 0) break;
}
if (monthIndex >= 12) return false;
tm.Day = Day;
tm.Month = monthIndex + 1;
tm.Year = CalendarYrToTm(Year);
return true;
}
void setAquariaTime() {
// first check to see if the RTC is already set
if (RTC.get == 0) {
// it's not so initialize the RTC with info
// from the compiler
getDate(__DATE__) && getTime(__TIME__);
RTC.write(tm);
}
// it is so lets...
setSyncProvider(RTC.get); // ...set the system time
// and check to see if it worked
if (timeStatus() != timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
setSyncInterval(60000); // sync system time with RTC every hour
}