Arduino sketch: fixed led colors
[fuzzy_alarm_clock.git] / ds1307 / arduino_sketch / fuzzy_alarm_clock_ds1307.pde
blobbfedfd7e1be0683ab39abaed5332c2036ebe42b1
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
11 #define NALARMS 4
13 #define RPIN 3
14 #define YPIN 5
15 #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(57600);
26     Wire.begin();
27     
28     set_time(11,9,2,5,0,0,0);
30     pinMode(RPIN,OUTPUT);
31     pinMode(YPIN,OUTPUT);
32     pinMode(BPIN,OUTPUT);
34     digitalWrite(RPIN,255);
35     digitalWrite(YPIN,0);
36     digitalWrite(BPIN,0);
37     
38     // read alarms from storage
39     for ( int i = 0 ; i < NALARMS ; i ++ ) {
40         alarms[i][0] = 0;
41     }
44 void loop () {
45   
46   // read commands from serial
47   
48   // read time, check alarms
49   
50   check_time();
51   Serial.println(st);
53   
54   // act on status: LEDs and buzzer
55   if ( st > 0 ) {
56       set_leds();
57   }
58   
59   // wait about till the next second
60   
61   delay(1000);
62   
65 // Set the current time
66 void set_time(int y,int m,int d, int w, int hh, int mm, int ss) {
67     Wire.beginTransmission(DS1307_ADDRESS);
68     Wire.send(0);
69     Wire.send(bin2bcd(ss));
70     Wire.send(bin2bcd(mm));
71     Wire.send(bin2bcd(hh));
72     Wire.send(w);
73     Wire.send(bin2bcd(d));
74     Wire.send(bin2bcd(m));
75     Wire.send(bin2bcd(y));
76     Wire.send(0);
77     Wire.endTransmission();
80 void check_time() {
81     Wire.beginTransmission(DS1307_ADDRESS);
82     Wire.send(0);
83     Wire.endTransmission();
85     Wire.requestFrom(DS1307_ADDRESS, 6);
86     Wire.receive();
87     int mm = bcd2bin(Wire.receive());
88     int hour = bcd2bin(Wire.receive());
89     int wday = Wire.receive();
90     int day = bcd2bin(Wire.receive());
91     int month = bcd2bin(Wire.receive());
93     if ( a < 0 ) {
94         for ( int i = 0; i < NALARMS ; i ++ ) {
95             // check alarm i
96             if ( ( alarms[i][0] & ( 1 << (wday - 1) ) ) || 
97                     (month == alarms[i][1] && day == alarms[i][2]) ) {
98                 // this is alarm day!
99                 if ( hour == alarms[i][3] && mm == alarms[i][4]) {
100                     // this is alarm hour!
101                     a = i;
102                     st = 0;
103                     cmin = mm;
104                     if ( ( alarms[i][0] & 128 ) == 0 ) {
105                         // this alarm won't be repeated
106                         alarms[i] = { 0,0,0,0,0 };
107                     }
108                     break;
109                 }
110             } 
111         }
112     } else {
113         if ( cmin < mm ) {
114             cmin = mm;
115             st++;
116         }
117     }
121 void set_leds() {
122   if ( st > 0 && st <= TIN) {
123       int y = int(float(st*255)/TIN);
124       int r = 255 - y;
125       analogWrite(RPIN,r);
126       analogWrite(YPIN,y);
127   } else if ( st > TIN && st < TDAY ) {
128       analogWrite(RPIN,0);
129       analogWrite(YPIN,255);
130       analogWrite(BPIN,0);
131   }else if (st >= TDAY) {
132       analogWrite(RPIN,0);
133       analogWrite(YPIN,0);
134       analogWrite(BPIN,255);
135   } else if (st == TOUT) {
136       // reset stuff
137       st = 0;
138       a = -1;
139       analogWrite(RPIN,255);
140       analogWrite(YPIN,0);
141       analogWrite(BPIN,0);
142   }
145 // BCD helper functions from adafruit-RTClib
146 static uint8_t bcd2bin (uint8_t val) { return val - 6 * (val >> 4); }
147 static uint8_t bin2bcd (uint8_t val) { return val + 6 * (val / 10); }
149 // vim: set filetype=c: