Pages: [1]   Go Down
Author Topic: DateTime question  (Read 511 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 38
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Mem:
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:
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);
else
   digitalWrite(ledPin), LOW);

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

Code:
#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(){
  Serial.begin(9600);
  pinMode(ledPin,OUTPUT);
  DateTime.sync(START_TIME);

}

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

  if( DateTime.now() >= nextAlarm  && DateTime.now()  < nextAlarm + alarmOffTime )
    digitalWrite((ledPin), HIGH);
  else
    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?

Logged

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I suggest you print the following values:
 DateTime.now()
 nextAlarm
 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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 38
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: