TimeRTCLog
From: https://github.com/thijse/Arduino-Code-and-Libraries/blob/master/Libraries/Time/Examples/TimeRTCLog/TimeRTCLog.pde
/*H*******************************************************
* TimeRTCLogger.pde
* example code illustrating adding and subtracting Time.
* 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.
********************************************************/
#include <Time.h>
#include <Wire.h>
#include <DS1307RTC.h> // BASIC DS1307 LIBRARY THAT RETURNS TIME AS time_t
//************************* DEFINES ************************************
#define BAUD 9600
const int nbrInputPins = 6; // monitor 6 digital pins
const int inputPins[nbrInputPins] = {2,3,4,5,6,7}; // pins to monitor
//************************* PROTOTYPES ************************************
void logEvent( int pin, boolean state, time_t timeNow, time_t duration );
void showTime( time_t t );
printDigits( int digits );
void showDuration( time_t duration );
//************************* VARIABLES ************************************
boolean state[nbrInputPins] ; // STATE OF MONITORED PINS
time_t prevEventTime[nbrInputPins] ; // TIME OF PREVIOUS EVENT
/*F********************************************************************
*
**********************************************************************/
void
setup( )
{
Serial.begin( 9600 );
setSyncProvider( RTC.get ); // FUNCTION TO SYNC TIME FROM RTC
for( int i =0; i < nbrInputPins; i++ )
{
pinMode( inputPins[i], INPUT );
// digitalWrite( inputPins[i], HIGH ); // UNCOMMENT THESE LINES IF
// state[i] = HIGH; // PULL-UP RESISTORS ARE WANTED
}
}
/*F********************************************************************
*
**********************************************************************/
void
loop( )
{
for( int i=0; i < nbrInputPins; i++ )
{
boolean val = digitalRead( inputPins[i] );
if( val != state[i] )
{
time_t duration = 0; // the time since the previous event
state[i] = val;
time_t timeNow = now( );
if( prevEventTime[i] > 0 )// NOT 1ST STATE CHNG, CALC TIME FROM PRIOR CHNG
duration = duration = timeNow - prevEventTime[i];
logEvent( inputPins[i], val, timeNow, duration ); // LOG EVENT
prevEventTime[i] = timeNow; // STORE TIME FOR THIS EVENT
}
}
}
/*F********************************************************************
*
**********************************************************************/
void
logEvent( int pin, boolean state, time_t timeNow, time_t duration )
{
Serial.print( "Pin " );
Serial.print( pin );
if( state == HIGH )
Serial.print( " went High at " );
else
Serial.print( " went Low at " );
showTime( timeNow );
if( duration > 0 )
{
// ONLY DISPLAY DURATION IF GREATER THAN 0
Serial.print( ", Duration was " );
showDuration( duration );
}
Serial.println( );
}
/*F********************************************************************
* DISPLAY GIVEN TIME
**********************************************************************/
void
showTime( time_t t )
{
Serial.print( hour( t ) );
printDigits( minute( t ) );
printDigits( second( t ) );
Serial.print( " " );
Serial.print( day( t ) );
Serial.print( " " );
Serial.print( month( t ) );
Serial.print( " " );
Serial.print( year( t ) );
}
/*F********************************************************************
* DIGITAL CLOCK DISPLAY: PRINTS PRECEDING COLON AND LEADING 0
**********************************************************************/
void
printDigits( int digits )
{
Serial.print( ":" );
if( digits < 10 )
Serial.print( '0' );
Serial.print( digits );
}
/*F********************************************************************
* PRINTS DURATION IN DAYS, HOURS, MINUTES AND SECONDS
**********************************************************************/
void
showDuration( time_t duration )
{
if( duration >= SECS_PER_DAY )
{
Serial.print( duration / SECS_PER_DAY );
Serial.print( " day( s ) " );
duration = duration % SECS_PER_DAY;
}
if( duration >= SECS_PER_HOUR )
{
Serial.print( duration / SECS_PER_HOUR );
Serial.print( " hour( s ) " );
duration = duration % SECS_PER_HOUR;
}
if( duration >= SECS_PER_MIN )
{
Serial.print( duration / SECS_PER_MIN );
Serial.print( " minute( s ) " );
duration = duration % SECS_PER_MIN;
}
Serial.print( duration );
Serial.print( " second( s ) " );
}