Read backed up status from the DS1307 (partial)
[fuzzy_alarm_clock.git] / ds1307 / arduino_sketch / fuzzy_alarm_clock_ds1307.pde
blobfd0682c6a82380a70871f3ff48da6062d6d99be2
1 #include <Wire.h>
3 // minutes of "dawn" before alarm
4 #define TIN 30
5 // "dawn" + "daylight"
6 #define TDAY 45
7 // "dawn" + "daylight" + blue blinding light
8 #define TOUT 75
10 // number of available alarms; max 10 for storage in the DS1307 ram 
11 #define NALARMS 4
13 // pins and addressed
14 #define RPIN 3
15 #define YPIN 5
16 #define BPIN 6
17 #define DS1307_ADDRESS 0x68
19 int st = 0; // alarm status (minutes from alarm - TIN)
20 char alarms[NALARMS][5];
21 char cmin; // current minute
22 int a = -1; // current alarm
24 void setup () {
25     Serial.begin(9600);
26     Wire.begin();
27     
29     pinMode(RPIN,OUTPUT);
30     pinMode(YPIN,OUTPUT);
31     pinMode(BPIN,OUTPUT);
33     digitalWrite(RPIN,255);
34     digitalWrite(YPIN,0);
35     digitalWrite(BPIN,0);
36     
37     // if the RTC is already running read alarms and status
38     Wire.beginTransmission(DS1307_ADDRESS);
39     Wire.send(0);
40     Wire.endTransmission();
42     Wire.requestFrom(DS1307_ADDRESS, 1);
43     char running = Wire.receive();
44     running = !(running>>7);
46     if ( running ) {
47         Wire.requestFrom(DS1307_ADDRESS, 12 + NALARMS * 5);
48         for (int i = 0; i < 8 ; i ++ ) {
49             Wire.receive();
50         }
51         st = Wire.receive();
52         a = Wire.receive();
53         cmin = Wire.receive();
54         for ( int i = 0; i < NALARMS ; i ++ ) {
55             for ( int j = 0; j < 5 ; j ++ ) {
56                 alarms[i][j] = Wire.receive();
57             }
58         }
59     } else {
60         for ( int i = 0 ; i < NALARMS ; i ++ ) {
61             for ( int j = 0; j < 5 ; j ++ ) {
62                 alarms[i][j] = 0;
63             }
64         }
65         st = 0;
66         a = -1;
67         // DEBUG: we don't want to set the time until we receive 
68         // it from serial, but serial is not implemented yet
69         set_time(11,9,2,5,0,0,0);
70     }
71     
74 void loop () {
75   
76   // read commands from serial
77   
78   // read time, check alarms
79   check_time();
80   Serial.println(st);
81   
82   // act on status: LEDs and buzzer
83   if ( st > 0 ) {
84       set_leds();
85   }
86   
87   // wait about till the next second
88   
89   delay(1000);
90   
93 // Set the current time
94 void set_time(int y,int m,int d, int w, int hh, int mm, int ss) {
95     Wire.beginTransmission(DS1307_ADDRESS);
96     Wire.send(0);
97     Wire.send(bin2bcd(ss));
98     Wire.send(bin2bcd(mm));
99     Wire.send(bin2bcd(hh));
100     Wire.send(w);
101     Wire.send(bin2bcd(d));
102     Wire.send(bin2bcd(m));
103     Wire.send(bin2bcd(y));
104     Wire.send(0);
105     Wire.endTransmission();
108 void check_time() {
109     Wire.beginTransmission(DS1307_ADDRESS);
110     Wire.send(0);
111     Wire.endTransmission();
113     Wire.requestFrom(DS1307_ADDRESS, 6);
114     Wire.receive();
115     int mm = bcd2bin(Wire.receive());
116     int hour = bcd2bin(Wire.receive());
117     int wday = Wire.receive();
118     int day = bcd2bin(Wire.receive());
119     int month = bcd2bin(Wire.receive());
121     if ( a < 0 ) {
122         for ( int i = 0; i < NALARMS ; i ++ ) {
123             // check alarm i
124             if ( ( alarms[i][0] & ( 1 << (wday - 1) ) ) || 
125                     (month == alarms[i][1] && day == alarms[i][2]) ) {
126                 // this is alarm day!
127                 if ( hour == alarms[i][3] && mm == alarms[i][4]) {
128                     // this is alarm hour!
129                     a = i;
130                     st = 0;
131                     cmin = mm;
132                     if ( ( alarms[i][0] & 128 ) == 0 ) {
133                         // this alarm won't be repeated
134                         alarms[i] = { 0,0,0,0,0 };
135                     }
136                     break;
137                 }
138             } 
139         }
140     } else {
141         if ( cmin < mm ) {
142             cmin = mm;
143             st++;
144         }
145     }
149 void set_leds() {
150   if ( st > 0 && st <= TIN) {
151       int y = int(float(st*255)/TIN);
152       int r = 255 - y;
153       analogWrite(RPIN,r);
154       analogWrite(YPIN,y);
155   } else if ( st > TIN && st < TDAY ) {
156       analogWrite(RPIN,0);
157       analogWrite(YPIN,255);
158       analogWrite(BPIN,0);
159   }else if (st >= TDAY) {
160       analogWrite(RPIN,0);
161       analogWrite(YPIN,0);
162       analogWrite(BPIN,255);
163   } else if (st == TOUT) {
164       // reset stuff
165       st = 0;
166       a = -1;
167       analogWrite(RPIN,255);
168       analogWrite(YPIN,0);
169       analogWrite(BPIN,0);
170   }
173 // BCD helper functions from adafruit-RTClib
174 static uint8_t bcd2bin (uint8_t val) { return val - 6 * (val >> 4); }
175 static uint8_t bin2bcd (uint8_t val) { return val + 6 * (val / 10); }
177 // vim: set filetype=c: