1
2
3#include <Adafruit_TLC5947.h>
4#include <Wire.h>
5#include <RTClib.h>
6#include <SD.h>
7#include <TMRpcm.h>
8#include <SPI.h>
9
10#define SD_ChipSelectPin 8
11#define NUM_TLC5974 1
12#define din 4
13#define clock 5
14#define latch 6
15RTC_DS1307 rtc;
16TMRpcm tmrpcm;
17Adafruit_TLC5947 tlc = Adafruit_TLC5947(NUM_TLC5974, clock, din, latch);
18
19
20byte alarmhours = 8;
21byte alarmminutes = 0;
22
23String inputString = "";
24boolean stringComplete = false;
25int t = 0;
26int hou = 0;
27int m = 0;
28int s = 0;
29String data = "";
30bool buttonpress = false;
31int light = 4096;
32byte to = 0;
33byte past = 0;
34bool itis = false;
35bool mins = false;
36byte h = 0;
37int button = 0;
38bool alarmran = false;
39
40void setup () {
41 rtc.begin();
42 inputString.reserve(200);
43
44 tmrpcm.speakerPin = 9;
45 if (!SD.begin(SD_ChipSelectPin))
46 {
47 Serial.println("SD fail");
48 return;
49 }
50
51 tmrpcm.setVolume(5);
52
53
54 Serial.begin(57600);
55 if (! rtc.begin()) {
56 Serial.println("Couldn't find RTC");
57 while (1);
58 }
59 if (! rtc.isrunning()) {
60 Serial.println("RTC is NOT running!");
61 rtc.adjust(DateTime(2019, 6, 11, 0, 0, 0));
62 }
63
64 tlc.begin();
65}
66
67void displayupdate() {
68 DateTime now = rtc.now();
69
70 for (uint16_t i = 0; i < 8; i++) {
71 tlc.setLED(i, 0, 0, 0);
72 }
73
74 past = 0;
75 to = 0;
76
77 itis = false;
78
79 if (now.minute() > 25 && now.minute() <= 30) {
80 tlc.setLED(0, light, light, 0);
81 itis = true;
82 }
83 if (now.minute() > 5 && now.minute() <= 10) {
84 tlc.setLED(0, light, 0, light);
85 itis = true;
86 }
87 if (now.minute() > 45 && now.minute() <= 50) {
88 tlc.setLED(0, light, 0, light);
89 itis = true;
90 }
91
92 if (itis == false) {
93 tlc.setLED(0, light, 0, 0);
94 }
95
96 if (now.minute() > 10 && now.minute() <= 15) {
97 tlc.setLED(1, light, 0, 0);
98 }
99 if (now.minute() > 40 && now.minute() <= 45) {
100 tlc.setLED(1, light, 0, 0);
101 }
102 if (now.minute() > 15 && now.minute() <= 20) {
103 tlc.setLED(1, 0, light, 0);
104 }
105 if (now.minute() > 35 && now.minute() <= 40) {
106 tlc.setLED(1, 0, light, 0);
107 }
108 if (now.minute() > 0 && now.minute() <= 5) {
109 tlc.setLED(1, 0, 0, light);
110 }
111 if (now.minute() > 50 && now.minute() <= 55) {
112 tlc.setLED(1, 0, 0, light);
113 }
114 if (now.minute() > 20 && now.minute() <= 25) {
115 tlc.setLED(1, 0, light, light);
116 }
117 if (now.minute() > 30 && now.minute() <= 35) {
118 tlc.setLED(1, 0, light, light);
119 }
120
121 if (now.minute() > 30 && now.minute() < 55) {
122 to = 1;
123 }
124 if (now.minute() < 31 && now.minute() > 0) {
125 past = 1;
126 }
127
128 mins = false;
129 if (now.minute() > 5 && now.minute() <= 10) {
130 tlc.setLED(2, light, light * to, light * past);
131 mins = true;
132 }
133 if (now.minute() > 45 && now.minute() <= 50) {
134 tlc.setLED(2, light, light * to, light * past);
135 mins = true;
136 }
137 if (now.minute() > 15 && now.minute() <= 25) {
138 tlc.setLED(2, light, light * to, light * past);
139 mins = true;
140 }
141 if (now.minute() > 30 && now.minute() <= 40) {
142 tlc.setLED(2, light, light * to, light * past);
143 mins = true;
144 }
145 if (now.minute() > 50 && now.minute() <= 55) {
146 tlc.setLED(2, light, light * to, light * past);
147 mins = true;
148 }
149 if (now.minute() > 0 && now.minute() <= 5) {
150 tlc.setLED(2, light, light * to, light * past);
151 mins = true;
152 }
153
154 if (mins == false) {
155 tlc.setLED(2, 0, light * to, light * past);
156 }
157
158 if (now.minute() > 30 && now.minute() < 55) {
159 h = now.hour() + 1;
160 }
161 if (now.minute() <= 30) {
162 h = now.hour();
163 }
164
165 if (h == 1) {
166 tlc.setLED(3, light, 0, 0);
167 }
168 if (h == 2) {
169 tlc.setLED(3, 0, 0, light);
170 }
171 if (h == 3) {
172 tlc.setLED(3, 0, light, 0);
173 }
174 if (h == 4) {
175 tlc.setLED(4, light, 0, 0);
176 }
177 if (h == 5) {
178 tlc.setLED(4, 0, light, 0);
179 }
180 if (h == 6) {
181 tlc.setLED(4, 0, 0, light);
182 }
183 if (h == 7) {
184 tlc.setLED(5, light, 0, 0);
185 }
186 if (h == 8) {
187 tlc.setLED(5, 0, light, 0);
188 }
189 if (h == 9) {
190 tlc.setLED(5, 0, 0, light);
191 }
192 if (h == 10) {
193 tlc.setLED(6, light, 0, 0);
194 }
195 if (h == 11) {
196 tlc.setLED(6, 0, light, 0);
197 }
198 if (h == 12) {
199 tlc.setLED(6, 0, 0, light);
200 }
201
202 if (h == 13) {
203 tlc.setLED(3, light, 0, 0);
204 }
205 if (h == 14) {
206 tlc.setLED(3, 0, 0, light);
207 }
208 if (h == 15) {
209 tlc.setLED(3, 0, light, 0);
210 }
211 if (h == 16) {
212 tlc.setLED(4, light, 0, 0);
213 }
214 if (h == 17) {
215 tlc.setLED(4, 0, light, 0);
216 }
217 if (h == 18) {
218 tlc.setLED(4, 0, 0, light);
219 }
220 if (h == 19) {
221 tlc.setLED(5, light, 0, 0);
222 }
223 if (h == 20) {
224 tlc.setLED(5, 0, light, 0);
225 }
226 if (h == 21) {
227 tlc.setLED(5, 0, 0, light);
228 }
229 if (h == 22) {
230 tlc.setLED(6, light, 0, 0);
231 }
232 if (h == 23) {
233 tlc.setLED(6, 0, light, 0);
234 }
235 if (h == 0) {
236 tlc.setLED(6, 0, 0, light);
237 }
238
239 if (now.minute() == 0 or now.minute() > 55) {
240 tlc.setLED(7, light, 0, 0);
241 }
242
243
244 tlc.write();
245}
246
247void addition() {
248 DateTime now = rtc.now();
249
250 if (now.hour() < 6 or now.hour() > 21) {
251 light = 500;
252 }
253 else {
254 light = 4096;
255 }
256}
257
258void(* resetFunc) (void) = 0;
259
260void loop () {
261
262 serialEvent();
263 displayupdate();
264 addition();
265
266 if (stringComplete) {
267 Serial.println(h);
268 t = inputString.toInt();
269 if (hou != 0 && m != 0 && s == 0) {
270 s = t;
271 }
272 if (hou != 0 && m == 0) {
273 m = t;
274 }
275 if (hou == 0) {
276 hou = t;
277 }
278 if (hou != 0 && m != 0 && s != 0) {
279 rtc.adjust(DateTime(2000, 0, 0, hou, m, s));
280 hou = 0;
281 m = 0;
282 s = 0;
283 }
284 inputString = "";
285 stringComplete = false;
286 }
287
288
289 DateTime now = rtc.now();
290 if (alarmhours == now.hour() && alarmminutes == now.minute() && alarmran == false) {
291 tmrpcm.play("test.wav");
292 do {
293 button = analogRead(0);
294 if (button > 800) {
295 buttonpress = false;
296
297 }
298 else {
299 buttonpress = true;
300 tmrpcm.disable();
301 }
302 } while (tmrpcm.isPlaying() == 1 && buttonpress == false);
303 alarmran = true;
304 tmrpcm.disable();
305 }
306
307 if (now.hour() == 12 && now.minute() == 1 && now.second() == 15) {
308
309 delay(1000);
310 resetFunc();
311 }
312 if (now.hour() == 1 && now.minute() == 1 && now.second() == 15) {
313
314 delay(1000);
315 resetFunc();
316 }
317
318 delay(1000);
319}
320
321
322
323void serialEvent() {
324 while (Serial.available()) {
325 char inChar = (char)Serial.read();
326 inputString += inChar;
327 if (inChar == '\n') {
328 stringComplete = true;
329 }
330 }
331}
332