Help with time alarms

Hi, im new to Arduino and im doing some testings with the sketch that its attached and its working great. The problem is that i can only set alarms based on hours.

// Hours that the light is switched on and off
int LightOnHour = 18;
int LightOffHour = 14;

// Create alarms that turn on and off the light
Alarm.alarmRepeat(LightOnHour,00,0, LightOnAlarm); // 8:00pm every day
Alarm.alarmRepeat(LightOffHour,00,0,LightOffAlarm); // 11:00am every day

// If the light is supposed to be on turn it on
if(hour()<LightOffHour || hour()>=LightOnHour)
{
digitalWrite(LIGHT_PIN, HIGH);
lightState = true;
}

How can i make this code work with more specific time, so i can set hours, minutes ,and seconds?

testA.ino (11.5 KB)

Im not sure sir, cause im completely new to arduino, i found this sketch ready. i suppose is to set minutes, but what about:

"if(hour()<LightOffHour || hour()>=LightOnHour)"

or

"int LightOnHour = 18;"

It seems like working only with hours and i cannot find a way to make it work also with minutes and seconds.

I know its an easy question but can some give me an example? how can i set it to 18:30?

int LightOnHour = 18;
int LightOffHour = 14;

  // Create alarms that turn on and off the light
  Alarm.alarmRepeat(LightOnHour,00,0, LightOnAlarm);  // 8:00pm every day
  Alarm.alarmRepeat(LightOffHour,00,0,LightOffAlarm);  // 11:00am every day

'18' is really 6:00pm and '14' is really 2:00pm, too, so the lights will be on for 20 hours per day, and those comments are inaccurate.

"LightOnAlarm" and "LightOffAlarm" are 'callback' functions that you define to determine what happens at the time you set in the 'alarmRepeat()' function.

You shouldn't actually need to do this, as I understand it:-

if(hour() == 6 && minute() == 10 && second() == 45){
    // will be true at 6:10:45

If you wanted to set the 'light on' time for 6:30pm, and the
'light off' time for 11:35pm, you would do this:-

Alarm.alarmRepeat(18,30,0, LightOnAlarm);  // 6:30pm every day
  Alarm.alarmRepeat(23,35,0,LightOffAlarm);  // 11:35pm every day

Then, 'LightOnAlarm()' would be called automatically at 6:30pm, and 'LightOffAlarm()' would be called automatically at 11:35pm. (The remaining zero is for seconds.)

You write those functions, and in them you write the code that you want to execute at those times.

Spend some time carefully reading the library documentation to see what other steps are necessary.
(I've never used this library.)

Thank you! i forgot to change those comments.

I tried to comment out the first part

// Hours that the light is switched on and off
// int LightOnHour = 18;
// int LightOffHour = 14;

In void setup() i change the second part to

Alarm.alarmRepeat(18,30,0, LightOnAlarm); // 18:30 every day
Alarm.alarmRepeat(14,30,0,LightOffAlarm); // 14:30 every day

The third part (also in void setup() ) i think its sets HIGH the relay that im using in the time period outside 14:30 - 18:30 that the relay it stays LOW :

// If the light is supposed to be on turn it on
if(hour()<LightOffHour || hour()>=LightOnHour)
{
digitalWrite(LIGHT_PIN, HIGH);
lightState = true;
}

So in witch way can i change this in the specific example?

This code below is not working the same way, i think it says if the time is equal, not before 14:30 and after 18:30

if(hour() == 6 && minute() == 10 && second() == 45){
// will be true at 6:10:45

if(hour() == 6 && minute() == 10 && second() == 45){
// will be true at 6:10:45

As I said in my last post:-

OldSteve:
You shouldn't actually need to do this, as I understand it

Did you not read my last reply?
Have you thoroughly read the documentation yet?
It answers all of your questions.

As I said in my last reply as well, there are callback functions that are automatically called at the appropriate times.
So instead of the above, you would do something like:-

void LightOnAlarm()
{
    digitalWrite(LIGHT_PIN, HIGH);
    lightState = true;
}

As I say, this would be automatically called. That's why you pass the function name in the 'alarmRepeat()' function.

As I also said, I've never used this library. I found this out by reading part of the documentation, (something you should do, rather than me read it and pass on the information to you).
Perhaps you should take the time to read all of the documentation thoroughly, and maybe even try the example program(s), then try again.

I feel like I completely wasted my time reading the example and posting my last reply.

Delta_G:
OK, do you want to do this with timeAlarms

Going by the code, the inclusion of the "TimeAlarms" library, and with callback function names passed to 'alarmRepeat()', I assumed that he does, despite the fact that he also has this in his code:-

if(hour() == 6 && minute() == 10 && second() == 45){
    // will be true at 6:10:45

Sorry for waisting your time, i have already read the TimeAlarms Library but without this part of code in void setup

if(hour()<LightOffHour || hour()>=LightOnHour)
{
digitalWrite(LIGHT_PIN, HIGH);
lightState = true;
}

the code seems not work correctly,I made some tests and i can explain to you why.

if this code does not exist:

  1. When arduino starts and the time is before 14:30 or after 18:30 the LIGHT_PIN stays LOW but we need to be HIGH

  2. If i just add "digitalWrite(LIGHT_PIN, HIGH);" in void setup for the LIGHT_PIN to be HIGH in case of power failure when arduino starts it will be successfully keep the LIGHT_PIN, HIGH but if arduino starts between 14:30 - 18:30 the LIGHT_PIN will be start in HIGH but we need to be LOW.

So the reason for that code to be there is the case of power failure.

So the question is, in witch way i must replace LightOffHour and LightOnHour to make this code working again?

if(hour()<LightOffHour || hour()>=LightOnHour)

grtester1:
Sorry for waisting your time, i have already read the TimeAlarms Library but without this part of code in void setup

if(hour()<LightOffHour || hour()>=LightOnHour)
{
digitalWrite(LIGHT_PIN, HIGH);
lightState = true;
}

the code seems not work correctly,I made some tests and i can explain to you why.

if this code does not exist:

  1. When arduino starts and the time is before 14:30 or after 18:30 the LIGHT_PIN stays LOW but we need to be HIGH

  2. If i just add "digitalWrite(LIGHT_PIN, HIGH);" in void setup for the LIGHT_PIN to be HIGH in case of power failure when arduino starts it will be successfully keep the LIGHT_PIN, HIGH but if arduino starts between 14:30 - 18:30 the LIGHT_PIN will be start in HIGH but we need to be LOW.

So the reason for that code to be there is the case of power failure.

So the question is, in witch way i must replace LightOffHour and LightOnHour to make this code working again?

if(hour()<LightOffHour || hour()>=LightOnHour)

Right, now I understand. I'm very sorry, I didn't see that your full code was an attachment, rather than posted inline between code tags, and was only going by what I saw in your actual posts.
You only want to do this in setup(), then use the callback functions the rest of the time.

Interesting. Because the light is supposed to be on from 6pm right through until 2pm the following day, I see where your code is failing. You also need to take the date into account. ie The light often must be on when the hour is larger than 'LightOffHour', and when it's smaller than 'LightOnHour'.
I don't have recent experience with using the DS1307 RTC module, (it's been >5 years since I last used one), so can't remember the method of retrieving the date, but you need a more specific algorithm that allows for time AND date.
I hope I've explained it well enough.
I can't spend more time right now, I have to do a few other things, but hopefully that will get you on the right track.
I'll check in again when I can, but in the meantime someone else might be able to help with that code if you have problems writing it.
Keep us posted.