]> git.piffa.net Git - arduino/blob - books/pdummies/Libraries/Time/Readme.txt
first commit
[arduino] / books / pdummies / Libraries / Time / Readme.txt
1 Readme file for Arduino Time Library\r
2 \r
3 Time is a library that provides timekeeping functionality for Arduino.\r
4 \r
5 The code is derived from the Playground DateTime library but is updated\r
6 to provide an API that is more flexable and easier to use.\r
7 \r
8 A primary goal was to enable date and time functionality that can be used with\r
9 a variety of external time sources with minimum differences required in sketch logic.\r
10 \r
11 Example sketches illustrate how similar sketch code can be used with: a Real Time Clock,\r
12 internet NTP time service, GPS time data, and Serial time messages from a computer\r
13 for time synchronization.\r
14 \r
15 The functions available in the library include:\r
16 \r
17 hour();            // the hour now  (0-23)\r
18 minute();          // the minute now (0-59)          \r
19 second();          // the second now (0-59) \r
20 day();             // the day now (1-31)\r
21 weekday();         // day of the week, Sunday is day 0 \r
22 month();           // the month now (1-12)\r
23 year();            // the full four digit year: (2009, 2010 etc) \r
24 \r
25 there are also functions to return the hour in 12 hour format\r
26 hourFormat12();    // the hour now in 12 hour format\r
27 isAM();            // returns true if time now is AM \r
28 isPM();            // returns true if time now is PM\r
29 \r
30 now();             // returns the current time as seconds since Jan 1 1970 \r
31 \r
32 The time and date functions can take an optional parameter for the time. This prevents\r
33 errors if the time rolls over between elements. For example, if a new minute begins\r
34 between getting the minute and second, the values will be inconsistent. Using the \r
35 following functions eliminates this probglem \r
36   time_t t = now(); // store the current time in time variable t \r
37   hour(t);          // returns the hour for the given time t\r
38   minute(t);        // returns the minute for the given time t\r
39   second(t);        // returns the second for the given time t \r
40   day(t);           // the day for the given time t \r
41   weekday(t);       // day of the week for the given time t  \r
42   month(t);         // the month for the given time t \r
43   year(t);          // the year for the given time t  \r
44   \r
45   \r
46 Functions for managing the timer services are:  \r
47 setTime(t);             // set the system time to the give time t\r
48 setTime(hr,min,sec,day,mnth,yr); // alternative to above, yr is 2 or 4 digit yr (2010 or 10 sets year to 2010)\r
49 adjustTime(adjustment); // adjust system time by adding the adjustment value\r
50 \r
51 timeStatus();       // indicates if time has been set and recently synchronized\r
52                     // returns one of the following enumerations:\r
53     timeNotSet      // the time has never been set, the clock started at Jan 1 1970\r
54     timeNeedsSync   // the time had been set but a sync attempt did not succeed\r
55     timeSet         // the time is set and is synced\r
56 Time and Date values are not valid if the status is timeNotSet. Otherwise values can be used but \r
57 the returned time may have drifted if the status is timeNeedsSync.      \r
58 \r
59 setSyncProvider(getTimeFunction);  // set the external time provider\r
60 setSyncInterval(interval);         // set the number of seconds between re-sync\r
61 \r
62 \r
63 There are many convenience macros in the time.h file for time constants and conversion of time units.\r
64 \r
65 To use the library, copy the download to the Library directory.\r
66 \r
67 The Time directory contains the Time library and some example sketches\r
68 illustrating how the library can be used with various time sources:\r
69 \r
70 - TimeSerial.pde shows Arduino as a clock without external hardware.\r
71   It is synchronized by time messages sent over the serial port.\r
72   A companion Processing sketch will automatically provide these messages\r
73   if it is running and connected to the Arduino serial port. \r
74 \r
75 - TimeSerialDateStrings.pde adds day and month name strings to the sketch above\r
76   Short (3 character) and long strings are available to print the days of \r
77   the week and names of the months. \r
78   \r
79 - TimeRTC uses a DS1307 real time clock to provide time synchronization.\r
80   A basic RTC library named DS1307RTC is included in the download.\r
81   To run this sketch the DS1307RTC library must be installed.\r
82 \r
83 - TimeRTCSet is similar to the above and adds the ability to set the Real Time Clock \r
84 \r
85 - TimeRTCLog demonstrates how to calculate the difference between times. \r
86   It is a vary simple logger application that monitors events on digtial pins\r
87   and prints (to the serial port) the time of an event and the time period since the previous event.\r
88   \r
89 - TimeNTP uses the Arduino Ethernet shield to access time using the internet NTP time service.\r
90   The NTP protocol uses UDP and the UdpBytewise library is required, see:\r
91   http://bitbucket.org/bjoern/arduino_osc/src/14667490521f/libraries/Ethernet/\r
92 \r
93 -TimeGPS gets time from a GPS\r
94  This requires the TinyGPS and NewSoftSerial libraries from Mikal Hart:\r
95  http://arduiniana.org/libraries/TinyGPS and http://arduiniana.org/libraries/newsoftserial/\r
96 \r
97 Differences between this code and the playground DateTime library\r
98 although the Time library is based on the DateTime codebase, the API has changed.\r
99 Changes in the Time library API:\r
100 - time elements are functions returning int (they are variables in DateTime)\r
101 - Years start from 1970 \r
102 - days of the week and months start from 1 (they start from 0 in DateTime)\r
103 - DateStrings do not require a seperate library\r
104 - time elements can be accessed non-atomically (in DateTime they are always atomic)\r
105 - function added to automatically sync time with extrnal source\r
106 - localTime and maketime parameters changed, localTime renamed to breakTime\r
107  \r
108 Technical notes:\r
109 \r
110 Internal system time is based on the standard Unix time_t.\r
111 The value is the number of seconds since Jan 1 1970.\r
112 System time begins at zero when the sketch starts.\r
113   \r
114 The internal time can be automatically synchronized at regular intervals to an external time source.\r
115 This is enabled by calling the setSyncProvider(provider) function - the provider argument is\r
116 the address of a function that returns the current time as a time_t.\r
117 See the sketches in the examples directory for usage.\r
118 \r
119 The default interval for re-syncing the time is 5 minutes but can be changed by calling the \r
120 setSyncInterval( interval) method to set the number of seconds between re-sync attempts.\r
121 \r
122 The Time library defines a structure for holding time elements that is a compact version of the  C tm structure.\r
123 All the members of the Arduino tm structure are bytes and the year is offset from 1970.\r
124 Convenience macros provide conversion to and from the Arduino format.\r
125 \r
126 Low level functions to convert between system time and individual time elements are provided:                    \r
127   breakTime( time, &tm);  // break time_t into elements stored in tm struct\r
128   makeTime( &tm);  // return time_t  from elements stored in tm struct \r
129 \r
130 The DS1307RTC library included in the download provides an example of how a time provider\r
131 can use the low level functions to interface with the Time library.