|
| 1 | +/*----------------------------------------------------------------------* |
| 2 | + * Example sketch for Arduino MCP79412 Library by Jack Christensen * |
| 3 | + * * |
| 4 | + * Set the RTC by entering a "Set" command on the serial monitor. * |
| 5 | + * Use a 24-hour clock and enter the command (case sensitive) exactly * |
| 6 | + * as follows: Set yyyy-mm-dd hh:mm:ss * |
| 7 | + * * |
| 8 | + * This work is licensed under the Creative Commons Attribution- * |
| 9 | + * ShareAlike 3.0 Unported License. To view a copy of this license, * |
| 10 | + * visit http://creativecommons.org/licenses/by-sa/3.0/ or send a * |
| 11 | + * letter to Creative Commons, 171 Second Street, Suite 300, * |
| 12 | + * San Francisco, California, 94105, USA. * |
| 13 | + *----------------------------------------------------------------------*/ |
| 14 | + |
| 15 | +#include <MCP79412RTC.h> //http://github.com/JChristensen/MCP79412RTC |
| 16 | +#include <Time.h> //http://www.arduino.cc/playground/Code/Time |
| 17 | +#include <Wire.h> //http://arduino.cc/en/Reference/Wire (included with Arduino IDE) |
| 18 | + |
| 19 | +#define PRINT_INTERVAL 1000 //ms between printing the time |
| 20 | + |
| 21 | +unsigned long ms, msLast; |
| 22 | + |
| 23 | +void setup() |
| 24 | +{ |
| 25 | + delay(2000); |
| 26 | + Serial.begin(9600); |
| 27 | + |
| 28 | + setSyncProvider(RTC.get); //the function to get the time from the RTC |
| 29 | + Serial.print("RTC SYNC"); |
| 30 | + if (timeStatus() != timeSet) Serial.print(" FAIL"); |
| 31 | + Serial.println(); |
| 32 | +} |
| 33 | + |
| 34 | +void loop(void) |
| 35 | +{ |
| 36 | + ms = millis(); |
| 37 | + readCommand(); |
| 38 | + if (ms - msLast >= PRINT_INTERVAL) { |
| 39 | + msLast = ms; |
| 40 | + printTime(now()); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +//Read command from the Arduino serial monitor to set the RTC. |
| 45 | +//Case-sensitive and must be entered exactly as (24-hour clock): |
| 46 | +// Set yyyy-mm-dd hh:mm:ss |
| 47 | +void readCommand() |
| 48 | +{ |
| 49 | + char cmd[24] = "Set yyyy-mm-dd hh:mm:ss"; |
| 50 | + static int i; |
| 51 | + tmElements_t tmSet; |
| 52 | + time_t tSet; |
| 53 | + |
| 54 | + if (Serial.available() >= 23) { //enough characters for the whole command? |
| 55 | + i = 0; //yes, read the available characters |
| 56 | + while (Serial.available() > 0) { |
| 57 | + if (i >= sizeof(cmd) - 1) { //more than we can enjoy |
| 58 | + flushInput(); //clear out the input buffer |
| 59 | + Serial.print("Too long: "); |
| 60 | + Serial.println(cmd); |
| 61 | + return; |
| 62 | + } |
| 63 | + delay(2); //let the next character trickle in |
| 64 | + cmd[i++] = char(Serial.read()); |
| 65 | + } |
| 66 | + cmd[i] = 0; //put in string terminator |
| 67 | + |
| 68 | + if (strncmp(cmd, "Set ", 4) == 0) { |
| 69 | + tmSet.Year = 1000 * (cmd[4] - '0') + 100 * (cmd[5] - '0') + 10 * (cmd[6] - '0') + cmd[7] - '0' - 1970; |
| 70 | + tmSet.Month = 10 * (cmd[9] - '0') + cmd[10] - '0'; |
| 71 | + tmSet.Day = 10 * (cmd[12] - '0') + cmd[13] - '0'; |
| 72 | + tmSet.Hour = 10 * (cmd[15] - '0') + cmd[16] - '0'; |
| 73 | + tmSet.Minute = 10 * (cmd[18] - '0') + cmd[19] - '0'; |
| 74 | + tmSet.Second = 10 * (cmd[21] - '0') + cmd[22] - '0'; |
| 75 | + tSet = makeTime(tmSet); //convert to time_t |
| 76 | + setTime(tSet); //set the system time |
| 77 | + RTC.set(now()); //set the rtc |
| 78 | + Serial.println("RTC set!"); |
| 79 | + flushInput(); //discard any extraneous trailing characters |
| 80 | + } |
| 81 | + else { |
| 82 | + Serial.print("Unknown: "); |
| 83 | + Serial.println(cmd); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +void flushInput(void) |
| 89 | +{ |
| 90 | + do { |
| 91 | + delay(2); |
| 92 | + Serial.read(); |
| 93 | + } while (Serial.available() > 0); |
| 94 | +} |
| 95 | + |
| 96 | +//Print time (and date) given a time_t value |
| 97 | +void printTime(time_t t) |
| 98 | +{ |
| 99 | + printI00(hour(t), ':'); |
| 100 | + printI00(minute(t), ':'); |
| 101 | + printI00(second(t), ' '); |
| 102 | + Serial.print(dayShortStr(weekday(t))); |
| 103 | + Serial.print(' '); |
| 104 | + printI00(day(t), ' '); |
| 105 | + Serial.print(monthShortStr(month(t))); |
| 106 | + Serial.print(' '); |
| 107 | + Serial.println(year(t)); |
| 108 | +} |
| 109 | + |
| 110 | +//Print an integer in "00" format (with leading zero), |
| 111 | +//followed by a delimiter. |
| 112 | +//Input value assumed to be between 0 and 99. |
| 113 | +void printI00(int val, char delim) |
| 114 | +{ |
| 115 | + if (val < 10) Serial.print('0'); |
| 116 | + Serial.print(val); |
| 117 | + Serial.print(delim); |
| 118 | + return; |
| 119 | +} |
| 120 | + |
0 commit comments