forked from PaulStoffregen/Time
Fix compiler error and generally clean up TimeRTCLog example
This commit is contained in:
@ -1,24 +1,24 @@
|
|||||||
/*
|
/*
|
||||||
* TimeRTCLogger.pde
|
* TimeRTCLogger.ino
|
||||||
* example code illustrating adding and subtracting Time.
|
* example code illustrating adding and subtracting Time.
|
||||||
*
|
*
|
||||||
* this sketch logs pin state change events
|
* this sketch logs pin state change events
|
||||||
* the time of the event and time since the previous event is calculated and sent to the serial port.
|
* the time of the event and time since the previous event is calculated and sent to the serial port.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <TimeLib.h>
|
#include <TimeLib.h>
|
||||||
#include <Wire.h>
|
#include <Wire.h>
|
||||||
#include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t
|
#include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t
|
||||||
|
|
||||||
const int nbrInputPins = 6; // monitor 6 digital pins
|
const int nbrInputPins = 6; // monitor 6 digital pins
|
||||||
const int inputPins[nbrInputPins] = {2,3,4,5,6,7}; // pins to monitor
|
const int inputPins[nbrInputPins] = {2,3,4,5,6,7}; // pins to monitor
|
||||||
boolean state[nbrInputPins] ; // the state of the monitored pins
|
boolean state[nbrInputPins] ; // the state of the monitored pins
|
||||||
time_t prevEventTime[nbrInputPins] ; // the time of the previous event
|
time_t prevEventTime[nbrInputPins] ; // the time of the previous event
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
setSyncProvider(RTC.get); // the function to sync the time from the RTC
|
setSyncProvider(RTC.get); // the function to sync the time from the RTC
|
||||||
for(int i=0; i < nbrInputPins; i++){
|
for (int i=0; i < nbrInputPins; i++) {
|
||||||
pinMode( inputPins[i], INPUT);
|
pinMode( inputPins[i], INPUT);
|
||||||
// uncomment these lines if pull-up resistors are wanted
|
// uncomment these lines if pull-up resistors are wanted
|
||||||
// pinMode( inputPins[i], INPUT_PULLUP);
|
// pinMode( inputPins[i], INPUT_PULLUP);
|
||||||
@ -28,19 +28,18 @@ void setup() {
|
|||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
for(int i=0; i < nbrInputPins; i++)
|
for (int i=0; i < nbrInputPins; i++) {
|
||||||
{
|
boolean val = digitalRead(inputPins[i]);
|
||||||
boolean val = digitalRead(inputPins[i]);
|
if (val != state[i]) {
|
||||||
if(val != state[i])
|
|
||||||
{
|
|
||||||
time_t duration = 0; // the time since the previous event
|
time_t duration = 0; // the time since the previous event
|
||||||
state[i] = val;
|
state[i] = val;
|
||||||
time_t timeNow = now();
|
time_t timeNow = now();
|
||||||
if(prevEventTime[i] > 0)
|
if (prevEventTime[i] > 0) {
|
||||||
// if this was not the first state change, calculate the time from the previous change
|
// if this was not the first state change, calculate the time from the previous change
|
||||||
duration = duration = timeNow - prevEventTime[i];
|
duration = timeNow - prevEventTime[i];
|
||||||
|
}
|
||||||
logEvent(inputPins[i], val, timeNow, duration ); // log the event
|
logEvent(inputPins[i], val, timeNow, duration ); // log the event
|
||||||
prevEventTime[i] = timeNow; // store the time for this event
|
prevEventTime[i] = timeNow; // store the time for this event
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -49,13 +48,14 @@ void logEvent( int pin, boolean state, time_t timeNow, time_t duration)
|
|||||||
{
|
{
|
||||||
Serial.print("Pin ");
|
Serial.print("Pin ");
|
||||||
Serial.print(pin);
|
Serial.print(pin);
|
||||||
if( state == HIGH)
|
if (state == HIGH) {
|
||||||
Serial.print(" went High at ");
|
Serial.print(" went High at ");
|
||||||
else
|
} else {
|
||||||
Serial.print(" went Low at ");
|
Serial.print(" went Low at ");
|
||||||
showTime(timeNow);
|
}
|
||||||
if(duration > 0){
|
showTime(timeNow);
|
||||||
// only display duration if greater than 0
|
if (duration > 0) {
|
||||||
|
// only display duration if greater than 0
|
||||||
Serial.print(", Duration was ");
|
Serial.print(", Duration was ");
|
||||||
showDuration(duration);
|
showDuration(duration);
|
||||||
}
|
}
|
||||||
@ -63,8 +63,9 @@ void logEvent( int pin, boolean state, time_t timeNow, time_t duration)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void showTime(time_t t){
|
void showTime(time_t t)
|
||||||
// display the given time
|
{
|
||||||
|
// display the given time
|
||||||
Serial.print(hour(t));
|
Serial.print(hour(t));
|
||||||
printDigits(minute(t));
|
printDigits(minute(t));
|
||||||
printDigits(second(t));
|
printDigits(second(t));
|
||||||
@ -73,7 +74,7 @@ void showTime(time_t t){
|
|||||||
Serial.print(" ");
|
Serial.print(" ");
|
||||||
Serial.print(month(t));
|
Serial.print(month(t));
|
||||||
Serial.print(" ");
|
Serial.print(" ");
|
||||||
Serial.print(year(t));
|
Serial.print(year(t));
|
||||||
}
|
}
|
||||||
|
|
||||||
void printDigits(int digits){
|
void printDigits(int digits){
|
||||||
@ -84,24 +85,25 @@ void printDigits(int digits){
|
|||||||
Serial.print(digits);
|
Serial.print(digits);
|
||||||
}
|
}
|
||||||
|
|
||||||
void showDuration(time_t duration){
|
void showDuration(time_t duration)
|
||||||
// prints the duration in days, hours, minutes and seconds
|
{
|
||||||
if(duration >= SECS_PER_DAY){
|
// prints the duration in days, hours, minutes and seconds
|
||||||
|
if (duration >= SECS_PER_DAY) {
|
||||||
Serial.print(duration / SECS_PER_DAY);
|
Serial.print(duration / SECS_PER_DAY);
|
||||||
Serial.print(" day(s) ");
|
Serial.print(" day(s) ");
|
||||||
duration = duration % SECS_PER_DAY;
|
duration = duration % SECS_PER_DAY;
|
||||||
}
|
}
|
||||||
if(duration >= SECS_PER_HOUR){
|
if (duration >= SECS_PER_HOUR) {
|
||||||
Serial.print(duration / SECS_PER_HOUR);
|
Serial.print(duration / SECS_PER_HOUR);
|
||||||
Serial.print(" hour(s) ");
|
Serial.print(" hour(s) ");
|
||||||
duration = duration % SECS_PER_HOUR;
|
duration = duration % SECS_PER_HOUR;
|
||||||
}
|
}
|
||||||
if(duration >= SECS_PER_MIN){
|
if (duration >= SECS_PER_MIN) {
|
||||||
Serial.print(duration / SECS_PER_MIN);
|
Serial.print(duration / SECS_PER_MIN);
|
||||||
Serial.print(" minute(s) ");
|
Serial.print(" minute(s) ");
|
||||||
duration = duration % SECS_PER_MIN;
|
duration = duration % SECS_PER_MIN;
|
||||||
}
|
}
|
||||||
Serial.print(duration);
|
Serial.print(duration);
|
||||||
Serial.print(" second(s) ");
|
Serial.print(" second(s) ");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user