3 * example code illustrating time synced from a GPS
8 #include <TinyGPS.h> // http://arduiniana.org/libraries/TinyGPS/
9 #include <SoftwareSerial.h>
10 // TinyGPS and SoftwareSerial libraries are the work of Mikal Hart
12 SoftwareSerial SerialGPS = SoftwareSerial(10, 11); // receive on pin 10
15 // To use a hardware serial port, which is far more efficient than
16 // SoftwareSerial, uncomment this line and remove SoftwareSerial
17 //#define SerialGPS Serial1
19 // Offset hours from gps time (UTC)
20 const int offset = 1; // Central European Time
21 //const int offset = -5; // Eastern Standard Time (USA)
22 //const int offset = -4; // Eastern Daylight Time (USA)
23 //const int offset = -8; // Pacific Standard Time (USA)
24 //const int offset = -7; // Pacific Daylight Time (USA)
26 // Ideally, it should be possible to learn the time zone
27 // based on the GPS position data. However, that would
28 // require a complex library, probably incorporating some
29 // sort of database using Eric Muller's time zone shape
30 // maps, at http://efele.net/maps/tz/
32 time_t prevDisplay = 0; // when the digital clock was displayed
37 while (!Serial) ; // Needed for Leonardo only
38 SerialGPS.begin(4800);
39 Serial.println("Waiting for GPS time ... ");
44 while (SerialGPS.available()) {
45 if (gps.encode(SerialGPS.read())) { // process gps messages
46 // when TinyGPS reports new data...
49 byte Month, Day, Hour, Minute, Second;
50 gps.crack_datetime(&Year, &Month, &Day, &Hour, &Minute, &Second, NULL, &age);
52 // set the Time to the latest GPS reading
53 setTime(Hour, Minute, Second, Day, Month, Year);
54 adjustTime(offset * SECS_PER_HOUR);
58 if (timeStatus()!= timeNotSet) {
59 if (now() != prevDisplay) { //update the display only if the time has changed
61 digitalClockDisplay();
66 void digitalClockDisplay(){
67 // digital clock display of the time
69 printDigits(minute());
70 printDigits(second());
74 Serial.print(month());
80 void printDigits(int digits) {
81 // utility function for digital clock display: prints preceding colon and leading 0