3 #include <RealTimeClockDS1307.h>
5 // minutes of "dawn" before alarm
9 // "dawn" + "daylight" + blue blinding light
12 // number of available alarms; max 10 for storage in the DS1307 ram
20 int st = 0; // alarm status (minutes from alarm - TIN)
21 char alarms[NALARMS][5]; // alarm settings
22 char cmin; // current minute
23 int a = -1; // current alarm
24 char dbg = 0; // print debug informations
34 digitalWrite(RPIN,255);
35 digitalWrite(YPIN,255);
38 // if the RTC is already running read alarms and status,
39 // otherwise set everything to a sane default
40 if ( RTC.readData(0x00) >> 7 ) {
41 for ( int i = 0 ; i < NALARMS ; i ++ ) {
42 for ( int j = 0; j < 5 ; j ++ ) {
50 st = RTC.readData(0x08);
51 a = RTC.readData(0x09);
52 cmin = RTC.readData(0x0a);
53 // This only works if the arduino had no power for a
54 // "short" time. This is by design. :D
56 st = st + (RTC.getMinutes() - cmin) % 60;
57 cmin = RTC.getMinutes();
59 for ( int i = 0; i < NALARMS ; i ++ ) {
60 for ( int j = 0; j < 5 ; j ++ ) {
61 alarms[i][j] = RTC.readData(0x0b + i*5 + j);
70 // read commands from serial
73 // read time, check alarms
80 Serial.print(", a: ");
82 Serial.print(", cmin: ");
83 Serial.println(cmin,DEC);
86 // act on status: LEDs and buzzer
91 // wait about till the next second
98 RTC.writeData(0x08,st);
99 RTC.writeData(0x09,a);
100 RTC.writeData(0x0a,cmin);
103 // ****** Serial interface management *************************************** //
105 void check_serial() {
106 int rec = Serial.read();
130 int i = s_read_dig();
131 for ( int j = 0; j < 5 ; j++) {
132 alarms[i][j] = s_read_2hex();
134 for ( int j = 0; j < 5 ; j++ ) {
135 RTC.writeData(0x0b + i*5 + j,alarms[i][j]);
137 Serial.print("Alarm ");
139 Serial.println(" set.");
144 RTC.setYear(s_read_2dig());
145 RTC.setMonth(s_read_2dig());
146 RTC.setDate(s_read_2dig());
147 RTC.setDayOfWeek(s_read_dig());
148 RTC.setHours(s_read_2dig());
149 RTC.setMinutes(s_read_2dig());
150 RTC.setSeconds(s_read_2dig());
152 Serial.print("Time set: ");
159 while ( rec == -1 ) {
167 n = s_read_dig() * 10;
168 n = n + s_read_dig();
175 while ( rec == -1 ) {
178 if ( rec >= 48 && rec < 58 ) {
180 } else if ( rec >= 65 && rec < 71 ) {
182 } else if ( rec >= 97 && rec < 102 ) {
191 n = s_read_hex() * 16;
192 n = n + s_read_hex();
196 void s_print_alarms() {
197 for ( int i = 0; i < NALARMS ; i++) {
200 for ( int j = 0; j < 5 ; j++) {
201 Serial.print(alarms[i][j],DEC);
208 void s_print_time() {
210 Serial.print(RTC.getYear(),DEC);
212 Serial.print(RTC.getMonth(),DEC);
214 Serial.print(RTC.getDate(),DEC);
216 Serial.print(RTC.getDayOfWeek(),DEC);
218 Serial.print(RTC.getHours(),DEC);
220 Serial.print(RTC.getMinutes(),DEC);
222 Serial.println(RTC.getSeconds(),DEC);
225 void s_toggle_debug() {
233 void s_print_help() {
235 Serial.println(" a<s> - set an alarm");
236 Serial.println(" <s> is nhhhhhhhhhh");
237 Serial.println(" where hh are hex values");
238 Serial.println(" s<s> - set the clock");
239 Serial.println(" <s> is yymmgguHHMMSS");
240 Serial.println(" p - print the alarms");
241 Serial.println(" t - print the clock");
242 Serial.println(" d - toggle printing of debug informations");
243 Serial.println(" h - print this help");
246 // ****** Time management *************************************************** //
248 // Set the current time
249 void set_time(int y,int m,int d, int w, int hh, int mm, int ss) {
264 int mm = RTC.getMinutes();
265 int hour = RTC.getHours();
266 int wday = RTC.getDayOfWeek();
267 int day = RTC.getDate();
268 int month = RTC.getMonth();
271 for ( int i = 0; i < NALARMS ; i ++ ) {
273 if ( ( alarms[i][0] & ( 1 << (wday - 1) ) ) ||
274 (month == alarms[i][1] && day == alarms[i][2]) ) {
275 // this is alarm day!
276 if ( hour == alarms[i][3] && mm == alarms[i][4]) {
277 // this is alarm hour!
282 if ( ( alarms[i][0] & 128 ) == 0 ) {
283 // this alarm won't be repeated
284 alarms[i] = { 0,0,0,0,0 };
285 for ( int j = 0; j < 5 ; j++ ) {
286 RTC.writeData(0x0b + i*5 + j,0);
303 // ****** LED management **************************************************** //
306 if ( st > 0 && st <= TIN) {
307 int y = int(float(st*255)/TIN);
311 } else if ( st > TIN && st < TDAY ) {
313 analogWrite(YPIN,255);
315 } else if (st >= TDAY && st < TOUT) {
318 analogWrite(BPIN,255);
319 } else if (st == TOUT) {
324 analogWrite(RPIN,255);
330 // vim: set filetype=c: