Go Down

Topic: DateTime question (Read 936 times) previous topic - next topic


In another thread you suggested the following code using DateTime to turn an led on for a specific length of time at a specific time of day.

Code: [Select]

time_t  alarmOnTime =  (8 * SECS_PER_HOUR) + (30 * SECS_PER_MIN);
time_t  alarmOffTime =  alarmOnTime  + (60 * SECS_PER_MIN);
time_t nextAlarm;

nextAlarm =  previousMidnight(DateTime.now())+ alarmOnTime;

if( DateTime.now() >= nextAlarm  && DateTime.now()  < nextAlarm + alarmOffTime )
   digitalWrite(ledPin), HIGH);
  digitalWrite(ledPin), LOW);

I attempted to put this in a sketch to try as follows:

Code: [Select]

#include <DateTime.h>
int ledPin=13;
#define START_TIME 1239202500 // set this to the Unix start time you want (0 is midnight Jan1 1970 UTC)
//get unix time code from  http://www.onlineconversion.com/unix_time.htm
time_t  alarmOnTime =  (14 * SECS_PER_HOUR) +( 56 * SECS_PER_MIN);  //2:56
time_t  alarmOffTime =  alarmOnTime  + (01 * SECS_PER_MIN);  //one minute
time_t nextAlarm;
void setup(){


void loop(){
 nextAlarm =  previousMidnight(DateTime.now())+ alarmOnTime;

 if( DateTime.now() >= nextAlarm  && DateTime.now()  < nextAlarm + alarmOffTime )
   digitalWrite((ledPin), HIGH);
   digitalWrite((ledPin), LOW);


It turns on the led at the proper time, but it never turns it off. I put in some print statements and can see the times are all doing the right thing, but I cannot figure out why the led is never turned off. Any ideas?


I think some debugging code will help to see what is happening.

I suggest you print the following values:
nextAlarm + alarmOffTime

Perhaps put a delay(1000) in loop so you only print the values every second.

You then want see why the code in the 'else' statement doesn't get called.


Yes. The second test in the if statement needs to be previousMidnight(DateTime.now()) + alarmOffTime and
everything works. Thanks.

Go Up