// @author: Brad Fettes // @thanks: Matt Sparks // @date: December 9, 2010 // @purpose: Check the ds1302 to see if the chronobox should be locked or unlocked #include #include #include // Set the appropriate digital I/O pin connections uint8_t CE_PIN = 5; // RST uint8_t IO_PIN = 6; uint8_t SCLK_PIN = 7; // Create a DS1302 object DS1302 rtc(CE_PIN, IO_PIN, SCLK_PIN); // Create buffers char buf[50]; char day[10]; // Thanks to the Arduino library... void check_alarms() { // Get the current time and date from the chip Time t = rtc.time(); // Name the day of the week memset(day, 0, sizeof(day)); // clear day buffer // Check the hour snprintf( buf, sizeof(buf), "%02d:%02d:%02d", t.hr, t.min, t.sec ); Serial.print(buf); Serial.print(" - "); switch (t.hr) { case 21: // After 9 pm Serial.println("UNLOCKED"); break; case 22: // After 10 pm Serial.println("UNLOCKED"); break; case 23: // After 11 pm Serial.println("UNLOCKED"); break; default: Serial.println("LOCKED"); } Serial.println(); } void set_time() { /* Initialize a new chip by turning off write protection and clearing the clock halt flag. These methods needn't always be called. See the DS1302 datasheet for details. */ rtc.write_protect(false); rtc.halt(false); /* Make a new time object to set the date and time */ /* Thursday, Dec 9, 2010 at 11:11:00. */ Time t(2010, 12, 9, 11, 11, 0, 4); /* Set the time and date on the chip */ rtc.time(t); } void setup() { delay(2500); // Ensure that we have a few seconds to upload code Serial.begin(9600); // then start the serial communications // Uncomment to set the time to the one defined above // make sure to upload it again uncommented or it will reset the // date each time gets powered up //set_time(); } /* Loop and print the time every second */ void loop() { check_alarms(); // Lock or Unlock delay(1000); // wait one sec }