2013-05-27 18:54:44 +02:00
|
|
|
/*
|
|
|
|
Time Check
|
|
|
|
|
|
|
|
Gets the time from the linino processor via Bridge
|
|
|
|
then parses out hours, minutes and seconds for the Arduino
|
2013-07-01 20:11:14 +02:00
|
|
|
using an Arduino Yún.
|
2013-05-27 18:54:44 +02:00
|
|
|
|
|
|
|
created 27 May 2013
|
2013-06-22 08:03:47 +02:00
|
|
|
modified 21 June 2013
|
2013-05-27 18:54:44 +02:00
|
|
|
By Tom Igoe
|
2013-07-01 20:11:14 +02:00
|
|
|
|
|
|
|
This example code is in the public domain.
|
2013-09-09 20:56:15 +02:00
|
|
|
|
|
|
|
http://arduino.cc/en/Tutorial/TimeCheck
|
2013-07-01 20:11:14 +02:00
|
|
|
|
2013-05-27 18:54:44 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#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() {
|
|
|
|
Bridge.begin(); // initialize Bridge
|
2013-07-01 20:11:14 +02:00
|
|
|
Serial.begin(9600); // initialize serial
|
2013-06-22 08:03:47 +02:00
|
|
|
|
2013-07-01 20:11:14 +02:00
|
|
|
while(!Serial); // wait for Serial Monitor to open
|
2013-05-27 18:54:44 +02:00
|
|
|
Serial.println("Time Check"); // Title of sketch
|
|
|
|
|
|
|
|
// run an initial date process. Should return:
|
|
|
|
// hh:mm:ss :
|
|
|
|
if (!date.running()) {
|
2013-06-22 08:03:47 +02:00
|
|
|
date.begin("date");
|
|
|
|
date.addParameter("+%T");
|
|
|
|
date.run();
|
2013-05-27 18:54:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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()) {
|
2013-06-22 08:03:47 +02:00
|
|
|
date.begin("date");
|
|
|
|
date.addParameter("+%T");
|
|
|
|
date.run();
|
2013-05-27 18:54:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//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();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|