summaryrefslogtreecommitdiff
path: root/Time/Examples/TimeRTCLog/TimeRTCLog.pde
blob: 76ed17dc34938cb6c6c68d6be4d45ec754b60628 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
 * 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>  // a basic DS1307 library that returns time as a time_t

const int nbrInputPins  = 6;             // monitor 6 digital pins 
const int inputPins[nbrInputPins] = {2,3,4,5,6,7};  // pins to monitor
boolean state[nbrInputPins] ;            // the state of the monitored pins
time_t  prevEventTime[nbrInputPins] ;    // the time of the previous event

void setup()  {
  Serial.begin(9600);
  setSyncProvider(RTC.get);   // the function to sync the time from the 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
  }
}

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)  
           // if this was not the first state change, calculate the time from the previous change
           duration = duration = timeNow - prevEventTime[i];         
        logEvent(inputPins[i], val, timeNow, duration );  // log the event
        prevEventTime[i] = timeNow;                       // store the time for this event  
     }
   }
}

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();
}


void showTime(time_t t){
  // display the given time 
  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)); 
}

void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

void showDuration(time_t duration){
// prints the duration in days, hours, minutes and seconds
  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) ");   
}