Skip to content

Commit

Permalink
Update example sketches:
Browse files Browse the repository at this point in the history
Fix compiler warnings,
Button library name change,
Shorter code for function to print date and time.
  • Loading branch information
JChristensen committed May 15, 2018
1 parent 3713faf commit dec9c14
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 172 deletions.
11 changes: 5 additions & 6 deletions examples/Change_TZ_1/Change_TZ_1.ino
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@
// Jack Christensen 02Jan2018

#include <avr/eeprom.h>
#include <Button.h> // http://github.com/JChristensen/Button
#include <JC_Button.h> // http://github.com/JChristensen/JC_Button
#include <Streaming.h> // http://arduiniana.org/libraries/streaming/
#include <Timezone.h> // 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
Expand Down Expand Up @@ -46,6 +44,7 @@ void setup()
eeprom_write_byte( &ee_tzIndex, tzIndex);
}

btn.begin();
Serial.begin(115200);
changeTZ();
}
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 5 additions & 6 deletions examples/Change_TZ_2/Change_TZ_2.ino
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@
//
// Jack Christensen 02Jan2018

#include <Button.h> // http://github.com/JChristensen/Button
#include <JC_Button.h> // http://github.com/JChristensen/JC_Button
#include <Streaming.h> // http://arduiniana.org/libraries/streaming/
#include <Timezone.h> // 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
Expand All @@ -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];
}
Expand Down Expand Up @@ -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);
Expand Down
85 changes: 28 additions & 57 deletions examples/Clock/Clock.ino
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,43 @@
// TimeChangeRules can be hard-coded or read from EEPROM, see comments.
// Jack Christensen Mar 2012

#include <Timezone.h> //https://github.com/JChristensen/Timezone
#include <Timezone.h> // 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';
Expand All @@ -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);
}
56 changes: 15 additions & 41 deletions examples/HardwareRTC/HardwareRTC.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
87 changes: 29 additions & 58 deletions examples/WorldClock/WorldClock.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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 <Timezone.h> // https://github.com/JChristensen/Timezone

Expand Down Expand Up @@ -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';
Expand All @@ -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);
}
Loading

0 comments on commit dec9c14

Please sign in to comment.