1 | /*
|
---|
2 | Time Check
|
---|
3 |
|
---|
4 | Gets the time from Linux via Bridge then parses out hours,
|
---|
5 | minutes and seconds for the Arduino using an Arduino Y炭n.
|
---|
6 |
|
---|
7 | created 27 May 2013
|
---|
8 | modified 21 June 2013
|
---|
9 | By Tom Igoe
|
---|
10 |
|
---|
11 | This example code is in the public domain.
|
---|
12 |
|
---|
13 | http://arduino.cc/en/Tutorial/TimeCheck
|
---|
14 |
|
---|
15 | */
|
---|
16 |
|
---|
17 |
|
---|
18 | #include <Process.h>
|
---|
19 |
|
---|
20 | Process date; // process used to get the date
|
---|
21 | int hours, minutes, seconds; // for the results
|
---|
22 | int lastSecond = -1; // need an impossible value for comparison
|
---|
23 |
|
---|
24 | void setup() {
|
---|
25 | Bridge.begin(); // initialize Bridge
|
---|
26 | Serial.begin(9600); // initialize serial
|
---|
27 |
|
---|
28 | while (!Serial); // wait for Serial Monitor to open
|
---|
29 | Serial.println("Time Check"); // Title of sketch
|
---|
30 |
|
---|
31 | // run an initial date process. Should return:
|
---|
32 | // hh:mm:ss :
|
---|
33 | if (!date.running()) {
|
---|
34 | date.begin("date");
|
---|
35 | date.addParameter("+%T");
|
---|
36 | date.run();
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | void loop() {
|
---|
41 |
|
---|
42 | if (lastSecond != seconds) { // if a second has passed
|
---|
43 | // print the time:
|
---|
44 | if (hours <= 9) Serial.print("0"); // adjust for 0-9
|
---|
45 | Serial.print(hours);
|
---|
46 | Serial.print(":");
|
---|
47 | if (minutes <= 9) Serial.print("0"); // adjust for 0-9
|
---|
48 | Serial.print(minutes);
|
---|
49 | Serial.print(":");
|
---|
50 | if (seconds <= 9) Serial.print("0"); // adjust for 0-9
|
---|
51 | Serial.println(seconds);
|
---|
52 |
|
---|
53 | // restart the date process:
|
---|
54 | if (!date.running()) {
|
---|
55 | date.begin("date");
|
---|
56 | date.addParameter("+%T");
|
---|
57 | date.run();
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | //if there's a result from the date process, parse it:
|
---|
62 | while (date.available() > 0) {
|
---|
63 | // get the result of the date process (should be hh:mm:ss):
|
---|
64 | String timeString = date.readString();
|
---|
65 |
|
---|
66 | // find the colons:
|
---|
67 | int firstColon = timeString.indexOf(":");
|
---|
68 | int secondColon = timeString.lastIndexOf(":");
|
---|
69 |
|
---|
70 | // get the substrings for hour, minute second:
|
---|
71 | String hourString = timeString.substring(0, firstColon);
|
---|
72 | String minString = timeString.substring(firstColon + 1, secondColon);
|
---|
73 | String secString = timeString.substring(secondColon + 1);
|
---|
74 |
|
---|
75 | // convert to ints,saving the previous second:
|
---|
76 | hours = hourString.toInt();
|
---|
77 | minutes = minString.toInt();
|
---|
78 | lastSecond = seconds; // save to do a time comparison
|
---|
79 | seconds = secString.toInt();
|
---|
80 | }
|
---|
81 |
|
---|
82 | }
|
---|