From 99cea65ce2f19486f29bffdb6e7f260f63e73d45 Mon Sep 17 00:00:00 2001 From: JChristensen Date: Fri, 14 Sep 2018 19:31:32 -0400 Subject: [PATCH] Improvements to example sketch. Removed unneeded variables and related code. --- examples/gsCurrentSensor/classes.h | 4 +- examples/gsCurrentSensor/gsCurrentSensor.ino | 83 ++++++++------------ 2 files changed, 36 insertions(+), 51 deletions(-) diff --git a/examples/gsCurrentSensor/classes.h b/examples/gsCurrentSensor/classes.h index 346baa9..6a04bf9 100644 --- a/examples/gsCurrentSensor/classes.h +++ b/examples/gsCurrentSensor/classes.h @@ -51,7 +51,7 @@ size_t OptionalLCD::write(uint8_t value) return 0; } -OptionalLCD lcd(0); //i2c address 0 (0x20) +OptionalLCD lcd(0); // i2c address 0 (0x20) class CurrentSensor : public CT_Control { @@ -98,7 +98,7 @@ void CurrentSensor::restart() float vcc = CT_Control::begin(); lcd.setCursor(0, 1); lcd << F("VCC " ) << _FLOAT(vcc, 3) << F(" V "); - Serial << millis() << F(" Vcc = ") << _FLOAT(vcc, 3) << endl; + Serial << millis() << F(" Vcc ") << _FLOAT(vcc, 3) << endl; } // read the ct and collect sample data, display on lcd diff --git a/examples/gsCurrentSensor/gsCurrentSensor.ino b/examples/gsCurrentSensor/gsCurrentSensor.ino index 3e965aa..2707dd7 100644 --- a/examples/gsCurrentSensor/gsCurrentSensor.ino +++ b/examples/gsCurrentSensor/gsCurrentSensor.ino @@ -36,46 +36,43 @@ gsXBee xb; // the XBee CurrentSensor cs(100, yelLED); // current transformer, 100mA threshold heartbeat hbLED(grnLED, 1000); -// time, time zone, etc. -uint32_t ms; // current time from millis() +// variables for time and timing time_t utc; // current utc time -time_t local; // current local time time_t utcStart; // sketch start time (actually the first time sync received) time_t lastTimeSyncRecd; // last time sync received +time_t nextWebTx; // time for next web data transmission +time_t nextTimeSync; // time for next time sync +time_t timeSyncRetry; // for time sync retries + // 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); TimeChangeRule *tcr; // pointer to the time change rule, use to get TZ abbrev -int utcH, utcM, utcS, utcDay, locH, locM, locS, locMon, locDay; // utc and local time parts // other global variables -time_t nextWebTx; // time for next web data transmission -time_t nextTimeSync; // time for next time sync -time_t timeSyncRetry; // for time sync retries XBeeAddress64 coordinator(0x0, 0x0); // coordinator address void setup() { Serial.begin(BAUD_RATE); Serial << F( "\n" __FILE__ " " __DATE__ " " __TIME__ "\n" ); - hbLED.begin(); - pinMode(redLED, OUTPUT); - pinMode(yelLED, OUTPUT); - pinMode(xbeeReset, OUTPUT); // drives pin low to reset the XBee - delay(10); // wait just a bit - digitalWrite(xbeeReset, HIGH); // enable pullups on unused pins for noise immunity for ( uint8_t i=0; i= XBEE_TIMEOUT) // timeout waiting for time sync response { @@ -139,23 +142,20 @@ void loop() break; case RUN: - utc = now(); - - // run the transmit state machine - xmit(xbStatus); + xmit(xbStatus); // run the transmit state machine // once-per-second processing static time_t lastUTC; // last time one-second processing code was executed if (utc > lastUTC) { lastUTC = utc; - updateTime(); cs.sample(); if (utc >= nextTimePrint) // print time to Serial once per minute { nextTimePrint += 60; - Serial << millis() << F(" Local: "); + time_t local = myTZ.toLocal(utc, &tcr); // TZ adjustment + Serial << millis() << ' '; printDateTime(local); cs.end(); // stop & restart the current sensor to re-read Vcc cs.restart(); @@ -266,29 +266,14 @@ void xmit(xbeeReadStatus_t xbStatus) } } -// update various time variables -void updateTime() -{ - utc = now(); - utcH = hour(utc); - utcM = minute(utc); - utcS = second(utc); - utcDay = day(utc); - local = myTZ.toLocal(utc, &tcr); // TZ adjustment - locH = hour(local); - locM = minute(local); - locS = second(local); - locMon = month(local); - locDay = day(local); -} - // set time to value received from master clock, update time variables void processTimeSync(time_t t) { setTime(t); + utc = now(); + if (lastTimeSyncRecd == 0) nextTimeSync = utc; + while (nextTimeSync <= utc) nextTimeSync += SYNC_INTERVAL; lastTimeSyncRecd = t; - updateTime(); - while ( nextTimeSync <= utc ) nextTimeSync += SYNC_INTERVAL; timeSyncRetry = 0; digitalWrite(redLED, LOW); }