# Seconds to hours with datetime.h

Okay, I'm new to arduino I have done the basic stuff, with an LCD. I have setup a relay to control a light, I would like to have that light come on at a certain hour. I can tell it to turn on with a delay and turn off. Seconds to hours or seconds to minutes to hours etc. I'm not sure how to go about this, the arduino will be on 24/7 powered by a wall adapter. Any help would be great. The lcd just displays light is on/ light is off No problems with basic code.

edit I looked at Timealarm is what I want to do, I'm surprised no one had that remark to try it :P

So basically you want the current time?

Arduino's timer isn't very accurate. So, if you want to have a reliable timer, get a RTC chip to go along. So, the basics: 1 second = 1000 mseconds 1 minute = 60 seconds 1 hour = 60 minutes = 3600 seconds. 1 day = 24 hours

But you allready knew that, right? So all you have to do, is write a program that will count this time variables. For example, I wrote this small example some time ago (before finding the time library)

``````int _SECOND=0;
int _MINUTE=0;
int _HOUR=0;
long _timer_start=0;
long _timer_interval=1000;

//long _timer_start2=0;
//long _timer_interval2=5000;

void setup()
{
Serial.begin(9600);
}

void loop()
{
simple_clock();

/*
if (millis() - _timer_start2 > (_timer_interval2))
{
Serial.print(_HOUR);
Serial.print(":");
Serial.print(_MINUTE);
Serial.print(":");
Serial.println(_SECOND);
_timer_start2=millis();
}
*/
}

void simple_clock()
{
/*
/////////////////////
A simple timer function
////////////////////
*/
if (millis() - _timer_start > _timer_interval)
{
Serial.print(_HOUR);
Serial.print(":");
Serial.print(_MINUTE);
Serial.print(":");
Serial.println(_SECOND);

_SECOND+=1;
_timer_start=millis();

}

if(_SECOND>=60)
{
_SECOND=0;
_MINUTE+=1;
}
if(_MINUTE>=60)
{
_MINUTE=0;
_HOUR+=1;
}

if(_HOUR>=24)
{
_HOUR=0;
}
}
``````

@kryptek, good to hear you found the timeAlarm library. The TimeAlarm example sketch in the time library download should be close to what you want to do.

@Odisej, the biggest advantage of an RTC is that it keeps time when the arduino board is switched off. A standard Arduino board is quite accurate, as are clones that use a crystal for the system clock.

btw, the code posted above can lose time because it does not take into account delays that occur between the time an exact second has elapsed and the call to simple_Clock to recalculate the next timer interval. Small delays will accumulate over time and a delay of more than a second betwen calls will not be handled correctly.