RTC and relay active during hours

i’m having difficulties with setting lights to be active during 8:30-22:00 , and off at other time.
The problem with my code is- if there is a power failure the arduino will wait until the specified time to perform an action.
how can i set the code to check the time interval?

https://hastebin.com/ducebaheha.http

i’ve tried this, but now the problem is it will check the statement on every hour on 30 minutes, so it will turn off relay on 22:30

if (now.hour() >= 8 && now.minute() == 30 && now.hour() <= 22)
{
Serial.println (" lights on");
digitalWrite (LIGHTS1_DIGITAL_PIN, LOW);
}
if (now.hour() >= 22 && now.hour() <= 8 && now.minute() == 30)
{

Serial.println (“lights off”);
digitalWrite (LIGHTS1_DIGITAL_PIN, HIGH);
}

}

I’ve tried googling my problem but no exact results, also my limited knownledge of code prevents me to understand my error :frowning: If i’m correct- i should try to use dailytime.h library?

It would be perfect if you could provide a link to solved problem or similiar project.

Thank you

You could write to the EEPROM when turning the lights on or off and check when restarting whether they should currently be on or off. Each EEPROM location has a lifetime of at least 100,000 writes so unless you are going to run the system for several years, that should cause no problems.

(now.hour() >= 22 && now.hour() <= 8

?? Is this possible?

UKHeliBob:
You could write to the EEPROM when turning the lights on or off and check when restarting whether they should currently be on or off. Each EEPROM location has a lifetime of at least 100,000 writes so unless you are going to run the system for several years, that should cause no problems.

is this correct?

if (now.hour() == 8 && now.minute() == 30 )
{
Serial.println (" lights on");
digitalWrite (LIGHTS1_DIGITAL_PIN, LOW);
EEPROM.put(RELAY_ADDR, LIGHTS1_DIGITAL_PIN);
}

if (now.hour() == 22)
{

Serial.println ("lights off");
digitalWrite (LIGHTS1_DIGITAL_PIN, HIGH);
EEPROM.put(RELAY_ADDR, LIGHTS1_DIGITAL_PIN);
}

}

EEPROM.put(RELAY_ADDR, LIGHTS1_DIGITAL_PIN);That would write the pin number to the EEPROM. You need to write the pin state to the EEPROM and you should only do it once per change of state.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

TomGeorge:
Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks, Tom :slight_smile:

The code

Thank you, Tom.

The code looks like this:

#include <EEPROM.h>
#include <Wire.h>
#include "RTClib.h"
#include <TimeAlarms.h>
#include <DailyTimer.h>

#define LIGHTS1_DIGITAL_PIN 10

RTC_DS1307 rtc;

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

void setup () {
  Serial.begin(57600);
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
     rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }
  EEPROM.get (RELAY_ADDR, LIGHTS1_DIGITAL_PIN)
}

void loop () {
    DateTime now = rtc.now();
   
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(" (");
    Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.print(") ");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
   
    Serial.print(" since midnight 1/1/1970 = ");
    Serial.print(now.unixtime());
    Serial.print("s = ");
    Serial.print(now.unixtime() / 86400L);
    Serial.println("d");
   
    // calculate a date which is 7 days and 30 seconds into the future
    DateTime future (now + TimeSpan(7,12,30,6));
   
    Serial.print(" now + 7d + 30s: ");
    Serial.print(future.year(), DEC);
    Serial.print('/');
    Serial.print(future.month(), DEC);
    Serial.print('/');
    Serial.print(future.day(), DEC);
    Serial.print(' ');
    Serial.print(future.hour(), DEC);
    Serial.print(':');
    Serial.print(future.minute(), DEC);
    Serial.print(':');
    Serial.print(future.second(), DEC);
    Serial.println();
   
    Serial.println();
    delay(3000);


  if (now.hour() == 8 && now.minute() == 30 ) 
{
  Serial.println (" lights on");
  digitalWrite (LIGHTS1_DIGITAL_PIN, LOW);
  EEPROM.put(RELAY_ADDR, LIGHTS1_DIGITAL_PIN);
}

if (now.hour() == 22)
{
  
  Serial.println ("lights off");
  digitalWrite (LIGHTS1_DIGITAL_PIN, HIGH);
  EEPROM.put(RELAY_ADDR, LIGHTS1_DIGITAL_PIN);
}

}

the circuit is quiet simple
I have a RTC DS1307 , a relay switch, and arduino uno.

So currently it works, when its 8 30 it will turn on and exactly at 22 it turns off. How should i write the code in case of power outage or sth unexpected?

Something like:

int pmMins = hours * 60 + minutes;
if(pmMins >= 510 && pmMins <= 1320) // 510 = 8:30, 1320 = 22:00
  ligntsON;
else
  lightsOFF;

The easiest way to handle this type of thing is to just work in minutes or seconds. Convert all your time values in code to minutes past midnight so you only have one number to deal with. Then it is trivial to see if the current time is greater than this number and less than that.

outsider:
Something like:

int pmMins = hours * 60 + minutes;

if(pmMins >= 510 && pmMins <= 1320) // 510 = 8:30, 1320 = 22:00
  ligntsON;
else
  lightsOFF;

The solution was so simple, but yet so complicated for me :smiley: Thank you. Oh what joy :smiley:

Hi,
I why use EPROM, if the RTC is battery backed up, when the code restarts don’t you check if you are in or out of the time interval and switch the lights ON or keep them OFF.

Or have I missed something?

Tom… :slight_smile:

TomGeorge:
Hi,
I why use EPROM, if the RTC is battery backed up, when the code restarts don't you check if you are in or out of the time interval and switch the lights ON or keep them OFF.

Or have I missed something?

Tom..... :slight_smile:

Tom, my suggestion to use the EEPROM was a quick fix that would involve not changing most of the code and getting into the intricacies of checking whether the current time is between two other times which the original code does not do.

UKHeliBob:
Tom, my suggestion to use the EEPROM was a quick fix that would involve not changing most of the code and getting into the intricacies of checking whether the current time is between two other times which the original code does not do.

Determining whether or not a number is in a range is more “intricate” than proper use of EEPROM?
That’s news to me.

outsider:

int pmMins = hours * 60 + minutes;

if(pmMins >= 510 && pmMins <= 1320) // 510 = 8:30, 1320 = 22:00
  ligntsON;
else
  lightsOFF;

You could even do:

int pmMins = hours * 100 + minutes;
if(pmMins >= 830 && pmMins <= 2200)
  lightsON;
else
  lightsOFF;

because we really don’t care about the exact number of minutes between times; we just want to know whether one time is earlier or later than another.

Determining whether or not a number is in a range is more "intricate" than proper use of EEPROM?
That's news to me.

News to you perhaps but maybe not for the OP. He had a working program that he wanted to add a feature to and my suggestion to use EEPROM was made in that light. The intricacy, such as it is, is not in determining whether a number is in a range but in changing the program to do it.

The thing that odometers code does is just set the lights ON or OFF every time through the loop.

This neatly solves the problem.

Lights that are on don't mind being turned on. Lights that are off don't mind being turned off.

Perhaps he'd want to minimise the printing that goes along with setting the relay, that's a different mattter.

I'm following along because I want to detect and compensate for a power failure that causes my automatic (well it will be one day) cat feeder to miss a feeding…

a7

alto777:
I'm following along because I want to detect and compensate for a power failure that causes my automatic (well it will be one day) cat feeder to miss a feeding…

For that, you might want to use EEPROM to keep track of when the cat was last fed.

How long a power outage are you concerned about? Here in the northeast U.S., it is by no means impossible for the power to be out for days at a time. It doesn't happen very often, but it happens often enough that you might want a backup power supply for your feeder.

That's an excellent suggestion. A backup power supply is a better place to put the solution.

Please be assured that it will not be days at a time the responsibility of my feeder to take care of my cat! But I did want to have a good chance of eliminating failure. Riding out little and not so small outrages is def the way to go.

Not for this forum, but I have to say I'll also have to outwit the cat. He has shown great intelligence and creativity when it comes to getting more than his share. Of everything.

a7

alto777:
Not for this forum, but I have to say I'll also have to outwit the cat. He has shown great intelligence and creativity when it comes to getting more than his share. Of everything.

If you weren't satisfied with what you were getting, and you had all the time in the world to think, I'm sure that you would come up with an idea or two.

alto777:
Not for this forum, but I have to say I'll also have to outwit the cat. He has shown great intelligence and creativity when it comes to getting more than his share. Of everything.

Like dogs, they have tons of spare time to workout us humans and how to make the most of us... :o :o

Tom... :slight_smile: