aboutsummaryrefslogtreecommitdiff
path: root/ds1307/arduino_sketch/fuzzy_alarm_clock_ds1307.pde
blob: 8c24791ec390064f6953667a4c841bca6426cd74 (plain)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#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
char dbg = 0; // print debug informations

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);
   
    // if the RTC is already running read alarms and status, 
    // otherwise set everything to a sane default
    if ( RTC.readData(0x00) >> 7 ) {
        for ( int i = 0 ; i < NALARMS ; i ++ ) {
            for ( int j = 0; j < 5 ; j ++ ) {
                alarms[i][j] = 0;
            }
        }
        st = 0;
        a = -1;
        save_status();
    } else {
        st = RTC.readData(0x08);
        a = RTC.readData(0x09);
        cmin = RTC.readData(0x0a);
        // This only works if the arduino had no power for a 
        // "short" time. This is by design. :D
        RTC.readClock();
        st = st + (RTC.getMinutes() - cmin) % 60;
        cmin = RTC.getMinutes();
        save_status();
        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
  check_serial();
  
  // read time, check alarms
  check_time();

  if ( dbg ) {
      s_print_time();
      Serial.print("st: ");
      Serial.print(st,DEC);
      Serial.print(", a: ");
      Serial.print(a,DEC);
      Serial.print(", cmin: ");
      Serial.println(cmin,DEC);
  }
  
  // act on status: LEDs and buzzer
  if ( st > 0 ) {
      set_leds();
  }
  
  // wait about till the next second

  delay(1000);
  
}

void save_status() {
    RTC.writeData(0x08,st);
    RTC.writeData(0x09,a);
    RTC.writeData(0x0a,cmin);
}

// ****** Serial interface management *************************************** //

void check_serial() {
    int rec = Serial.read();
    switch (rec) {
        case 'a':
            s_set_alarm();
            break;
        case 's':
            s_set_time();
            break;
        case 'p':
            s_print_alarms();
            break;
        case 't':
            s_print_time();
            break;
        case 'd':
            s_toggle_debug();
            break;
        case 'h':
            s_print_help();
            break;
    }
}

void s_set_alarm() {
    int i = s_read_dig();
    for ( int j = 0; j < 5 ; j++) {
        alarms[i][j] = s_read_2hex();
    }
    for ( int j = 0; j < 5 ; j++ ) {
        RTC.writeData(0x0b + i*5 + j,alarms[i][j]);
    }
    Serial.print("Alarm ");
    Serial.print(i,DEC);
    Serial.println(" set.");
}

void s_set_time() {
    RTC.start();
    RTC.setYear(s_read_2dig());
    RTC.setMonth(s_read_2dig());
    RTC.setDate(s_read_2dig());
    RTC.setDayOfWeek(s_read_dig());
    RTC.setHours(s_read_2dig());
    RTC.setMinutes(s_read_2dig());
    RTC.setSeconds(s_read_2dig());
    RTC.setClock();
    Serial.print("Time set: ");
    s_print_time();
}

int s_read_dig() {
    int rec;
    rec = Serial.read();
    while ( rec == -1 ) {
        rec = Serial.read();
    }
    return rec - 48;
}

int s_read_2dig() {
    int n;
    n = s_read_dig() * 10;
    n = n + s_read_dig();
    return n;
}

int s_read_hex() {
    int rec;
    rec = Serial.read();
    while ( rec == -1 ) {
        rec = Serial.read();
    }
    if ( rec >= 48 && rec < 58 ) {
        return rec - 48;
    } else if ( rec >= 65 && rec < 71 ) {
        return rec - 55;
    } else if ( rec >= 97 && rec < 102 ) {
        return rec - 87;
    } else {
        return 0;
    }
}

int s_read_2hex() {
    int n;
    n = s_read_hex() * 16;
    n = n + s_read_hex();
    return n;
}

void s_print_alarms() {
    for ( int i = 0; i < NALARMS ; i++) {
        Serial.print(i,DEC);
        Serial.print(" - ");
        for ( int j = 0; j < 5 ; j++) {
            Serial.print(alarms[i][j],DEC);
            Serial.print(" ");
        }
        Serial.println("");
    }
}

void s_print_time() {
    RTC.readClock();
    Serial.print(RTC.getYear(),DEC);
    Serial.print("/");
    Serial.print(RTC.getMonth(),DEC);
    Serial.print("/");
    Serial.print(RTC.getDate(),DEC);
    Serial.print(" (");
    Serial.print(RTC.getDayOfWeek(),DEC);
    Serial.print(") ");
    Serial.print(RTC.getHours(),DEC);
    Serial.print(":");
    Serial.print(RTC.getMinutes(),DEC);
    Serial.print(":");
    Serial.println(RTC.getSeconds(),DEC);
}

void s_toggle_debug() {
    if ( dbg ) {
        dbg = 0;
    } else {
        dbg = 1;
    }
}

void s_print_help() {
    Serial.println("");
    Serial.println("  a<s> - set an alarm");
    Serial.println("         <s> is nhhhhhhhhhh");
    Serial.println("         where hh are hex values");
    Serial.println("  s<s> - set the clock");
    Serial.println("         <s> is yymmgguHHMMSS");
    Serial.println("  p    - print the alarms");
    Serial.println("  t    - print the clock");
    Serial.println("  d    - toggle printing of debug informations");
    Serial.println("  h    - print this help");
}

// ****** Time management *************************************************** //

// 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();

    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;
                    save_status();
                    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++;
            save_status();
        } 
    }

}

// ****** LED management **************************************************** //

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;
      save_status();
      analogWrite(RPIN,255);
      analogWrite(YPIN,0);
      analogWrite(BPIN,0);
  }
}

// vim: set filetype=c: