From dec9c14a24bbbd92a9eed8b0337775e77d4ca834 Mon Sep 17 00:00:00 2001 From: JChristensen Date: Tue, 15 May 2018 18:04:01 -0400 Subject: [PATCH] Update example sketches: Fix compiler warnings, Button library name change, Shorter code for function to print date and time. --- examples/Change_TZ_1/Change_TZ_1.ino | 11 ++-- examples/Change_TZ_2/Change_TZ_2.ino | 11 ++-- examples/Clock/Clock.ino | 85 +++++++++------------------ examples/HardwareRTC/HardwareRTC.ino | 56 +++++------------- examples/WorldClock/WorldClock.ino | 87 ++++++++++------------------ examples/WriteRules/WriteRules.ino | 6 +- library.properties | 2 +- 7 files changed, 86 insertions(+), 172 deletions(-) diff --git a/examples/Change_TZ_1/Change_TZ_1.ino b/examples/Change_TZ_1/Change_TZ_1.ino index 3dbd6f9..7eb8913 100644 --- a/examples/Change_TZ_1/Change_TZ_1.ino +++ b/examples/Change_TZ_1/Change_TZ_1.ino @@ -11,14 +11,12 @@ // Jack Christensen 02Jan2018 #include -#include // http://github.com/JChristensen/Button +#include // http://github.com/JChristensen/JC_Button #include // http://arduiniana.org/libraries/streaming/ #include // http://github.com/JChristensen/Timezone -const uint8_t BUTTON_PIN(9); // connect a button from this pin to ground -const bool PULLUP(true), INVERT(true); -const uint32_t DEBOUNCE_MS(25); -Button btn(BUTTON_PIN, PULLUP, INVERT, DEBOUNCE_MS); +const uint8_t BUTTON_PIN(8); // connect a button from this pin to ground +Button btn(BUTTON_PIN); uint8_t tzIndex; //index to the arrays below EEMEM uint8_t ee_tzIndex; //copy of tzIndex persisted in EEPROM @@ -46,6 +44,7 @@ void setup() eeprom_write_byte( &ee_tzIndex, tzIndex); } + btn.begin(); Serial.begin(115200); changeTZ(); } @@ -99,7 +98,7 @@ void printDateTime(time_t t) time_t compileTime() { const time_t FUDGE(10); //fudge factor to allow for upload time, etc. (seconds, YMMV) - char *compDate = __DATE__, *compTime = __TIME__, *months = "JanFebMarAprMayJunJulAugSepOctNovDec"; + const char *compDate = __DATE__, *compTime = __TIME__, *months = "JanFebMarAprMayJunJulAugSepOctNovDec"; char compMon[3], *m; strncpy(compMon, compDate, 3); diff --git a/examples/Change_TZ_2/Change_TZ_2.ino b/examples/Change_TZ_2/Change_TZ_2.ino index 63122f5..9e3d175 100644 --- a/examples/Change_TZ_2/Change_TZ_2.ino +++ b/examples/Change_TZ_2/Change_TZ_2.ino @@ -8,14 +8,12 @@ // // Jack Christensen 02Jan2018 -#include // http://github.com/JChristensen/Button +#include // http://github.com/JChristensen/JC_Button #include // http://arduiniana.org/libraries/streaming/ #include // http://github.com/JChristensen/Timezone -const uint8_t BUTTON_PIN(9); // connect a button from this pin to ground -const bool PULLUP(true), INVERT(true); -const uint32_t DEBOUNCE_MS(25); -Button btn(BUTTON_PIN, PULLUP, INVERT, DEBOUNCE_MS); +const uint8_t BUTTON_PIN(8); // connect a button from this pin to ground +Button btn(BUTTON_PIN); //Continental US Time Zones TimeChangeRule EDT = { "EDT", Second, Sun, Mar, 2, -240 }; //Daylight time = UTC - 4 hours @@ -42,6 +40,7 @@ void setup() // adjust the following line accordingly if you're in another time zone setTime(compileTime() + 300 * 60); + btn.begin(); Serial.begin(115200); tz = timezones[tzIndex]; } @@ -85,7 +84,7 @@ void printDateTime(time_t t) time_t compileTime() { const time_t FUDGE(10); //fudge factor to allow for upload time, etc. (seconds, YMMV) - char *compDate = __DATE__, *compTime = __TIME__, *months = "JanFebMarAprMayJunJulAugSepOctNovDec"; + const char *compDate = __DATE__, *compTime = __TIME__, *months = "JanFebMarAprMayJunJulAugSepOctNovDec"; char compMon[3], *m; strncpy(compMon, compDate, 3); diff --git a/examples/Clock/Clock.ino b/examples/Clock/Clock.ino index 741e9e3..5dc28a8 100644 --- a/examples/Clock/Clock.ino +++ b/examples/Clock/Clock.ino @@ -6,47 +6,43 @@ // TimeChangeRules can be hard-coded or read from EEPROM, see comments. // Jack Christensen Mar 2012 -#include //https://github.com/JChristensen/Timezone +#include // https://github.com/JChristensen/Timezone -//US Eastern Time Zone (New York, Detroit) -TimeChangeRule myDST = {"EDT", Second, Sun, Mar, 2, -240}; //Daylight time = UTC - 4 hours -TimeChangeRule mySTD = {"EST", First, Sun, Nov, 2, -300}; //Standard time = UTC - 5 hours +// US Eastern Time Zone (New York, Detroit) +TimeChangeRule myDST = {"EDT", Second, Sun, Mar, 2, -240}; // Daylight time = UTC - 4 hours +TimeChangeRule mySTD = {"EST", First, Sun, Nov, 2, -300}; // Standard time = UTC - 5 hours Timezone myTZ(myDST, mySTD); -//If TimeChangeRules are already stored in EEPROM, comment out the three -//lines above and uncomment the line below. -//Timezone myTZ(100); //assumes rules stored at EEPROM address 100 +// If TimeChangeRules are already stored in EEPROM, comment out the three +// lines above and uncomment the line below. +//Timezone myTZ(100); // assumes rules stored at EEPROM address 100 -TimeChangeRule *tcr; //pointer to the time change rule, use to get TZ abbrev -time_t utc, local; +TimeChangeRule *tcr; // pointer to the time change rule, use to get TZ abbrev -void setup(void) +void setup() { Serial.begin(115200); setTime(myTZ.toUTC(compileTime())); //setTime(01, 55, 00, 11, 3, 2012); //another way to set the time (hr,min,sec,day,mnth,yr) } -void loop(void) +void loop() { + time_t utc = now(); + time_t local = myTZ.toLocal(utc, &tcr); Serial.println(); - utc = now(); - printTime(utc, "UTC"); - local = myTZ.toLocal(utc, &tcr); - printTime(local, tcr -> abbrev); + printDateTime(utc, "UTC"); + printDateTime(local, tcr -> abbrev); delay(10000); } -//Function to return the compile date and time as a time_t value -time_t compileTime(void) +// Function to return the compile date and time as a time_t value +time_t compileTime() { -#define FUDGE 25 //fudge factor to allow for compile time (seconds, YMMV) - - char *compDate = __DATE__, *compTime = __TIME__, *months = "JanFebMarAprMayJunJulAugSepOctNovDec"; + const time_t FUDGE(10); // fudge factor to allow for compile time (seconds, YMMV) + const char *compDate = __DATE__, *compTime = __TIME__, *months = "JanFebMarAprMayJunJulAugSepOctNovDec"; char chMon[3], *m; - int d, y; tmElements_t tm; - time_t t; strncpy(chMon, compDate, 3); chMon[3] = '\0'; @@ -58,43 +54,18 @@ time_t compileTime(void) tm.Hour = atoi(compTime); tm.Minute = atoi(compTime + 3); tm.Second = atoi(compTime + 6); - t = makeTime(tm); - return t + FUDGE; //add fudge factor to allow for compile time + time_t t = makeTime(tm); + return t + FUDGE; // add fudge factor to allow for compile time } -//Function to print time with time zone -void printTime(time_t t, char *tz) +// format and print a time_t value, with a time zone appended. +void printDateTime(time_t t, const char *tz) { - sPrintI00(hour(t)); - sPrintDigits(minute(t)); - sPrintDigits(second(t)); - Serial.print(' '); - Serial.print(dayShortStr(weekday(t))); - Serial.print(' '); - sPrintI00(day(t)); - Serial.print(' '); - Serial.print(monthShortStr(month(t))); - Serial.print(' '); - Serial.print(year(t)); - Serial.print(' '); - Serial.print(tz); - Serial.println(); + char buf[32]; + char m[4]; // temporary storage for month string (DateStrings.cpp uses shared buffer) + strcpy(m, monthShortStr(month(t))); + sprintf(buf, "%.2d:%.2d:%.2d %s %.2d %s %d %s", + hour(t), minute(t), second(t), dayShortStr(weekday(t)), day(t), m, year(t), tz); + Serial.println(buf); } -//Print an integer in "00" format (with leading zero). -//Input value assumed to be between 0 and 99. -void sPrintI00(int val) -{ - if (val < 10) Serial.print('0'); - Serial.print(val, DEC); - return; -} - -//Print an integer in ":00" format (with leading zero). -//Input value assumed to be between 0 and 99. -void sPrintDigits(int val) -{ - Serial.print(':'); - if(val < 10) Serial.print('0'); - Serial.print(val, DEC); -} diff --git a/examples/HardwareRTC/HardwareRTC.ino b/examples/HardwareRTC/HardwareRTC.ino index 129a6e2..87c5df7 100644 --- a/examples/HardwareRTC/HardwareRTC.ino +++ b/examples/HardwareRTC/HardwareRTC.ino @@ -20,12 +20,11 @@ Timezone myTZ(myDST, mySTD); // If TimeChangeRules are already stored in EEPROM, comment out the three // lines above and uncomment the line below. -// Timezone myTZ(100); //assumes rules stored at EEPROM address 100 +//Timezone myTZ(100); //assumes rules stored at EEPROM address 100 TimeChangeRule *tcr; //pointer to the time change rule, use to get TZ abbrev -time_t utc, local; -void setup(void) +void setup() { Serial.begin(115200); setSyncProvider(RTC.get); // the function to get the time from the RTC @@ -35,49 +34,24 @@ void setup(void) Serial.println("RTC has set the system time"); } -void loop(void) +void loop() { + time_t utc = now(); + time_t local = myTZ.toLocal(utc, &tcr); Serial.println(); - utc = now(); - printTime(utc, "UTC"); - local = myTZ.toLocal(utc, &tcr); - printTime(local, tcr -> abbrev); + printDateTime(utc, "UTC"); + printDateTime(local, tcr -> abbrev); delay(10000); } -// Function to print time with time zone -void printTime(time_t t, char *tz) +// format and print a time_t value, with a time zone appended. +void printDateTime(time_t t, const char *tz) { - sPrintI00(hour(t)); - sPrintDigits(minute(t)); - sPrintDigits(second(t)); - Serial.print(' '); - Serial.print(dayShortStr(weekday(t))); - Serial.print(' '); - sPrintI00(day(t)); - Serial.print(' '); - Serial.print(monthShortStr(month(t))); - Serial.print(' '); - Serial.print(year(t)); - Serial.print(' '); - Serial.print(tz); - Serial.println(); -} - -// Print an integer in "00" format (with leading zero). -// Input value assumed to be between 0 and 99. -void sPrintI00(int val) -{ - if (val < 10) Serial.print('0'); - Serial.print(val, DEC); - return; + char buf[32]; + char m[4]; // temporary storage for month string (DateStrings.cpp uses shared buffer) + strcpy(m, monthShortStr(month(t))); + sprintf(buf, "%.2d:%.2d:%.2d %s %.2d %s %d %s", + hour(t), minute(t), second(t), dayShortStr(weekday(t)), day(t), m, year(t), tz); + Serial.println(buf); } -// Print an integer in ":00" format (with leading zero). -// Input value assumed to be between 0 and 99. -void sPrintDigits(int val) -{ - Serial.print(':'); - if(val < 10) Serial.print('0'); - Serial.print(val, DEC); -} diff --git a/examples/WorldClock/WorldClock.ino b/examples/WorldClock/WorldClock.ino index 2a9610b..55a5544 100644 --- a/examples/WorldClock/WorldClock.ino +++ b/examples/WorldClock/WorldClock.ino @@ -5,9 +5,8 @@ // Self-adjusting clock for multiple time zones. // Jack Christensen Mar 2012 // -// Sources for DST rule information: +// For time zone information: // http://www.timeanddate.com/worldclock/ -// http://home.tiscali.nl/~t876506/TZworld.html #include // https://github.com/JChristensen/Timezone @@ -53,39 +52,35 @@ TimeChangeRule usPDT = {"PDT", Second, dowSunday, Mar, 2, -420}; TimeChangeRule usPST = {"PST", First, dowSunday, Nov, 2, -480}; Timezone usPT(usPDT, usPST); -time_t utc; - -void setup(void) +void setup() { Serial.begin(115200); setTime(usET.toUTC(compileTime())); } -void loop(void) +void loop() { + time_t utc = now(); Serial.println(); - utc = now(); - printTime(ausET, utc, "Sydney"); - printTime(CE, utc, "Paris"); - printTime(UK, utc, " London"); - printTime(UTC, utc, " Universal Coordinated Time"); - printTime(usET, utc, " New York"); - printTime(usCT, utc, " Chicago"); - printTime(usMT, utc, " Denver"); - printTime(usAZ, utc, " Phoenix"); - printTime(usPT, utc, " Los Angeles"); + printDateTime(ausET, utc, "Sydney"); + printDateTime(CE, utc, "Paris"); + printDateTime(UK, utc, " London"); + printDateTime(UTC, utc, " Universal Coordinated Time"); + printDateTime(usET, utc, " New York"); + printDateTime(usCT, utc, " Chicago"); + printDateTime(usMT, utc, " Denver"); + printDateTime(usAZ, utc, " Phoenix"); + printDateTime(usPT, utc, " Los Angeles"); delay(10000); } // Function to return the compile date and time as a time_t value -time_t compileTime(void) +time_t compileTime() { - const time_t FUDGE(25); // fudge factor to allow for compile time (seconds, YMMV) - char *compDate = __DATE__, *compTime = __TIME__, *months = "JanFebMarAprMayJunJulAugSepOctNovDec"; - char chMon[4], *m; - int d, y; + const time_t FUDGE(10); // fudge factor to allow for compile time (seconds, YMMV) + const char *compDate = __DATE__, *compTime = __TIME__, *months = "JanFebMarAprMayJunJulAugSepOctNovDec"; + char chMon[3], *m; tmElements_t tm; - time_t t; strncpy(chMon, compDate, 3); chMon[3] = '\0'; @@ -97,47 +92,23 @@ time_t compileTime(void) tm.Hour = atoi(compTime); tm.Minute = atoi(compTime + 3); tm.Second = atoi(compTime + 6); - t = makeTime(tm); - return t + FUDGE; // add fudge factor to allow for compile time + time_t t = makeTime(tm); + return t + FUDGE; // add fudge factor to allow for compile time } -// Function to print time with time zone -void printTime(Timezone tz, time_t utc, char *loc) +// given a Timezone object, UTC and a string description, convert and print local time with time zone +void printDateTime(Timezone tz, time_t utc, const char *descr) { + char buf[40]; + char m[4]; // temporary storage for month string (DateStrings.cpp uses shared buffer) TimeChangeRule *tcr; // pointer to the time change rule, use to get the TZ abbrev + time_t t = tz.toLocal(utc, &tcr); - sPrintI00(hour(t)); - sPrintDigits(minute(t)); - sPrintDigits(second(t)); - Serial.print(' '); - Serial.print(dayShortStr(weekday(t))); - Serial.print(' '); - sPrintI00(day(t)); - Serial.print(' '); - Serial.print(monthShortStr(month(t))); - Serial.print(' '); - Serial.print(year(t)); - Serial.print(' '); - Serial.print(tcr -> abbrev); + strcpy(m, monthShortStr(month(t))); + sprintf(buf, "%.2d:%.2d:%.2d %s %.2d %s %d %s", + hour(t), minute(t), second(t), dayShortStr(weekday(t)), day(t), m, year(t), tcr -> abbrev); + Serial.print(buf); Serial.print(' '); - Serial.print(loc); - Serial.println(); -} - -// Print an integer in "00" format (with leading zero). -// Input value assumed to be between 0 and 99. -void sPrintI00(int val) -{ - if (val < 10) Serial.print('0'); - Serial.print(val, DEC); - return; + Serial.println(descr); } -// Print an integer in ":00" format (with leading zero). -// Input value assumed to be between 0 and 99. -void sPrintDigits(int val) -{ - Serial.print(':'); - if(val < 10) Serial.print('0'); - Serial.print(val, DEC); -} diff --git a/examples/WriteRules/WriteRules.ino b/examples/WriteRules/WriteRules.ino index 713b43e..377cf68 100644 --- a/examples/WriteRules/WriteRules.ino +++ b/examples/WriteRules/WriteRules.ino @@ -5,20 +5,20 @@ // Write TimeChangeRules to EEPROM. // Jack Christensen Mar 2012 -#include // https://github.com/JChristensen/Timezone +#include // https://github.com/JChristensen/Timezone // US Eastern Time Zone (New York, Detroit) TimeChangeRule usEdt = {"EDT", Second, Sun, Mar, 2, -240}; // UTC - 4 hours TimeChangeRule usEst = {"EST", First, Sun, Nov, 2, -300}; // UTC - 5 hours Timezone usEastern(usEdt, usEst); -void setup(void) +void setup() { pinMode(13, OUTPUT); usEastern.writeRules(100); // write rules to EEPROM address 100 } -void loop(void) +void loop() { // fast blink to indicate EEPROM write is complete digitalWrite(13, HIGH); diff --git a/library.properties b/library.properties index 3b732cc..614ea0e 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Timezone -version=1.1.2 +version=1.1.3 author=Jack Christensen maintainer=Jack Christensen sentence=Arduino library to facilitate time zone conversions and automatic daylight saving (summer) time adjustments.