RTC and RELAYS by webaddic

hi all,

i have installed a solar system (for pump) to lower the electric bills.Now everyday i have to go to the pump and switch it from the electric provider to solar and vice versa. What i have done so far is that i have made the hardware and switched relays as they should. But due to load shedding in our part of the world, when the program restart, the relay wont start until the time that is written in the program comes. i cant rewrite the sketch and make it bulky by adding time for every second. is there a command to switch on the relay even if the Arduino restarts in between the times

(e.g if it is 7 am the relay starts and at 7:05 am electricity provider disconnects the power and resumes it after 10min i.e 7:15am. the relay will remain switched off until it is 7pm ((or 19 HRS)as in the program.

Summary

  1. i have two relays one relay switches on from 07-19 HRS. (24 hrs format)
  2. second relay works from 19.01 HRS to 06.59 HRS
  3. program attached below.

in short what i want is that when the electricity discontinued by the the electric company resumes, respective relay starts from that moment onwards and not wait for the program and rtc to strike 7am or 7pm .

(by the way i have installed 18650 battery to the project too but still want to add the command to be on the safe side)

i hope you all understand what i tried to explain.

:slight_smile: thanks in advance

OLED_CLOCK_WITH_RELAY_SETTINGS_FINAL_FOR_RELAY.ino (5.67 KB)

Calculate the current time in seconds from midnight. Note that you'll need a long for this. Calculate your two trigger times in the same way. If the time is greater than one trigger and less than the other, set your relays one way, if not set them the other.

It doesn't matter to the Arduino or the relay if you keep doing digitalWrite to the same state, but you could add flags to tell you whether you've already set the relays into the necessary state.

Thanks wildbill for such a fast reply. Can you tell this the easy way... lol
Maybe with example.
Thanks again

Did u look at the code

25200 seconds from midnight to 7:00am

68400seconds from 7am to 7pm

18000 seconds from 7pm to midnight

25000 seconds from midnight to 7 am

Here's an example:

#include <OLED_I2C.h>   //Connecting a library to the display      
#include <Wire.h>       //For I2C
#include <Time.h>       //For hours
#include <TimeLib.h>    //
#include <Timezone.h>   //
#include <DS1307RTC.h>
#include <DS3231.h>     //For RTC

int Relay1 = 13;  //for 12v led
int Relay2 = 12;  //for 5v led
int Relay3 = 11;  //for 12v relay
int Relay4 = 10;  //for 5v relay
int Buzzer = 9;

DS3231  rtc(SDA, SCL);
Time t;

const int OnHour = 07;     //ON AND OFF HOURS FOR SOLAR RELAY
const int OnMin = 00;
const int OffHour = 19;
const int OffMin = 00;

const unsigned long OnTimeSec =  OnHour * 3600UL + OnMin * 60UL;
const unsigned long OffTimeSec = OffHour * 3600UL + OffMin * 60UL;

void setup()
{
  Serial.begin(115200);
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if (timeStatus() != timeSet)
    Serial.println("Unable to sync with the RTC");
  else
    Serial.println("RTC has set the system time");
  rtc.begin();
  pinMode(Relay1, OUTPUT);  //for 12v led
  pinMode(Relay2, OUTPUT);  //for 5v led
  pinMode(Relay3, OUTPUT);  //for 12v relay
  pinMode(Relay4, OUTPUT);  //for 5v relay
  pinMode(Buzzer, OUTPUT);
  digitalWrite(Relay1, HIGH);
  digitalWrite(Relay2, HIGH);
  digitalWrite(Relay3, HIGH);
  digitalWrite(Relay4, HIGH);
  digitalWrite(Buzzer, LOW);
}

void loop()
{
  //Timer setting for relays
  t = rtc.getTime();                            //to get time for relay switching
  Serial.print(t.hour);
  Serial.print(" hour(s), ");
  Serial.print(t.min);
  Serial.print(" minute(s), ");
  Serial.print(t.sec);
  Serial.print(" second(s)");
  unsigned long TimeSecs = t.hour * 3600UL + t.min * 60UL + t.secs;
  if (TimeSecs >= OnTimeSecs && TimeSecs < OffTimeSecs)
    {
    digitalWrite(Relay4, LOW);
    Serial.println("ON SOLAR");
    }
  else
    {
    digitalWrite(Relay4, HIGH);
    Serial.println("SWITCHING FROM SOLAR TO KE");
    }
}

I couldn't compile it - I must have different library versions to you.

Note that it will spam the hell out of serial because I didn't bother to check the state of the relay before setting it again and sending a message about doing so.

Ok thanks ,,, will try and update asap

got an error while compiling

OLED_CLOCK_WITH_RELAY_SETTINGS_FINAL_FOR_RELAY:31:21: error: redefinition of 'const long unsigned int OnTimeSec'

const unsigned long OnTimeSec = OnHour * 3600UL + OnMin * 60UL;

^~~~~~~~~

C:\Users\Desktop\OLED_CLOCK_WITH_RELAY_SETTINGS_FINAL_FOR_RELAY\OLED_CLOCK_WITH_RELAY_SETTINGS_FINAL_FOR_RELAY.ino:24:21: note: 'const long unsigned int OnTimeSec' previously defined here

const unsigned long OnTimeSec = OnHour * 3600UL + OnMin * 60UL;

^~~~~~~~~

OLED_CLOCK_WITH_RELAY_SETTINGS_FINAL_FOR_RELAY:32:21: error: redefinition of 'const long unsigned int OffTimeSec'

const unsigned long OffTimeSec = OffHour * 3600UL + OffMin * 60UL;

^~~~~~~~~~

C:\Users\Desktop\OLED_CLOCK_WITH_RELAY_SETTINGS_FINAL_FOR_RELAY\OLED_CLOCK_WITH_RELAY_SETTINGS_FINAL_FOR_RELAY.ino:25:21: note: 'const long unsigned int OffTimeSec' previously defined here

const unsigned long OffTimeSec = OffHour * 3600UL + OffMin * 60UL;

^~~~~~~~~~

C:\Users\Desktop\OLED_CLOCK_WITH_RELAY_SETTINGS_FINAL_FOR_RELAY\OLED_CLOCK_WITH_RELAY_SETTINGS_FINAL_FOR_RELAY.ino: In function 'void loop()':

OLED_CLOCK_WITH_RELAY_SETTINGS_FINAL_FOR_RELAY:89:63: error: 'class Time' has no member named 'secs'; did you mean 'sec'?

unsigned long TimeSecs = t.hour * 3600UL + t.min * 60UL + t.secs;

^~~~

sec

OLED_CLOCK_WITH_RELAY_SETTINGS_FINAL_FOR_RELAY:90:19: error: 'OnTimeSecs' was not declared in this scope

if (TimeSecs >= OnTimeSecs && TimeSecs < OffTimeSecs)

^~~~~~~~~~

C:\Users\Desktop\OLED_CLOCK_WITH_RELAY_SETTINGS_FINAL_FOR_RELAY\OLED_CLOCK_WITH_RELAY_SETTINGS_FINAL_FOR_RELAY.ino:90:19: note: suggested alternative: 'OnTimeSec'

if (TimeSecs >= OnTimeSecs && TimeSecs < OffTimeSecs)

^~~~~~~~~~

OnTimeSec

OLED_CLOCK_WITH_RELAY_SETTINGS_FINAL_FOR_RELAY:90:44: error: 'OffTimeSecs' was not declared in this scope

if (TimeSecs >= OnTimeSecs && TimeSecs < OffTimeSecs)

^~~~~~~~~~~

C:\Users\ashak\Desktop\OLED_CLOCK_WITH_RELAY_SETTINGS_FINAL_FOR_RELAY\OLED_CLOCK_WITH_RELAY_SETTINGS_FINAL_FOR_RELAY.ino:90:44: note: suggested alternative: 'OffTimeSec'

if (TimeSecs >= OnTimeSecs && TimeSecs < OffTimeSecs)

^~~~~~~~~~~

OffTimeSec

exit status 1
redefinition of 'const long unsigned int OnTimeSec'

well i tried again with a different sketch this time. it was a hint that i tried to implement in my sketch from the arduino forum itself. Link to the forum is given below.

https://forum.arduino.cc/index.php?topic=552537.0

But the program is not working as it should. the relay that should be on in the morning is active in the night too.... can somebody help me out. below is the copy of my sketch.

thanks in advance

Final_RELAY_SETTINGS.ino (6.12 KB)

I don't understand why your code is so complex. You're checking repeatedly with multiple if statements whether the time is between 7:00 and 19:00 and then checking the opposite.

I suggest that you have one if that checks this. If you're in daytime, set all the things you need for day, else set the things for night operation.

Also, consider putting the buzzer functionality in it's own function and implement it with a for loop, not copy & paste.

Thanks for the reply! i tried to shorten the code and used two if commands, one for the day and one for night. as far as buzzer is concerned i only want it to beep ten times just to indicate that the motor is about to shift from the power supplier to solar. (if the buzzer does not work, that will also not bother me as the hardware will run in the room which is very rarely visited by anyone) .

i connected two leds to pin 10 & 11 on my uno, changed the time, but the leds are always on .... i wonder why. IS it that the (pmMin command is not working.)

wish i had stuck with the old program which did work..... but due to power outage, program restarts and then i have to wait for the next time to switch the relay on or off. i had an option to install a battery but i want to do it within software :frowning:

attaching the program below with some changes

Final_RELAY_SETTINGS2.ino (5.65 KB)

I assume that your times are wacky so you can test.

I still think that you only need a single if. If you're in daytime you need one setup, else it must be night and you need the other. No need to do red and green in separate if statements.

ok let me try again :slight_smile:

how to add solved in the topic?

LOL

THANKS MY FRIEND (WILDBILL) YOUR HELP WAS OF GREAT USE....... FINALLY DID IT.....

THANK YOU VERY VERY MUCH ONCE AGAIN.

I think if you edit the first post in the thread, you can edit the title too.

hi again,

just bumped into another problem .... The timer and the relays are working fine. But i think there is a glitch in the library. the relays are working fine from 10:00 to 23:00 hours. But whenever i set the time from 00:00 till 9:00 hours, the relays stays on the evening mode and does not shift to morning mode.

When i set time from 7:00 am in the morning till 19:00 hours, relay is not on solar mode, while it should be. it stays on power supply mode (k.e in my case) and does not shift the relays .

i checked again by putting time from 10:00 to 19:00 and 19:00 to 10:00 and the relays are working fine.

but when the time is in single digit mode i.e from 00:00 to 9:00am, it deos not work . i tied it with inserting time as 7:00 and 07:00 (with and without a zero before 7) but still the same issue.

how can i correct this ? i am posting the ds3231 library that i have.

please help!

DS3231.zip (379 KB)

What does your latest code look like?

Sorry for a very late replay.

i am attaching the latest sketch and library.

(in reference to the problem in message #14)

DS3231.zip (379 KB)

FINAL_WITHOUT_OLED.zip (1.04 KB)

Most people here (including myself) are not comfortable opening zip files. You can likely post your sketch in-line, it seems small enough.

sorry i didnt know that zip files are not entertained. sending the ino file

FINAL_WITHOUT_OLED.ino (4.68 KB)

A few things: you can't use int to store TimeSecs or the other two variables that are counts of seconds unless the Arduino you're using has 32 bit ints. Use long instead. Better yet unsigned long although it doesn't really matter for a 24 hour time period.

Also, what does your clock code return for hours? is it a twelve or 24 hour clock?

It's probably risky to try and use midnight too. Use 00:01 instead.