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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
#include <Wire.h>
#include <RealTimeClockDS1307.h>
// minutes of "dawn" before alarm
#define TIN 30
// "dawn" + "daylight"
#define TDAY 45
// "dawn" + "daylight" + blue blinding light
#define TOUT 75
// number of available alarms; max 10 for storage in the DS1307 ram
#define NALARMS 4
// pins and addressed
#define RPIN 3
#define YPIN 5
#define BPIN 6
int st = 0; // alarm status (minutes from alarm - TIN)
char alarms[NALARMS][5]; // alarm settings
char cmin; // current minute
int a = -1; // current alarm
void setup () {
Serial.begin(9600);
Wire.begin();
pinMode(RPIN,OUTPUT);
pinMode(YPIN,OUTPUT);
pinMode(BPIN,OUTPUT);
digitalWrite(RPIN,255);
digitalWrite(YPIN,255);
digitalWrite(BPIN,0);
// DEBUG: we want to read the setup messages
delay(5000);
digitalWrite(YPIN,0);
// if the RTC is already running read alarms and status,
// otherwise set everything to the default
if ( RTC.readData(0x00) >> 7 ) {
Serial.println("Setup the clock");
for ( int i = 0 ; i < NALARMS ; i ++ ) {
for ( int j = 0; j < 5 ; j ++ ) {
alarms[i][j] = 0;
}
}
st = 0;
a = -1;
// DEBUG: we don't want to set the time until we receive
// it from serial, but serial is not implemented yet
alarms[0] = {16,0,0,13,40};
set_time(11,9,2,5,13,39,30);
} else {
st = RTC.readData(0x08);
a = RTC.readData(0x09);
cmin = RTC.readData(0x0a);
// FIXME: we want to update st to the time passed
// since the data was saved
for ( int i = 0; i < NALARMS ; i ++ ) {
for ( int j = 0; j < 5 ; j ++ ) {
alarms[i][j] = RTC.readData(0x0b + i*5 + j);
}
}
}
}
void loop () {
// read commands from serial
// read time, check alarms
check_time();
//DEBUG
Serial.println(st);
// act on status: LEDs and buzzer
if ( st > 0 ) {
set_leds();
}
// wait about till the next second
delay(1000);
}
// Set the current time
void set_time(int y,int m,int d, int w, int hh, int mm, int ss) {
RTC.setYear(y);
RTC.setMonth(m);
RTC.setDate(m);
RTC.setDayOfWeek(w);
RTC.setHours(hh);
RTC.setMinutes(mm);
RTC.setSeconds(ss);
RTC.switchTo24h();
RTC.setClock();
}
void check_time() {
RTC.readClock();
int mm = RTC.getMinutes();
int hour = RTC.getHours();
int wday = RTC.getDayOfWeek();
int day = RTC.getDate();
int month = RTC.getMonth();
// DEBUG
Serial.print("Time: ");
Serial.print(hour,DEC);
Serial.print(":");
Serial.print(mm,DEC);
Serial.print(":");
Serial.println(RTC.getSeconds());
if ( a < 0 ) {
for ( int i = 0; i < NALARMS ; i ++ ) {
// check alarm i
if ( ( alarms[i][0] & ( 1 << (wday - 1) ) ) ||
(month == alarms[i][1] && day == alarms[i][2]) ) {
// this is alarm day!
if ( hour == alarms[i][3] && mm == alarms[i][4]) {
// this is alarm hour!
a = i;
st = 1;
cmin = mm;
if ( ( alarms[i][0] & 128 ) == 0 ) {
// this alarm won't be repeated
alarms[i] = { 0,0,0,0,0 };
for ( int j = 0; j < 5 ; j++ ) {
RTC.writeData(0x0b + i*5 + j,0);
}
}
break;
}
}
}
} else {
if ( cmin != mm ) {
cmin = mm;
st++;
}
}
}
void set_leds() {
if ( st > 0 && st <= TIN) {
int y = int(float(st*255)/TIN);
int r = 255 - y;
analogWrite(RPIN,r);
analogWrite(YPIN,y);
} else if ( st > TIN && st < TDAY ) {
analogWrite(RPIN,0);
analogWrite(YPIN,255);
analogWrite(BPIN,0);
}else if (st >= TDAY && st < TOUT) {
analogWrite(RPIN,0);
analogWrite(YPIN,0);
analogWrite(BPIN,255);
} else if (st == TOUT) {
// reset stuff
st = 0;
a = -1;
analogWrite(RPIN,255);
analogWrite(YPIN,0);
analogWrite(BPIN,0);
}
}
// vim: set filetype=c:
|