libraries / Bridge / examples / TimeCheck / TimeCheck.inoon commit Added link to project report (97a3ba0)
   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://www.arduino.cc/en/Tutorial/TimeCheck
  14
  15 */
  16
  17
  18#include <Process.h>
  19
  20Process date;                 // process used to get the date
  21int hours, minutes, seconds;  // for the results
  22int lastSecond = -1;          // need an impossible value for comparison
  23
  24void setup() {
  25  Bridge.begin();        // initialize Bridge
  26  SerialUSB.begin(9600);    // initialize serial
  27
  28  while (!Serial);              // wait for Serial Monitor to open
  29  SerialUSB.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
  40void loop() {
  41
  42  if (lastSecond != seconds) { // if a second has passed
  43    // print the time:
  44    if (hours <= 9) {
  45      SerialUSB.print("0");  // adjust for 0-9
  46    }
  47    SerialUSB.print(hours);
  48    SerialUSB.print(":");
  49    if (minutes <= 9) {
  50      SerialUSB.print("0");  // adjust for 0-9
  51    }
  52    SerialUSB.print(minutes);
  53    SerialUSB.print(":");
  54    if (seconds <= 9) {
  55      SerialUSB.print("0");  // adjust for 0-9
  56    }
  57    SerialUSB.println(seconds);
  58
  59    // restart the date process:
  60    if (!date.running()) {
  61      date.begin("date");
  62      date.addParameter("+%T");
  63      date.run();
  64    }
  65  }
  66
  67  //if there's a result from the date process, parse it:
  68  while (date.available() > 0) {
  69    // get the result of the date process (should be hh:mm:ss):
  70    String timeString = date.readString();
  71
  72    // find the colons:
  73    int firstColon = timeString.indexOf(":");
  74    int secondColon = timeString.lastIndexOf(":");
  75
  76    // get the substrings for hour, minute second:
  77    String hourString = timeString.substring(0, firstColon);
  78    String minString = timeString.substring(firstColon + 1, secondColon);
  79    String secString = timeString.substring(secondColon + 1);
  80
  81    // convert to ints,saving the previous second:
  82    hours = hourString.toInt();
  83    minutes = minString.toInt();
  84    lastSecond = seconds;          // save to do a time comparison
  85    seconds = secString.toInt();
  86  }
  87
  88}