diff options
author | Tom Igoe <tom.igoe@gmail.com> | 2013-05-27 12:54:44 -0400 |
---|---|---|
committer | Tom Igoe <tom.igoe@gmail.com> | 2013-05-27 12:54:44 -0400 |
commit | 952d2b41ad1d0e553910be652f9f5d3b2b1e68f2 (patch) | |
tree | 9b204df0b8c8821f18d34a1d374245adf4c2e3a4 /libraries | |
parent | 2427e3398a5686e275b7a98f78de5437cec7b862 (diff) |
Added TimeCheck example to demonstrate re-use of Process
Diffstat (limited to 'libraries')
-rw-r--r-- | libraries/Bridge/examples/TimeCheck/TimeCheck.ino | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/libraries/Bridge/examples/TimeCheck/TimeCheck.ino b/libraries/Bridge/examples/TimeCheck/TimeCheck.ino new file mode 100644 index 0000000..69a8544 --- /dev/null +++ b/libraries/Bridge/examples/TimeCheck/TimeCheck.ino @@ -0,0 +1,96 @@ + +/* + Time Check + + Gets the time from the linino processor via Bridge + then parses out hours, minutes and seconds for the Arduino + using an Arduino Yun. + + created 27 May 2013 + By Tom Igoe + */ + + +#include <Process.h> + +Process date; // process used to get the date +int hours, minutes, seconds; // for the results +int lastSecond = -1; // need an impossible value for comparison + +void setup() { + Serial.begin(9600); // initialize serial + Bridge.begin(); // initialize Bridge + delay(2000); // wait 2 seconds + + while(!Serial); // wait for Serial Monitor to open + Serial.println("Time Check"); // Title of sketch + + // run an initial date process. Should return: + // hh:mm:ss : + if (!date.running()) { + date.begin("date"); + date.addParameter("+%T"); + date.run(); + } +} + +void loop() { + + if(lastSecond != seconds) { // if a second has passed + // print the time: + if (hours <= 9) Serial.print("0"); // adjust for 0-9 + Serial.print(hours); + Serial.print(":"); + if (minutes <= 9) Serial.print("0"); // adjust for 0-9 + Serial.print(minutes); + Serial.print(":"); + if (seconds <= 9) Serial.print("0"); // adjust for 0-9 + Serial.println(seconds); + + // restart the date process: + if (!date.running()) { + date.begin("date"); + date.addParameter("+%T"); + date.run(); + } + } + + //if there's a result from the date process, parse it: + while (date.available()>0) { + // get the result of the date process (should be hh:mm:ss): + String timeString = date.readString(); + + // find the colons: + int firstColon = timeString.indexOf(":"); + int secondColon= timeString.lastIndexOf(":"); + + // get the substrings for hour, minute second: + String hourString = timeString.substring(0, firstColon); + String minString = timeString.substring(firstColon+1, secondColon); + String secString = timeString.substring(secondColon+1); + + // convert to ints,saving the previous second: + hours = hourString.toInt(); + minutes = minString.toInt(); + lastSecond = seconds; // save to do a time comparison + seconds = secString.toInt(); + + /* + NOTE: If the parseInt() and parseFloar() methods from Stream + were included with Process, this would be simpler: + + hours = date.parseInt(); + minutes = date.parseInt(); + seconds = date.parseInt(); + + */ + } + +} + + + + + + + |