From 6c0e2a0c7e4f79087eccaca34bdefe86b6a9c423 Mon Sep 17 00:00:00 2001 From: Elena ``of Valhalla'' Grandi Date: Fri, 2 Sep 2011 23:05:49 +0200 Subject: Save status in the ds1307 RAM --- ds1307/arduino_sketch/fuzzy_alarm_clock_ds1307.pde | 27 ++++++++++++++-------- 1 file changed, 18 insertions(+), 9 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 224706a..54c6993 100644 --- a/ds1307/arduino_sketch/fuzzy_alarm_clock_ds1307.pde +++ b/ds1307/arduino_sketch/fuzzy_alarm_clock_ds1307.pde @@ -35,15 +35,9 @@ void setup () { 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 + // otherwise set everything to a sane 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; @@ -51,12 +45,17 @@ void setup () { } st = 0; a = -1; + save_status(); } 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 + // 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); @@ -95,6 +94,12 @@ void loop () { } +void save_status() { + RTC.writeData(0x08,st); + RTC.writeData(0x09,a); + RTC.writeData(0x0a,cmin); +} + // ****** Serial interface management *************************************** // void check_serial() { @@ -135,6 +140,7 @@ void s_set_alarm() { } void s_set_time() { + RTC.start(); RTC.setYear(s_read_2dig()); RTC.setMonth(s_read_2dig()); RTC.setDate(s_read_2dig()); @@ -271,6 +277,7 @@ void check_time() { 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 }; @@ -286,6 +293,7 @@ void check_time() { if ( cmin != mm ) { cmin = mm; st++; + save_status(); } } @@ -311,6 +319,7 @@ void set_leds() { // reset stuff st = 0; a = -1; + save_status(); analogWrite(RPIN,255); analogWrite(YPIN,0); analogWrite(BPIN,0); -- cgit v1.2.3