Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
21 | jlesech | 1 | // Date and time functions using a DS1307 RTC connected via I2C and Wire lib |
2 | |||
3 | #include <Wire.h> |
||
4 | #include "RTClib.h" |
||
5 | |||
6 | RTC_DS1307 RTC; |
||
7 | |||
8 | void setup () { |
||
9 | Serial.begin(57600); |
||
10 | Wire.begin(); |
||
11 | RTC.begin(); |
||
12 | |||
13 | if (! RTC.isrunning()) { |
||
14 | Serial.println("RTC is NOT running!"); |
||
15 | // following line sets the RTC to the date & time this sketch was compiled |
||
16 | RTC.adjust(DateTime(__DATE__, __TIME__)); |
||
17 | } |
||
18 | } |
||
19 | |||
20 | void loop () { |
||
21 | DateTime now = RTC.now(); |
||
22 | |||
23 | Serial.print(now.year(), DEC); |
||
24 | Serial.print('/'); |
||
25 | Serial.print(now.month(), DEC); |
||
26 | Serial.print('/'); |
||
27 | Serial.print(now.day(), DEC); |
||
28 | Serial.print(' '); |
||
29 | Serial.print(now.hour(), DEC); |
||
30 | Serial.print(':'); |
||
31 | Serial.print(now.minute(), DEC); |
||
32 | Serial.print(':'); |
||
33 | Serial.print(now.second(), DEC); |
||
34 | Serial.println(); |
||
35 | |||
36 | Serial.print(" since midnight 1/1/1970 = "); |
||
37 | Serial.print(now.unixtime()); |
||
38 | Serial.print("s = "); |
||
39 | Serial.print(now.unixtime() / 86400L); |
||
40 | Serial.println("d"); |
||
41 | |||
42 | // calculate a date which is 7 days and 30 seconds into the future |
||
43 | DateTime future (now.unixtime() + 7 * 86400L + 30); |
||
44 | |||
45 | Serial.print(" now + 7d + 30s: "); |
||
46 | Serial.print(future.year(), DEC); |
||
47 | Serial.print('/'); |
||
48 | Serial.print(future.month(), DEC); |
||
49 | Serial.print('/'); |
||
50 | Serial.print(future.day(), DEC); |
||
51 | Serial.print(' '); |
||
52 | Serial.print(future.hour(), DEC); |
||
53 | Serial.print(':'); |
||
54 | Serial.print(future.minute(), DEC); |
||
55 | Serial.print(':'); |
||
56 | Serial.print(future.second(), DEC); |
||
57 | Serial.println(); |
||
58 | |||
59 | Serial.println(); |
||
60 | delay(3000); |
||
61 | } |