DS1307 timer with relay

Hello i need some help to improve my sketch using ds1307 and relays,

The problem was it doesnt turn on in between the set time.

#include “Wire.h”
#define DS1307_ADDRESS 0x68

#define RELAY_ON 0 // 0 means active low relay
#define RELAY_OFF 1

#define Relay_1 2 // Arduino Digital I/O pin number
#define Relay_2 3
#define Relay_3 4
#define Relay_4 5
#define Relay_5 6
#define Relay_6 7
#define Relay_7 8
#define Relay_8 9
/-----( Declare objects )-----/

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

//-------( Initialize Pins so relays are inactive at reset)----
digitalWrite(Relay_1, RELAY_OFF);
digitalWrite(Relay_2, RELAY_OFF);
digitalWrite(Relay_3, RELAY_OFF);
digitalWrite(Relay_4, RELAY_OFF);
digitalWrite(Relay_5, RELAY_OFF);
digitalWrite(Relay_6, RELAY_OFF);
digitalWrite(Relay_7, RELAY_OFF);
digitalWrite(Relay_8, RELAY_OFF);

//—( THEN set pins as outputs )----
pinMode(Relay_1, OUTPUT);
pinMode(Relay_2, OUTPUT);
pinMode(Relay_3, OUTPUT);
pinMode(Relay_4, OUTPUT);
pinMode(Relay_5, OUTPUT);
pinMode(Relay_6, OUTPUT);
pinMode(Relay_7, OUTPUT);
pinMode(Relay_8, OUTPUT);
// delay(4000); //Check that all relays are inactive at Reset

}

void loop(){
printDate();
delay(1000);
}

byte bcdToDec(byte val) {
// Convert binary coded decimal to normal decimal numbers
return ( (val/16*10) + (val%16) );
}

void printDate(){

// Reset the register pointer
Wire.beginTransmission(DS1307_ADDRESS);

byte zero = 0x00;
Wire.write(zero);
Wire.endTransmission();

Wire.requestFrom(DS1307_ADDRESS, 7);

int second = bcdToDec(Wire.read());
int minute = bcdToDec(Wire.read());
int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
int weekDay = bcdToDec(Wire.read()); //0-6 → sunday - Saturday
int monthDay = bcdToDec(Wire.read());
int month = bcdToDec(Wire.read());
int year = bcdToDec(Wire.read());

//print the date EG 3/1/11 23:59:59
Serial.print(month);
Serial.print("/");
Serial.print(monthDay);
Serial.print("/");
Serial.print(year);
Serial.print(" “);
Serial.print(hour);
Serial.print(”:");
Serial.print(minute);
Serial.print(":");
Serial.println(second);

// Relay 1
if((hour == 13 )&& (minute == 32))
digitalWrite(Relay_1, RELAY_ON);// set the Relay ON

THE PROBLEM IS IT WILL NOT TURN ON IN BETWEEN OF 13:33 AND 18:33 IF YOU POWER UP THE ARDUINO IN BETWEEN THOSE TIME(IE. POWER FAILURE). I NEED TO WAIT UNTIL 13:32

if((hour == 18) && (minute == 34))
digitalWrite(Relay_1, RELAY_OFF);// set the Relay OFF

// Relay 2
if((hour == 13) && (minute == 33))
digitalWrite(Relay_2, RELAY_ON);// set the Relay ON
if((hour == 13) && (minute == 35))
digitalWrite(Relay_2, RELAY_OFF);// set the Relay OFF

// Relay 3
if((hour == 13) && (minute == 25))
digitalWrite(Relay_3, RELAY_ON);// set the Relay ON
if((hour == 13) && (minute == 28))
digitalWrite(Relay_3, RELAY_OFF);// set the Relay OFF

// Relay 4
if((hour == 21) && (minute == 54))
digitalWrite(Relay_4, RELAY_ON);// set the Relay ON
if((hour == 21) && (minute == 55))
digitalWrite(Relay_4, RELAY_OFF);// set the Relay OFF

ANOTHER HELP. I WANT TO TURN OFF THE RELAY 2,3 AND 4 IF THEY ARE ON MOMENTARILY FOR 30 MINS IF IM AM GOING TO PRESS PIN 1 (INTRODUCE NEW PIN ASSIGNMENT)

// Relay 5
if((hour == 21) && (minute == 56))
digitalWrite(Relay_5, RELAY_ON);// set the Relay ON
if((hour == 21) && (minute == 57))
digitalWrite(Relay_5, RELAY_OFF);// set the Relay OFF

// Relay 6
if((hour == 21) && (minute == 56))
digitalWrite(Relay_6, RELAY_ON);// set the Relay ON
if((hour == 21) && (minute == 57))
digitalWrite(Relay_6, RELAY_OFF);// set the Relay OFF

MY FUTURE PLAN IS TO CHANGE THIS RELAY 7 AND 8 WITH A TRANSISTOR WITH LED DIMMING CAPABILITIES…

// Relay 7
if((hour == 21) && (minute == 56))
digitalWrite(Relay_7, RELAY_ON);// set the Relay ON
if((hour == 21) && (minute == 57))
digitalWrite(Relay_7, RELAY_OFF);// set the Relay OFF

// Relay 8
if((hour == 21) && (minute == 56))
digitalWrite(Relay_8, RELAY_ON);// set the Relay ON
if((hour == 21) && (minute == 57))
digitalWrite(Relay_8, RELAY_OFF);// set the Relay OFF

}

ANY HELP WILL BE HIGHLY APPRECIATED

active_low_relay_witth_working1.ino (3.72 KB)

Please edit your post and put the code inside code tags. You can fix your problem by testing the time variables with inequalities instead of equalities.

Also please move the shouting comments that aren't C comments out of the code. It makes it non-working code.

Not familiar with the RTC, however it would be near impossible to test for a range in time. Does the RTC have a running minutes (or seconds) function?

My initial thought on testing for a range in time would be something like this:

int totalMinutes = (hour * 60) + minute;
if ((totalMinutes >= 812) && (totalMinutes <= 1113)) {  // true from 13:32 to 18:33
  digitalWrite(Relay_1, RELAY_ON);// set the Relay ON
}

dlloyd: Not familiar with the RTC, however it would be near impossible to test for a range in time. Does the RTC have a running minutes (or seconds) function?

No, it does not. The easiest approach is to use the time library, and do all the time calculations in Unix time.

f1manoy:
ANY HELP WILL BE HIGHLY APPRECIATED

A couple of weeks ago I provided an example program in the German forum.
Code is fully commented in English.

Main reason was to develop “automatic Daylight Saving Time” (summertime) switching for the local time automatically, because this is not really good supported by existing libraries. DST date calculation is currently available for European countries and for East Australia.

But the program can also be used to run your RTC in “zone time” only.

The example program includes a “1-channel timer switch with unlimited daily swithing times” for demonstration, you just need to add the switching times as you need it.

The example also supports an 16x2 text LCD display to display date/time and switching state.

Sketch included.
No installation needed, no third-party libraries needed.
Just unpack the folder from the ZIP file into your Arduino sketch folder.
RTCs supported: DS1307 and DS3231

Please let me know if you have any trouble or if you need a different DST algarithm when living outside Europe and outside East Australia (UTC+10), where currently no DST algorithm is available with my code.

P.S.: If you need an 8-channel timer switch instead of 1-channel timer switch, the program will surely need some small changes in the “switchTimes” array and in the switching logic.

dateTimeLibEasyDemo.zip (5.45 KB)