#include #include // 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: