I had a similar problem when i tried to control some heating source here at home.This was how I made my steps to get it working.
First I will assume you are using the library time.h and DS1307RTC.h (check here for info: https://www.pjrc.com/teensy/td_libs_Time.html).
Assuming that you will need 3 variables of the type tmElements_t.
tm will store your current time( the one you get from DS1307). The other two are auxiliary for defining time you start and time you stop the pump.
In similar way, you will need 3 variables of type time_t for the same reasons.
time_t variables store a the timestamps since 01/01/1970 (Unix epoch) which are easier to work with but tmElements structures are more human friendly.
tmElements_t tm, tm_hour_start, tm_hour_end;
time_t now, t_hour_start, t_hour_end;
Now in your loop you need to get the current time from rtc:
time_t now = RTC.get();
after, use the breaktime funtion to convert the timestamp value to tmElements struture:
breakTime(now, tm);
Next step is to copy contents of tm to the other auxiliary structures, use the memcpy function:
memcpy(&tm_hour_start, &tm, sizeof(tm));
memcpy(&tm_hour_end, &tm, sizeof(tm));
after, change the the auxiliaries vars to meet your correct schedule of start and stop (are seconds important? if so, do similar way):
tm_hour_start.Hour = startHour;
tm_hour_start.Minute = startMinute;
tm_hour_start.Second = 0;
tm_hour_end.Hour = endHour;
tm_hour_end.Minute = endMinute;
tm_hour_end.Second = 0;
Ok, we have the more human friendly start and end structures, lets get the timestamps from them. To do soo we use the makeTime function that converts tmElements_t structures to time_t:
t_hour_start = makeTime(tm_hour_start);
t_hour_end = makeTime(tm_hour_end);
All you need to do now, is check if your current timestamp (now) is between this newly created timestamps:
if ((t_hour_start <= now) && (now <= t_hour_end)){
//do something is soo...
}else{
// do something if not...
}
Extra tip: In case your stop hour goes past midnight, add one more day to the timestamp:
if (startHour > endHour) //past midnight correction
t_hour_end = t_hour_end + SECS_PER_DAY;
clean code:
#include <time.h>
#include <DS1307RTC.h>
#include <stdio.h>
// other includes etc...
byte startHour=8
byte startMinute=15
byte endHour= 14
byte endMinute= 20
tmElements_t tm, tm_hour_start, tm_hour_end;
time_t now, t_hour_start, t_hour_end;
// setup function and other needed things...
void loop(){
//get current timestamp
time_t now = RTC.get();
// make current date and time structure
breakTime(now, tm);
// make auxiliary structures to be more human editable and friendly
memcpy(&tm_hour_start, &tm, sizeof(tm));
memcpy(&tm_hour_end, &tm, sizeof(tm));
// change auxiliary structures to meet your start and end schedule
tm_hour_start.Hour = startHour;
tm_hour_start.Minute = startMinute;
tm_hour_start.Second = 0;
tm_hour_end.Hour = endHour;
tm_hour_end.Minute = endMinute;
tm_hour_end.Second = 0;
// reverse process to get timestamps
t_hour_start = makeTime(tm_hour_start);
t_hour_end = makeTime(tm_hour_end);
// check if end time is past midnight and correct if needed
if (startHour > endHour) //past midnight correction
t_hour_end = t_hour_end + SECS_PER_DAY;
//final part
if ((t_hour_start <= now) && (now <= t_hour_end)){
//do something is soo...
}else{
// do something if not...
}
}
EDIT: to correct the thing about midnight checks spotted in comments. New code:
#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h>
#include <stdio.h>
// other includes etc...
byte startHour = 8;
byte startMinute = 15;
byte endHour = 14;
byte endMinute = 20;
tmElements_t tm, tm_hour_start, tm_hour_end;
time_t now_, t_hour_start, t_hour_end;
boolean update_tm = 1;
// setup function and other needed things...
void setup(){};
void loop(){
//get current timestamp
time_t now_ = RTC.get();
// make current date and time structure
breakTime(now_, tm);
// make auxiliary structures to be more human editable and friendly
if(update_tm){
memcpy(&tm_hour_start, &tm, sizeof(tm));
memcpy(&tm_hour_end, &tm, sizeof(tm));
// change auxiliary structures to meet your start and end schedule
tm_hour_start.Hour = startHour;
tm_hour_start.Minute = startMinute;
tm_hour_start.Second = 0;
tm_hour_end.Hour = endHour;
tm_hour_end.Minute = endMinute;
tm_hour_end.Second = 0;
// reverse process to get timestamps
t_hour_start = makeTime(tm_hour_start);
t_hour_end = makeTime(tm_hour_end);
// check if end time is past midnight and correct if needed
if (startHour > endHour) //past midnight correction
t_hour_end = t_hour_end + SECS_PER_DAY;
}
//final part
if ((t_hour_start <= now_) && (now_ <= t_hour_end)){
/* if we got a valid schedule, don't change the tm_hour structures and the
respective t_hour_start and t_hour_end timestamps. They should be updated
after exiting the valid schedule */
if(update_tm)
update_tm = 0;
//do something is soo...
}else{
if(update_tm == 0)
update_tm = 1;
// do something if not...
}
}