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