From 12954be78df7cb32eb0744c8dac0a0704f866bf7 Mon Sep 17 00:00:00 2001 From: Elena ``of Valhalla'' Grandi Date: Fri, 2 Sep 2011 19:09:18 +0200 Subject: Use RealTimeClockDS1307 library from https://github.com/davidhbrown/RealTimeClockDS1307 --- ds1307/arduino_sketch/fuzzy_alarm_clock_ds1307.pde | 120 ++++++++++----------- 1 file changed, 60 insertions(+), 60 deletions(-) (limited to 'ds1307/arduino_sketch') diff --git a/ds1307/arduino_sketch/fuzzy_alarm_clock_ds1307.pde b/ds1307/arduino_sketch/fuzzy_alarm_clock_ds1307.pde index fd0682c..53b2abe 100644 --- a/ds1307/arduino_sketch/fuzzy_alarm_clock_ds1307.pde +++ b/ds1307/arduino_sketch/fuzzy_alarm_clock_ds1307.pde @@ -1,5 +1,7 @@ #include +#include + // minutes of "dawn" before alarm #define TIN 30 // "dawn" + "daylight" @@ -14,49 +16,33 @@ #define RPIN 3 #define YPIN 5 #define BPIN 6 -#define DS1307_ADDRESS 0x68 int st = 0; // alarm status (minutes from alarm - TIN) -char alarms[NALARMS][5]; +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,0); + digitalWrite(YPIN,255); digitalWrite(BPIN,0); + + // DEBUG: we want to read the setup messages - // if the RTC is already running read alarms and status - Wire.beginTransmission(DS1307_ADDRESS); - Wire.send(0); - Wire.endTransmission(); - - Wire.requestFrom(DS1307_ADDRESS, 1); - char running = Wire.receive(); - running = !(running>>7); - - if ( running ) { - Wire.requestFrom(DS1307_ADDRESS, 12 + NALARMS * 5); - for (int i = 0; i < 8 ; i ++ ) { - Wire.receive(); - } - st = Wire.receive(); - a = Wire.receive(); - cmin = Wire.receive(); - for ( int i = 0; i < NALARMS ; i ++ ) { - for ( int j = 0; j < 5 ; j ++ ) { - alarms[i][j] = Wire.receive(); - } - } - } else { + 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; @@ -66,7 +52,19 @@ void setup () { a = -1; // DEBUG: we don't want to set the time until we receive // it from serial, but serial is not implemented yet - set_time(11,9,2,5,0,0,0); + 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); + } + } } } @@ -77,6 +75,7 @@ void loop () { // read time, check alarms check_time(); + //DEBUG Serial.println(st); // act on status: LEDs and buzzer @@ -85,39 +84,41 @@ void loop () { } // 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) { - Wire.beginTransmission(DS1307_ADDRESS); - Wire.send(0); - Wire.send(bin2bcd(ss)); - Wire.send(bin2bcd(mm)); - Wire.send(bin2bcd(hh)); - Wire.send(w); - Wire.send(bin2bcd(d)); - Wire.send(bin2bcd(m)); - Wire.send(bin2bcd(y)); - Wire.send(0); - Wire.endTransmission(); + 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() { - Wire.beginTransmission(DS1307_ADDRESS); - Wire.send(0); - Wire.endTransmission(); - - Wire.requestFrom(DS1307_ADDRESS, 6); - Wire.receive(); - int mm = bcd2bin(Wire.receive()); - int hour = bcd2bin(Wire.receive()); - int wday = Wire.receive(); - int day = bcd2bin(Wire.receive()); - int month = bcd2bin(Wire.receive()); - + 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 @@ -127,21 +128,24 @@ void check_time() { if ( hour == alarms[i][3] && mm == alarms[i][4]) { // this is alarm hour! a = i; - st = 0; + 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 ) { + if ( cmin != mm ) { cmin = mm; st++; - } + } } } @@ -156,7 +160,7 @@ void set_leds() { analogWrite(RPIN,0); analogWrite(YPIN,255); analogWrite(BPIN,0); - }else if (st >= TDAY) { + }else if (st >= TDAY && st < TOUT) { analogWrite(RPIN,0); analogWrite(YPIN,0); analogWrite(BPIN,255); @@ -170,8 +174,4 @@ void set_leds() { } } -// BCD helper functions from adafruit-RTClib -static uint8_t bcd2bin (uint8_t val) { return val - 6 * (val >> 4); } -static uint8_t bin2bcd (uint8_t val) { return val + 6 * (val / 10); } - // vim: set filetype=c: -- cgit v1.2.3