diff --git a/Readme.md b/Readme.md index 795d26e..4ba53cc 100644 --- a/Readme.md +++ b/Readme.md @@ -1,3 +1,24 @@ +# Fork Edit + +This library has been modified to achieve timer capabilities. This includes +checking elapsed time from a certain point and running functions after a set interval +of time. + +The following functions have been added: +```c +timerReset(); //resets the timer - equivalent of starting a new timer. +timerNow(); //returns how many ms have passed since last timerReset +timerSecond(); //returns how many seconds have passed since last timerReset, returns integer value +timerMin(); //returns how many minutes have passed since last timerReset, returns integer value +timerMin(); //returns how many hours have passed since last timerReset, returns integer value +timerEvery(interval,functionToRun); //runs function every set interval +``` +Note that the timer is reset on every board RESET. If timer is never reset, it'll be equivalent to millis() + +`timerEvery()` takes two parameters, first is interval duration in milliseconds, and the other is the function to run. +The `functionToRun` can NOT TAKE PARAMETERS and should return void. +If you want to pass a functionA that does take parameters, consider wrapping the functionA in another functionB that doesn't take parameters and simply runs functionA. + # Arduino Time Library Time is a library that provides timekeeping functionality for Arduino. diff --git a/Time.cpp b/Time.cpp index 8e53e56..71b6381 100644 --- a/Time.cpp +++ b/Time.cpp @@ -15,7 +15,7 @@ You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - + 1.0 6 Jan 2010 - initial release 1.1 12 Feb 2010 - fixed leap year calculation error 1.2 1 Nov 2010 - fixed setTime bug (thanks to Korman for this) @@ -28,9 +28,9 @@ */ #if ARDUINO >= 100 -#include +#include #else -#include +#include #endif #include "TimeLib.h" @@ -41,22 +41,22 @@ static uint32_t syncInterval = 300; // time sync will be attempted after this m void refreshCache(time_t t) { if (t != cacheTime) { - breakTime(t, tm); - cacheTime = t; + breakTime(t, tm); + cacheTime = t; } } -int hour() { // the hour now - return hour(now()); +int hour() { // the hour now + return hour(now()); } int hour(time_t t) { // the hour for the given time refreshCache(t); - return tm.Hour; + return tm.Hour; } int hourFormat12() { // the hour now in 12 hour format - return hourFormat12(now()); + return hourFormat12(now()); } int hourFormat12(time_t t) { // the hour for the given time in 12 hour format @@ -70,32 +70,32 @@ int hourFormat12(time_t t) { // the hour for the given time in 12 hour format } uint8_t isAM() { // returns true if time now is AM - return !isPM(now()); + return !isPM(now()); } uint8_t isAM(time_t t) { // returns true if given time is AM - return !isPM(t); + return !isPM(t); } uint8_t isPM() { // returns true if PM - return isPM(now()); + return isPM(now()); } uint8_t isPM(time_t t) { // returns true if PM - return (hour(t) >= 12); + return (hour(t) >= 12); } int minute() { - return minute(now()); + return minute(now()); } int minute(time_t t) { // the minute for the given time refreshCache(t); - return tm.Minute; + return tm.Minute; } int second() { - return second(now()); + return second(now()); } int second(time_t t) { // the second for the given time @@ -104,7 +104,7 @@ int second(time_t t) { // the second for the given time } int day(){ - return(day(now())); + return(day(now())); } int day(time_t t) { // the day for the given time (0-6) @@ -113,16 +113,16 @@ int day(time_t t) { // the day for the given time (0-6) } int weekday() { // Sunday is day 1 - return weekday(now()); + return weekday(now()); } int weekday(time_t t) { refreshCache(t); return tm.Wday; } - + int month(){ - return month(now()); + return month(now()); } int month(time_t t) { // the month for the given time @@ -130,8 +130,8 @@ int month(time_t t) { // the month for the given time return tm.Month; } -int year() { // as in Processing, the full four digit year: (2009, 2010 etc) - return year(now()); +int year() { // as in Processing, the full four digit year: (2009, 2010 etc) + return year(now()); } int year(time_t t) { // the year for the given time @@ -139,7 +139,7 @@ int year(time_t t) { // the year for the given time return tmYearToCalendar(tm.Year); } -/*============================================================================*/ +/*============================================================================*/ /* functions to convert to and from system time */ /* These are for interfacing with time serivces and are not normally needed in a sketch */ @@ -147,7 +147,7 @@ int year(time_t t) { // the year for the given time #define LEAP_YEAR(Y) ( ((1970+(Y))>0) && !((1970+(Y))%4) && ( ((1970+(Y))%100) || !((1970+(Y))%400) ) ) static const uint8_t monthDays[]={31,28,31,30,31,30,31,31,30,31,30,31}; // API starts months from 1, this array starts from 0 - + void breakTime(time_t timeInput, tmElements_t &tm){ // break the given time_t into time components // this is a more compact version of the C library localtime function @@ -165,18 +165,18 @@ void breakTime(time_t timeInput, tmElements_t &tm){ time /= 60; // now it is hours tm.Hour = time % 24; time /= 24; // now it is days - tm.Wday = ((time + 4) % 7) + 1; // Sunday is day 1 - - year = 0; + tm.Wday = ((time + 4) % 7) + 1; // Sunday is day 1 + + year = 0; days = 0; while((unsigned)(days += (LEAP_YEAR(year) ? 366 : 365)) <= time) { year++; } - tm.Year = year; // year is offset from 1970 - + tm.Year = year; // year is offset from 1970 + days -= LEAP_YEAR(year) ? 366 : 365; time -= days; // now it is days in this year, starting at 0 - + days=0; month=0; monthLength=0; @@ -190,22 +190,22 @@ void breakTime(time_t timeInput, tmElements_t &tm){ } else { monthLength = monthDays[month]; } - + if (time >= monthLength) { time -= monthLength; } else { break; } } - tm.Month = month + 1; // jan is month 1 + tm.Month = month + 1; // jan is month 1 tm.Day = time + 1; // day of month } -time_t makeTime(const tmElements_t &tm){ -// assemble time elements into time_t +time_t makeTime(const tmElements_t &tm){ +// assemble time elements into time_t // note year argument is offset from 1970 (see macros in time.h to convert to other formats) // previous version used full four digit year (or digits since 2000),i.e. 2009 was 2009 or 9 - + int i; uint32_t seconds; @@ -216,10 +216,10 @@ time_t makeTime(const tmElements_t &tm){ seconds += SECS_PER_DAY; // add extra days for leap years } } - + // add days for this year, months start from 1 for (i = 1; i < tm.Month; i++) { - if ( (i == 2) && LEAP_YEAR(tm.Year)) { + if ( (i == 2) && LEAP_YEAR(tm.Year)) { seconds += SECS_PER_DAY * 29; } else { seconds += SECS_PER_DAY * monthDays[i-1]; //monthDay array starts from 0 @@ -229,9 +229,9 @@ time_t makeTime(const tmElements_t &tm){ seconds+= tm.Hour * SECS_PER_HOUR; seconds+= tm.Minute * SECS_PER_MIN; seconds+= tm.Second; - return (time_t)seconds; + return (time_t)seconds; } -/*=====================================================*/ +/*=====================================================*/ /* Low level system time functions */ static uint32_t sysTime = 0; @@ -243,7 +243,7 @@ getExternalTime getTimePtr; // pointer to external sync function //setExternalTime setTimePtr; // not used in this version #ifdef TIME_DRIFT_INFO // define this to get drift data -time_t sysUnsyncedTime = 0; // the time sysTime unadjusted by sync +time_t sysUnsyncedTime = 0; // the time sysTime unadjusted by sync #endif @@ -252,9 +252,9 @@ time_t now() { while (millis() - prevMillis >= 1000) { // millis() and prevMillis are both unsigned ints thus the subtraction will always be the absolute value of the difference sysTime++; - prevMillis += 1000; + prevMillis += 1000; #ifdef TIME_DRIFT_INFO - sysUnsyncedTime++; // this can be compared to the synced time to measure long term drift + sysUnsyncedTime++; // this can be compared to the synced time to measure long term drift #endif } if (nextSyncTime <= sysTime) { @@ -267,29 +267,29 @@ time_t now() { Status = (Status == timeNotSet) ? timeNotSet : timeNeedsSync; } } - } + } return (time_t)sysTime; } -void setTime(time_t t) { +void setTime(time_t t) { #ifdef TIME_DRIFT_INFO - if(sysUnsyncedTime == 0) - sysUnsyncedTime = t; // store the time of the first call to set a valid Time + if(sysUnsyncedTime == 0) + sysUnsyncedTime = t; // store the time of the first call to set a valid Time #endif - sysTime = (uint32_t)t; + sysTime = (uint32_t)t; nextSyncTime = (uint32_t)t + syncInterval; Status = timeSet; prevMillis = millis(); // restart counting from now (thanks to Korman for this fix) -} +} void setTime(int hr,int min,int sec,int dy, int mnth, int yr){ - // year can be given as full four digit year or two digts (2010 or 10 for 2010); + // year can be given as full four digit year or two digts (2010 or 10 for 2010); //it is converted to years since 1970 if( yr > 99) yr = yr - 1970; else - yr += 30; + yr += 30; tm.Year = yr; tm.Month = mnth; tm.Day = dy; @@ -310,7 +310,7 @@ timeStatus_t timeStatus() { } void setSyncProvider( getExternalTime getTimeFunction){ - getTimePtr = getTimeFunction; + getTimePtr = getTimeFunction; nextSyncTime = sysTime; now(); // this will sync the clock } @@ -319,3 +319,45 @@ void setSyncInterval(time_t interval){ // set the number of seconds between re-s syncInterval = (uint32_t)interval; nextSyncTime = sysTime + syncInterval; } + + +unsigned long timeStart = 0; //indicates the position of last restart. Used as a refernce for our timer. +unsigned long timeHold = 0; //indicates time since function was last run + +void timerReset() //resets the timer - equivalent of starting a new timer +{ + timeStart = millis(); //update our refernce to current time. (Reset clock to now) +} + +unsigned long timerNow() //returns how many ms have passed since last timerReset +{ + return (millis() - timeStart); +} + +unsigned long timerSecond() //returns how many seconds have passed since last timerReset, notice this is an *integer* value +{ + return (millis() - timeStart) / 1000; +} + +unsigned long timerMin() //returns how many minutes have passed since last timerReset, notice this is an *integer* value +{ + return (millis() - timeStart) / 60000; +} + +unsigned long timerHour() //returns how many hours have passed since last timerReset, notice this is an *integer* value +{ + return (millis() - timeStart) / 3600000; +} + +void timerEvery(int interval, void *functionPtr) +{ + void(*f)(); + f = functionPtr; + + if (timerNow() - timeHold >= interval) + { + //run function + f(); + timeHold = timerNow(); + } +} diff --git a/TimeLib.h b/TimeLib.h index 20e2445..8584565 100644 --- a/TimeLib.h +++ b/TimeLib.h @@ -5,7 +5,7 @@ /* July 3 2011 - fixed elapsedSecsThisWeek macro (thanks Vincent Valdy for this) - fixed daysToTime_t macro (thanks maniacbug) -*/ +*/ #ifndef _Time_h #ifdef __cplusplus @@ -40,23 +40,23 @@ typedef enum { typedef enum { tmSecond, tmMinute, tmHour, tmWday, tmDay,tmMonth, tmYear, tmNbrFields -} tmByteFields; +} tmByteFields; -typedef struct { - uint8_t Second; - uint8_t Minute; - uint8_t Hour; +typedef struct { + uint8_t Second; + uint8_t Minute; + uint8_t Hour; uint8_t Wday; // day of week, sunday is day 1 uint8_t Day; - uint8_t Month; - uint8_t Year; // offset from 1970; + uint8_t Month; + uint8_t Year; // offset from 1970; } tmElements_t, TimeElements, *tmElementsPtr_t; -//convenience macros to convert to and from tm years -#define tmYearToCalendar(Y) ((Y) + 1970) // full four digit year +//convenience macros to convert to and from tm years +#define tmYearToCalendar(Y) ((Y) + 1970) // full four digit year #define CalendarYrToTm(Y) ((Y) - 1970) #define tmYearToY2k(Y) ((Y) - 30) // offset is from 2000 -#define y2kYearToTm(Y) ((Y) + 30) +#define y2kYearToTm(Y) ((Y) + 30) typedef time_t(*getExternalTime)(); //typedef void (*setExternalTime)(const time_t); // not used in this version @@ -71,32 +71,32 @@ typedef time_t(*getExternalTime)(); #define SECS_PER_WEEK ((time_t)(SECS_PER_DAY * DAYS_PER_WEEK)) #define SECS_PER_YEAR ((time_t)(SECS_PER_DAY * 365UL)) // TODO: ought to handle leap years #define SECS_YR_2000 ((time_t)(946684800UL)) // the time at the start of y2k - + /* Useful Macros for getting elapsed time */ -#define numberOfSeconds(_time_) ((_time_) % SECS_PER_MIN) -#define numberOfMinutes(_time_) (((_time_) / SECS_PER_MIN) % SECS_PER_MIN) +#define numberOfSeconds(_time_) ((_time_) % SECS_PER_MIN) +#define numberOfMinutes(_time_) (((_time_) / SECS_PER_MIN) % SECS_PER_MIN) #define numberOfHours(_time_) (((_time_) % SECS_PER_DAY) / SECS_PER_HOUR) #define dayOfWeek(_time_) ((((_time_) / SECS_PER_DAY + 4) % DAYS_PER_WEEK)+1) // 1 = Sunday #define elapsedDays(_time_) ((_time_) / SECS_PER_DAY) // this is number of days since Jan 1 1970 -#define elapsedSecsToday(_time_) ((_time_) % SECS_PER_DAY) // the number of seconds since last midnight +#define elapsedSecsToday(_time_) ((_time_) % SECS_PER_DAY) // the number of seconds since last midnight // The following macros are used in calculating alarms and assume the clock is set to a date later than Jan 1 1971 // Always set the correct time before settting alarms #define previousMidnight(_time_) (((_time_) / SECS_PER_DAY) * SECS_PER_DAY) // time at the start of the given day -#define nextMidnight(_time_) (previousMidnight(_time_) + SECS_PER_DAY) // time at the end of the given day +#define nextMidnight(_time_) (previousMidnight(_time_) + SECS_PER_DAY) // time at the end of the given day #define elapsedSecsThisWeek(_time_) (elapsedSecsToday(_time_) + ((dayOfWeek(_time_)-1) * SECS_PER_DAY)) // note that week starts on day 1 #define previousSunday(_time_) ((_time_) - elapsedSecsThisWeek(_time_)) // time at the start of the week for the given time #define nextSunday(_time_) (previousSunday(_time_)+SECS_PER_WEEK) // time at the end of the week for the given time /* Useful Macros for converting elapsed time to a time_t */ -#define minutesToTime_t ((M)) ( (M) * SECS_PER_MIN) -#define hoursToTime_t ((H)) ( (H) * SECS_PER_HOUR) +#define minutesToTime_t ((M)) ( (M) * SECS_PER_MIN) +#define hoursToTime_t ((H)) ( (H) * SECS_PER_HOUR) #define daysToTime_t ((D)) ( (D) * SECS_PER_DAY) // fixed on Jul 22 2011 -#define weeksToTime_t ((W)) ( (W) * SECS_PER_WEEK) +#define weeksToTime_t ((W)) ( (W) * SECS_PER_WEEK) /*============================================================================*/ /* time and date functions */ -int hour(); // the hour now +int hour(); // the hour now int hour(time_t t); // the hour for the given time int hourFormat12(); // the hour now in 12 hour format int hourFormat12(time_t t); // the hour for the given time in 12 hour format @@ -104,31 +104,39 @@ uint8_t isAM(); // returns true if time now is AM uint8_t isAM(time_t t); // returns true the given time is AM uint8_t isPM(); // returns true if time now is PM uint8_t isPM(time_t t); // returns true the given time is PM -int minute(); // the minute now +int minute(); // the minute now int minute(time_t t); // the minute for the given time -int second(); // the second now +int second(); // the second now int second(time_t t); // the second for the given time -int day(); // the day now +int day(); // the day now int day(time_t t); // the day for the given time -int weekday(); // the weekday now (Sunday is day 1) -int weekday(time_t t); // the weekday for the given time +int weekday(); // the weekday now (Sunday is day 1) +int weekday(time_t t); // the weekday for the given time int month(); // the month now (Jan is month 1) int month(time_t t); // the month for the given time -int year(); // the full four digit year: (2009, 2010 etc) +int year(); // the full four digit year: (2009, 2010 etc) int year(time_t t); // the year for the given time -time_t now(); // return the current time as seconds since Jan 1 1970 +unsigned long timerNow(); //returns how many ms have passed since last timerReset +unsigned long timerSecond(); //returns how many seconds have passed since last timerReset, notice this is an *integer* value +unsigned long timerMin(); //returns how many minutes have passed since last timerReset, notice this is an *integer* value +unsigned long timerHour(); //returns how many hours have passed since last timerReset, notice this is an *integer* value + +void timerEvery(int interval, void (*functionPtr)); +void timerReset(); //resets the timer - equivalent of starting a new timer + +time_t now(); // return the current time as seconds since Jan 1 1970 void setTime(time_t t); void setTime(int hr,int min,int sec,int day, int month, int yr); void adjustTime(long adjustment); -/* date strings */ +/* date strings */ #define dt_MAX_STRING_LEN 9 // length of longest date string (excluding terminating null) char* monthStr(uint8_t month); char* dayStr(uint8_t day); char* monthShortStr(uint8_t month); char* dayShortStr(uint8_t day); - + /* time sync functions */ timeStatus_t timeStatus(); // indicates if time has been set and recently synchronized void setSyncProvider( getExternalTime getTimeFunction); // identify the external time provider @@ -141,4 +149,3 @@ time_t makeTime(const tmElements_t &tm); // convert time elements into time_t } // extern "C++" #endif // __cplusplus #endif /* _Time_h */ - diff --git a/examples/Blink_Timer/Blink_Timer.ino b/examples/Blink_Timer/Blink_Timer.ino new file mode 100644 index 0000000..375d27b --- /dev/null +++ b/examples/Blink_Timer/Blink_Timer.ino @@ -0,0 +1,18 @@ +#include +#include + + +void setup() { + // put your setup code here, to run once: +pinMode(11,OUTPUT); +} + +void loop() { + // put your main code here, to run repeatedly: + timerEvery(1000,toggle); + +} + +void toggle(){ + digitalWrite(11,!digitalRead(11)); + } diff --git a/examples/time_Timer/time_Timer.ino b/examples/time_Timer/time_Timer.ino new file mode 100644 index 0000000..922d5eb --- /dev/null +++ b/examples/time_Timer/time_Timer.ino @@ -0,0 +1,25 @@ +#include +#include + +void setup() { + // put your setup code here, to run once: +Serial.begin(9600); +} + +void loop() { + // put your main code here, to run repeatedly: + Serial.print("Hours Passed: "); + Serial.println(timerHour()); + + Serial.print("Minutes Passed:"); + Serial.println(timerMin()); + + Serial.print("Seconds Passed:"); + Serial.println(timerSecond()); + + Serial.print("Millis Passed:"); + Serial.println(timerNow()); + + Serial.println(); + delay(1000); +} diff --git a/examples/timerReset_Timer/timerReset_Timer.ino b/examples/timerReset_Timer/timerReset_Timer.ino new file mode 100644 index 0000000..5f43e6b --- /dev/null +++ b/examples/timerReset_Timer/timerReset_Timer.ino @@ -0,0 +1,25 @@ +#include +#include + + +void setup() { + // put your setup code here, to run once: + Serial.begin(9600); + pinMode(11,INPUT); +} + +void loop() { + // put your main code here, to run repeatedly: + Serial.print("Time Since Last Reset:" ); + Serial.print(timerNow()); + Serial.print(","); + Serial.println(millis()); + + if (digitalRead(11)){ + Serial.print("Reset"); + timerReset(); + } + + delay(500); + +} diff --git a/keywords.txt b/keywords.txt index 073f8f8..5b70604 100644 --- a/keywords.txt +++ b/keywords.txt @@ -9,6 +9,16 @@ time_t KEYWORD1 ####################################### # Methods and Functions (KEYWORD2) ####################################### + +timerReset KEYWORD2 +timerNow KEYWORD2 +timerSecond KEYWORD2 +timerMin KEYWORD2 +timerHour KEYWORD2 + +timerEvery KEYWORD2 + + now KEYWORD2 second KEYWORD2 minute KEYWORD2