// RealTime Accelerometer clock // Ken Wallich // October, 2009 // http://kencasting.com // DS1307 time setting and retrieval code from: // Maurice Ribble // 4-17-2008 // http://www.glacialwanderer.com/hobbyrobotics // This code used the DS1307 Real Time clock on the Arduino board. // The ds1307 works in binary coded decimal or BCD. You can look up // bcd in google if you aren't familior with it. #include "Wire.h" #include #include #include #define DEBUG // Modes in the clock set mode. #define CLOCKMODE 0 #define ALARMTIME 1 #define ALARMSET 2 #define ALARMMODE 3 #define BRIGHTMODE 4 #define BACKLIGHTTIMER 5 #define IRPOWER 6 #define DSTSET 7 #define CALIBRATE 8 #define CONTRASTMODE 9 #define TIMESET 10 #define TIMEADJ 11 #define ENDMODE 12 #define DS1307_I2C_ADDRESS 0x68 #define TIME_MSG_LEN 11 // time sync to PC is HEADER followed by unix time_t as ten ascii digits #define TIME_HEADER 255 // Header tag for serial time sync message void lcdDigits(byte digits, bool colon=true); //LCD Pins: 1:GND 2:VDD 3:VO 4:RS 5:RW 6:E 7-14: DB0-DB7 15:BLAnode 16:BLCathode /* LiquidCrystal(rs, enable, d4, d5, d6, d7) rs: the number of the Arduino pin that is connected to the RS pin on the LCD rw: the number of the Arduino pin that is connected to the RW pin on the LCD (optional) enable: the number of the Arduino pin that is connected to the enable pin on the LCD d0, d1, d2, d3, d4, d5, d6, d7: the numbers of the Arduino pins that are connected to the corresponding data pins on the LCD. d0, d1, d2, and d3 are optional; if omitted, the LCD will be controlled using only the four data lines (d4, d5, d6, d7). */ LiquidCrystal lcd(12, 13, 7, 6, 5, 4); int zCenter = 340; int xCenter = 380; int yCenter = 380; int xRight = xCenter + 40; int xLeft = xCenter - 40; int zUp = zCenter - 20; int zDown = zCenter + 20; int lcdBacklightPin = 3; int lcdBacklightState = 0; unsigned long lcdBacklightDelay = 300000; // in milliseconds. Init to 5 minutes. byte lcdBacklightDelaySec; // Used for storing onto Real Time Clock unsigned long lcdBacklightTimer; int sensorPin = 0; // select the input pin for the potentiometer int ledPin = 13; // select the pin for the LED int sensorValue = 0; // variable to store the value coming from the sensor int zPin = 0; int yPin = 1; int xPin = 2; int irPin = 3; int irPowerPin = 2; unsigned int irDistance = 100; boolean irOn = true; int piezoPin = 8; int rPin = 9; int gPin = 10; int bPin = 11; int clockState = CLOCKMODE; byte aHour = 7; byte aMin = 0; boolean aSet = false; byte aMode = 0; byte cBright = 100; int alarmSilence = 0; int cContrast = 5; boolean DST = false; // ******************************************************************************* // ********** Real Time clock functions ******* // ******************************************************************************* // Convert normal decimal numbers to binary coded decimal byte decToBcd(byte val) { return ( (val/10*16) + (val%10) ); } // Convert binary coded decimal to normal decimal numbers byte bcdToDec(byte val) { return ( (val/16*10) + (val%16) ); } // Stops the DS1307, but it has the side effect of setting seconds to 0 // Probably only want to use this for testing /*void stopDs1307() { Wire.beginTransmission(DS1307_I2C_ADDRESS); Wire.send(0); Wire.send(0x80); Wire.endTransmission(); }*/ // 1) Sets the date and time on the ds1307 // 2) Starts the clock // 3) Sets hour mode to 24 hour clock // Assumes you're passing in valid numbers void setDateDs1307(byte second, // 0-59 byte minute, // 0-59 byte hour, // 1-23 byte dayOfWeek, // 1-7 byte dayOfMonth, // 1-28/29/30/31 byte month, // 1-12 byte year) // 0-99 { Wire.beginTransmission(DS1307_I2C_ADDRESS); Wire.send(0); Wire.send(decToBcd(second)); // 0 to bit 7 starts the clock Wire.send(decToBcd(minute)); Wire.send(decToBcd(hour)); // If you want 12 hour am/pm you need to set // bit 6 (also need to change readDateDs1307) Wire.send(decToBcd(dayOfWeek)); Wire.send(decToBcd(dayOfMonth)); Wire.send(decToBcd(month)); Wire.send(decToBcd(year)); Wire.endTransmission(); } void setSettingDs1307(byte hour, byte minute, byte alarmSet, byte alarmMode, byte backlight, byte backlightDelay, byte DSTMode, byte irIsOn) { Wire.beginTransmission(DS1307_I2C_ADDRESS); Wire.send(0x08); // Just after time Wire.send(decToBcd(hour)); Wire.send(decToBcd(minute)); Wire.send(decToBcd(alarmSet)); Wire.send(decToBcd(alarmMode)); Wire.send(backlight); // Range is 0-255 so we don't want to pack and loose resolution Wire.send(decToBcd(backlightDelay)); Wire.send(decToBcd(DSTMode)); Wire.send(decToBcd(irIsOn)); Wire.endTransmission(); } void getSettingDs1307(byte *hour, byte *minute, byte *alarmSet, byte *alarmMode, byte *backlight, byte *backlightDelay, byte *DSTMode, byte *irIsOn) { Wire.beginTransmission(DS1307_I2C_ADDRESS); Wire.send(0x08); Wire.endTransmission(); Wire.requestFrom(DS1307_I2C_ADDRESS, 8); // A few of these need masks because certain bits are control bits *hour = bcdToDec(Wire.receive()); *minute = bcdToDec(Wire.receive()); *alarmSet = bcdToDec(Wire.receive()); *alarmMode = bcdToDec(Wire.receive()); *backlight = Wire.receive(); // Range is 0-255, we don't pack the backlight brightness *backlightDelay = bcdToDec(Wire.receive()); *DSTMode = bcdToDec(Wire.receive()); *irIsOn = bcdToDec(Wire.receive()); } // Gets the date and time from the ds1307 void getDateDs1307(byte *second, byte *minute, byte *hour, byte *dayOfWeek, byte *dayOfMonth, byte *month, byte *year) { // Reset the register pointer Wire.beginTransmission(DS1307_I2C_ADDRESS); Wire.send(0); Wire.endTransmission(); Wire.requestFrom(DS1307_I2C_ADDRESS, 7); // A few of these need masks because certain bits are control bits *second = bcdToDec(Wire.receive() & 0x7f); *minute = bcdToDec(Wire.receive()); *hour = bcdToDec(Wire.receive() & 0x3f); // Need to change this if 12 hour am/pm *dayOfWeek = bcdToDec(Wire.receive()); *dayOfMonth = bcdToDec(Wire.receive()); *month = bcdToDec(Wire.receive()); *year = bcdToDec(Wire.receive()); if (DST) *hour = *hour + 1; } // **************************************************************************************** // *********** LCD/Display functions ********** // **************************************************************************************** void digitalClockDisplay(){ // digital clock display of current date and time /* Serial.print(DateTime.Hour,DEC); printDigits(DateTime.Minute); printDigits(DateTime.Second); Serial.print(" "); Serial.print(DateTimeStrings.dayStr(DateTime.DayofWeek)); Serial.print(" "); Serial.print(DateTimeStrings.monthStr(DateTime.Month)); Serial.print(" "); Serial.println(DateTime.Day,DEC); */ //lcd.clear(); Clearing each time will cause the display to blink. lcd.setCursor(0, 0); lcd.print(DateTimeStrings.dayStr(DateTime.DayofWeek)); lcd.print(""); lcdDigits(DateTime.Day,false); lcd.print(DateTimeStrings.monthStr(DateTime.Month)); lcdDigits(DateTime.Year-100,false); lcd.print(" ") ; // Sometimes a leftover character appears at the end of the 1st line lcd.setCursor(0, 1); if (aSet) { switch (aMode) { case 0: lcd.print("+"); break; case 1: lcd.print("o"); break; case 2: lcd.print("*"); break; } } else lcd.print(" "); lcd.print(" "); lcd.print(DateTime.Hour,DEC); lcdDigits(DateTime.Minute); lcdDigits(DateTime.Second); lcd.print(" "); if (irOn == false) lcd.print("-"); else lcd.print(" "); } void printDigits(byte digits){ // utility function for digital clock display: prints preceding colon and leading 0 Serial.print(":"); if(digits < 10) Serial.print('0'); Serial.print(digits,DEC); } void lcdDigits(byte digits, bool colon){ // utility function for digital clock display: prints preceding colon and leading 0 if (colon) lcd.print(":"); if(digits < 10) lcd.print('0'); lcd.print(digits,DEC); } void lcdShow(char *s) { lcd.clear(); lcd.setCursor(0,0); lcd.print(s); } void setBacklight(boolean l=true) { if (l) { analogWrite(lcdBacklightPin, cBright); lcdBacklightTimer = millis() + lcdBacklightDelay; // set backlight timer to 5 minutes lcdBacklightDelaySec = lcdBacklightDelay/60000; lcdBacklightState = 1; } else { analogWrite(lcdBacklightPin, 0); lcdBacklightState = 0; } } void soundAlarm() { int i; int x, y, z; // Assumes rPin, gPin, and bPin are sequential, low to high if (irOn == false) digitalWrite(irPowerPin, HIGH); // Turn IR power on just during duration of alarm to save power, if off if ((aMode == 1) || (aMode == 2)) digitalWrite(piezoPin, HIGH); delay(100); if ((aMode == 0) || (aMode == 2)) { for (i = 0; i < 5; i++) { analogWrite(random(rPin,bPin+1), 0); // Note, LED has Anode on 5V, so 0 is ON, 255 is OFF delay(100); analogWrite(random(rPin,bPin+1), random(10, 255)); getAccel(&x, &y, &z); irDistance = analogRead(irPin); if ((z > zDown) || (z < zUp) || (irDistance > 400)) { lcdShow("Alarm silence"); alarmSilence = 1; break; } } analogWrite(rPin, 255); analogWrite(gPin, 255); analogWrite(bPin, 255); } if ((aMode == 1) || (aMode == 2)) digitalWrite(piezoPin, LOW); if (irOn == false) digitalWrite(irPowerPin, LOW); // Turn off IR power if it was off coming into the routine } // This is the big "set the modes" routine. Uses the accelerometer to set the alarm and it's modes // brightness, time adjustments for the real time clock, etc. void setClockMode(int clockMode) { int x, y, z; int i; unsigned long t1, t2, t3, t4; int t; t1 = millis(); t3 = t1 + 10000; setBacklight(); while (millis() < t3) { #ifdef DEBUG Serial.print(" t1: "); Serial.print(t1); Serial.print(" t2: "); Serial.print(t2); Serial.print(" t3: "); Serial.print(t3); Serial.print(" ms: "); Serial.println(millis()); Serial.print("clockState: "); Serial.print(clockState); #endif DEBUG switch (clockState) { case ALARMTIME: lcdShow("Alarm Time"); t2 = millis() + 1000; while (millis() < t2) { getAccel(&x, &y, &z); if (z > zDown) { t4 = millis() + 10000; t2 = t4; lcdShow("Set Alarm Time"); lcd.setCursor(0,1); lcdDigits(aHour, false); lcdDigits(aMin, true); while(millis() < t4) { getAccel(&x, &y, &z); if (x > xRight+5) { aMin = ((aMin + 1) % 60); Serial.print("(x:"); Serial.print(xRight); lcd.setCursor(0,1); lcdDigits(aHour, false); lcdDigits(aMin, true); t4 = t4+600; delay(max((xCenter - ((x - xRight)*10)), 50)); // Roll digits faster as rotation increases } else if (x < xLeft - 5) { aHour = ((aHour + 1) % 24); lcd.setCursor(0,1); lcdDigits(aHour, false); lcdDigits(aMin, true); t4 = t4+400; delay(max(300, (xCenter - ((xLeft - x)*10)))); } else if ((z > (zDown + 30)) || (z < (zUp - 15))) { lcdShow("Exiting setting"); return; } } // while } } break; case ALARMSET: lcdShow("Alarm Setting"); delay(150); t2 = millis() + 1000; while (millis() < t2) { getAccel(&x, &y, &z); if (z > zDown) { t4 = millis() + 5000; t2 = t4; lcdShow("Set Alarm"); while(millis() < t4) { lcd.setCursor(0,1); if (aSet) lcd.print("Alarm On "); else lcd.print("Alarm Off"); getAccel(&x, &y, &z); if (x > xRight) { aSet = true; } else if (x < xLeft) { aSet = false; } else if ((z > (zDown + 30)) || (z < (zUp - 15))) { lcdShow("Exiting setting"); return; } } // while } } break; case ALARMMODE: lcdShow("Alarm Type"); t2 = millis() + 1000; while (millis() < t2) { getAccel(&x, &y, &z); if (z > zDown) { t4 = millis() + 8000; t2 = t4; lcdShow("Set Alarm Type"); while(millis() < t4) { lcd.setCursor(0,1); switch (aMode) { case 0: lcd.print("LED "); /* analogWrite(rPin, 0); delay(10); analogWrite(gPin, 0); delay(10); analogWrite(bPin, 0); delay(50); analogWrite(rPin, 255); analogWrite(gPin, 255); analogWrite(bPin, 255); */ break; case 1: lcd.print("Piezo "); /* digitalWrite(piezoPin, HIGH); delay(50); digitalWrite(piezoPin, LOW); */ break; case 2: lcd.print("LED+Piezo "); /* digitalWrite(piezoPin, HIGH); analogWrite(rPin, 0); delay(10); analogWrite(gPin, 0); delay(10); analogWrite(bPin, 0); delay(50); analogWrite(rPin, 255); // turn off the voltage on pin 4 to 0 volts analogWrite(gPin, 255); // turn off the voltage on pin 4 to 0 volts // wait for 500 milli seconds analogWrite(bPin, 255); // turn on the voltage on pin 4 to 5 volts digitalWrite(piezoPin, LOW); */ break; } getAccel(&x, &y, &z); if (x > xRight) { t = aMode; aMode = min((t + 1), 2); Serial.print("aMode: "); Serial.println(aMode); lcd.setCursor(0,1); switch (aMode) { case 0: lcd.print("LED "); break; case 1: lcd.print("Piezo "); break; case 2: lcd.print("LED+Piezo "); break; } delay(750); } else if (x < xLeft) { t=aMode; aMode = max((t - 1), 0); Serial.print("aMode: "); Serial.println(aMode); lcd.setCursor(0,1); switch (aMode) { case 0: lcd.print("LED "); break; case 1: lcd.print("Piezo "); break; case 2: lcd.print("LED+Piezo "); break; } delay(750); } else if ((z > (zDown + 30)) || (z < (zUp - 15))) { lcdShow("Exiting setting"); return; } } // while } } break; case BRIGHTMODE: lcdShow("Brightness"); t2 = millis() + 1000; while (millis() < t2) { getAccel(&x, &y, &z); if (z > zDown) { t4 = millis() + 10000; t2 = t4; lcdShow("Set Brightness"); lcd.setCursor(0,1); lcdDigits(cBright, false); while(millis() < t4) { getAccel(&x, &y, &z); if (x > xRight) { cBright = byte(min((cBright + 10), 255)); lcd.setCursor(0,1); if (cBright < 255) lcdDigits(cBright, false); else lcd.print("Max"); lcd.print(" "); // overwrite any extra digits setBacklight(); t4 = t4+55; delay(150); } else if (x < xLeft) { cBright = byte(max((cBright - 10), 10)); lcd.setCursor(0,1); if (cBright > 10) lcdDigits(cBright, false); else lcd.print("Min"); lcd.print(" "); // overwrite any extra digits setBacklight(); t4 = t4+55; delay(150); } else if ((z > (zDown + 30)) || (z < (zUp - 15))) { lcdShow("Exiting setting"); return; } } // while } } break; case BACKLIGHTTIMER: lcdShow("BLight Delay"); t2 = millis() + 1000; while (millis() < t2) { getAccel(&x, &y, &z); if (z > zDown) { t4 = millis() + 10000; t2 = t4; lcdShow("Set BLight Delay"); lcd.setCursor(0,1); lcdDigits(lcdBacklightDelay/60000, false); while(millis() < t4) { getAccel(&x, &y, &z); if (x > xRight) { lcdBacklightDelay = min((lcdBacklightDelay + 60000), 3600000); lcd.setCursor(0,1); if (lcdBacklightDelay < 3600000) lcdDigits((lcdBacklightDelay/60000), false); else lcd.print("Max"); lcd.print(" "); // overwrite any extra digits setBacklight(); t4 = t4+75; delay(150); } else if (x < xLeft) { lcdBacklightDelay = max((lcdBacklightDelay - 60000), 60000); lcd.setCursor(0,1); if (lcdBacklightDelay > 60000) lcdDigits(lcdBacklightDelay/60000, false); else lcd.print("Min"); lcd.print(" "); // overwrite any extra digits setBacklight(); t4 = t4+75; delay(150); } else if ((z > (zDown + 30)) || (z < (zUp - 15))) { lcdShow("Exiting setting"); return; } } // while } } break; case IRPOWER: lcdShow("IR Power Setting"); delay(150); t2 = millis() + 1000; while (millis() < t2) { getAccel(&x, &y, &z); if (z > zDown) { t4 = millis() + 5000; t2 = t4; lcdShow("Set Power"); while(millis() < t4) { lcd.setCursor(0,1); if (irOn) lcd.print("Power On "); else lcd.print("Power Off"); getAccel(&x, &y, &z); if (x > xRight) { irOn = true; } else if (x < xLeft) { irOn = false; } else if ((z > (zDown + 30)) || (z < (zUp - 15))) { lcdShow("Exiting setting"); return; } } // while } } break; case DSTSET: lcdShow("DST Setting"); delay(150); t2 = millis() + 1000; while (millis() < t2) { getAccel(&x, &y, &z); if (z > zDown) { t4 = millis() + 5000; t2 = t4; lcdShow("Set DST"); while(millis() < t4) { lcd.setCursor(0,1); if (DST) lcd.print("DST On "); else lcd.print("DST Off"); getAccel(&x, &y, &z); if (x > xRight) { DST = true; } else if (x < xLeft) { DST = false; } else if ((z > (zDown + 30)) || (z < (zUp - 15))) { lcdShow("Exiting setting"); return; } } // while } } break; case CALIBRATE: lcdShow("Calibrate"); t2 = millis() + 1000; while (millis() < t2) { getAccel(&x, &y, &z); if (z > zDown) { t4 = millis() + 5000; t2 = t4; // Calibration countdown for (i = 10; i > 0; i--) { lcdShow("Calibrating"); lcd.setCursor(0, 1); lcd.print("in: "); lcd.print(i); delay(500); getAccel(&x, &y, &z); if ((z > (zDown + 30)) || (z < (zUp - 15))) { lcdShow("Exiting setting"); return; } delay(500); } calibrate(); } } break; case ENDMODE: //lcdShow("Clock"); delay(500); clockState = CLOCKMODE; break; default: clockState = CLOCKMODE; } // switch getAccel(&x, &y, &z); if (z < 315) { clockState++; } if (clockState == CLOCKMODE) return; } // while clockState = CLOCKMODE; } // ************************************************************************** // *************** General support functions // ************************************************************************** // If serial port is attaached, we'll want to see if the time is set externally // I used this early on to set time from the PC, left it here for just in case - Ken boolean getPCtime() { // if time sync available from serial port, update time and return true while(Serial.available() >= TIME_MSG_LEN ){ // time message consists of a header and ten ascii digits if( Serial.read() == TIME_HEADER ) { time_t pctime = 0; for(int i=0; i < TIME_MSG_LEN -1; i++){ char c= Serial.read(); if( c >= '0' && c <= '9'){ pctime = (10 * pctime) + (c - '0') ; // convert digits to a number } } DateTime.sync(pctime); // Sync Arduino clock to the time received on the serial port //setDateDs1307(DateTime.Second, DateTime.Minute, DateTime.Hour, DateTime.DayofWeek, DateTime.Day, DateTime.Month, DateTime.Year); Serial.println("ack"); return true; // return true if time message received on the serial port } } return false; //if no message return false } // ***************************************************************************** // ********* Accelerometer functions // ***************************************************************************** void calibrate() { int x, y, z; getAccel(&x, &y, &z); zCenter = z; xCenter = x; yCenter = y; xRight = xCenter + 30; xLeft = xCenter - 30; zUp = zCenter - 20; zDown = zCenter + 20; } void getAccel(int *xVal, int *yVal, int *zVal) { *xVal = analogRead(xPin); *yVal = analogRead(yPin); *zVal = analogRead(zPin); } void accelDisplay(int x, int y,int z ) { lcd.clear(); lcd.setCursor(0, 1); // print the number of seconds since reset: //lcd.print(millis()/1000); lcd.print("x:"); lcd.print(x); lcd.print("y:"); lcd.print(y); lcd.print("z:"); lcd.print(z); } boolean isAbout(int a, int b, int v=20) { // Return true if a is within v of b if ((a > b - v) && (a < b + v)) return true; else return false; } //Track arduino drift int resyncCount=0; void setup() { byte second, minute, hour, dayOfWeek, dayOfMonth, month, year; int i; time_t time; Wire.begin(); Serial.begin(19200); pinMode(piezoPin, OUTPUT); // Change these values to what you want to set your time to. // You probably only want to set your clock once and then remove // the setDateDs1307 call. second = 30; minute = 4; hour = 19; dayOfWeek = 7; dayOfMonth = 31; month = 10; year = 9; // To reset the time: // measure number of seconds to upload the sketch // manually set the time here, adjusting for upload time // enable the setDateDs1307 below // upload the sketch // disable the setDateDs1307 below // upload the sketch again // Probably would be cool to grab the time from the PC, but getting the serial reads and writes working // reliably was difficult, and the time needs to be reset just a few times a year //setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year); getSettingDs1307(&aHour, &aMin, &aSet, &aMode, &cBright,&lcdBacklightDelaySec, &DST, &irOn); #ifdef DEBUG Serial.print("aHour: "); Serial.print(aHour, DEC); Serial.print(" aMin: "); Serial.print(aMin, DEC); Serial.print(" aSet" ); Serial.print(aSet, DEC); Serial.print(" aMode: "); Serial.print(aMode, DEC); Serial.print(" cBright "); Serial.print(cBright, DEC); Serial.print(" lcdBLD: "); Serial.print(lcdBacklightDelaySec, DEC); Serial.print(" irOn: "); Serial.print(irOn, DEC); #endif DEBUG // Make sure LED's are off at init analogWrite(rPin, 255); // turn off the voltage on pin 4 to 0 volts analogWrite(gPin, 255); // turn off the voltage on pin 4 to 0 volts analogWrite(bPin, 255); // turn on the voltage on pin 4 to 5 volts lcdBacklightDelay = lcdBacklightDelaySec * 60000; // set up the LCD's number of rows and columns: pinMode(irPowerPin, OUTPUT); if (irOn) digitalWrite(irPowerPin, HIGH); else digitalWrite(irPowerPin, LOW); lcd.begin(16, 2); pinMode(lcdBacklightPin, OUTPUT); lcd.clear(); // Print a message to the LCD. lcd.print("Kaydub Klock 0.9"); setBacklight(); getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year); time = DateTime.makeTime(second, minute, hour, dayOfMonth, month-1, year+2000 ); DateTime.sync(time); // Calibration countdown for (i = 10; i > 0; i--) { lcdShow("Init Calibration"); lcd.setCursor(0, 1); lcd.print("in: "); lcd.print(i); delay(1000); } calibrate(); digitalClockDisplay(); } void loop() { byte second, minute, hour, dayOfWeek, dayOfMonth, month, year; int x, y, z; time_t time; getAccel(&x, &y, &z); // accelDisplay(x, y, z); getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year); irDistance = analogRead(irPin); #ifdef DEBUG //Accelerometer Serial.print("LED: "); Serial.print(irDistance); Serial.print(" X: "); Serial.print(x); Serial.print(" Y: "); Serial.print(y); Serial.print(" Z: "); Serial.println(z); //Clock Serial.print(hour, DEC); Serial.print(":"); Serial.print(minute,DEC); Serial.print(":"); Serial.print(second, DEC); Serial.print(" "); Serial.print(month, DEC); Serial.print("/"); Serial.print(dayOfMonth, DEC); Serial.print("/"); Serial.print(year+2000, DEC); Serial.print(" Day_of_week:"); Serial.print(dayOfWeek, DEC); Serial.print(" ResyncCount: "); Serial.print(resyncCount); Serial.print("("); Serial.print(millis()/1000); Serial.println(")"); #endif time = DateTime.makeTime(second, minute, hour, dayOfMonth, month-1, year+2000 ); // Fix any Arduino drift if (DateTime.available() && time == DateTime.now()) { //Serial.println("Synced"); } else { Serial.println("NOT synced, syncing"); DateTime.sync(time); resyncCount++; } // Check Alarm if (aSet) { if( (aHour == hour) && (aMin == minute) && (second < 30) && (alarmSilence == 0)) { digitalClockDisplay(); setBacklight(); soundAlarm(); } else if ((alarmSilence) && (second > 30)) alarmSilence = 0; } // Check backlight timer if (lcdBacklightTimer < millis()) setBacklight(false); // Check for motion if (irOn && analogRead(irPin) > 400) setBacklight(); //DateTime.sync(makeTime(byte second, byte minute, byte hour, byte dayOfWeek, byte month, int year+2000 )); //accelDisplay(x, y, z); if ((isAbout(x,xCenter, 30)) && (isAbout(y, yCenter, 30)) && (isAbout(z, zCenter,30))) { digitalClockDisplay(); } else { if (z < zUp) { // Note Y also changes on "rollup", could use Z or Y. //lcdShow("Settings"); //delay(200); clockState++; setClockMode(clockState); setSettingDs1307(aHour, aMin, aSet, aMode, cBright,lcdBacklightDelaySec, DST, irOn); if (irOn) digitalWrite(irPowerPin, HIGH); else digitalWrite(irPowerPin, LOW); clockState = CLOCKMODE; } else if (z > zDown) { setBacklight(); lcdShow("Roll down"); } else if (x > xRight) { setBacklight(); lcdShow("Roll right"); } else if (x < xLeft) { setBacklight(); lcdShow("Roll left"); } else if (y < 320) { lcdShow("Inverted"); if (lcdBacklightState) { setBacklight(false); } else { setBacklight(); } } else accelDisplay(x, y, z); } delay(500); // Don't really need to delay, but may later toss in power saving routines here }