hello
I am attaching a code that works after rtc time in which I cannot determine the operation as a blink between relay 1 and 2
//////////////////////////////////////////////////////////////
// Real Time Clock with Displayed on I2C 20x4 LCD
// Parts
// 1 x Arduino UNO
// 1 x 2004 I2C LCD
// 1 x DS3231 RTC Module
// RTC and LCD both use Pins A4 & A5
//***************************************************************
#include <Wire.h> //WireLibrary comes with arduino
#include <LiquidCrystal_I2C.h> //i2c LCD Library
#include <DS3231.h>
int Relay1 = 4;
int Relay2 = 5;
DS3231 rtc(SDA, SCL);
Time t;
const int OnHour = 19; //SET TIME TO ON RELAY (24 HOUR FORMAT)
const int OnMin = 23;
const int OffHour = 19; //SET TIME TO OFF RELAY
const int OffMin = 24;
#define DS3231_I2C_ADDRESS 0x68 //RTC Address
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
//***********************************************************
void setup()
{
Wire.begin(); //initialise the TWI
lcd.begin(20, 4); //20 x 4 LCD (Change if required)
for (int i = 0; i < 0; i++) //Flash LCD backlight 2 times on startup
{
lcd.backlight();
delay(250);
lcd.noBacklight();
delay(250);
}
lcd.backlight(); // finish with backlight on
lcd.setCursor(0, 0); //Start at character 0 on line 0
lcd.print("URA: DATUM:");
Serial.begin(115200);
rtc.begin();
pinMode(Relay1, OUTPUT);
pinMode(Relay2, OUTPUT);
digitalWrite(Relay1, HIGH);
digitalWrite(Relay2, HIGH);
}
//***********************************************************
void loop()
{
displayTime(); //calls the display time function
t = rtc.getTime();
Serial.print(t.hour);
Serial.print(" hour(s), ");
Serial.print(t.min);
Serial.print(" minute(s)");
Serial.println(" ");
delay (1000);
if(t.hour == OnHour && t.min == OnMin){
digitalWrite(Relay1,LOW);
Serial.println("LIGHT ON");
digitalWrite(Relay1,LOW);
Serial.println("LIGHT ON");
}
else if(t.hour == OffHour && t.min == OffMin){
digitalWrite(Relay1,HIGH);
Serial.println("LIGHT OFF");
digitalWrite(Relay1,HIGH);
Serial.println("LIGHT OFF");
}
}
//*******************************************************************
byte decToBcd(byte val)//used when sending time
{
return ( (val / 10 * 16) + (val % 10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return ( (val / 16 * 10) + (val % 16) );
}
//**********************************************************************
void readDS3231time(byte *second, byte *minute, byte *hour, byte *dayOfWeek, byte *dayOfMonth, byte *month, byte *year)
{
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set DS3231 register pointer to 00h
Wire.endTransmission();
Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
// request seven bytes of data from DS3231 starting from register 00h
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f);
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
//****************************************************************************
void displayTime()
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
&year);
if (hour < 10)
{
lcd.setCursor(0, 1);
lcd.print("0");
lcd.setCursor(1, 1);
lcd.print(hour , DEC);
}
if (hour > 9)
{
lcd.setCursor(0, 1);
lcd.print(hour , DEC);
}
lcd.setCursor(2, 1);
lcd.print(":");
if (minute < 10)
{
lcd.setCursor(3, 1);
lcd.print("0");
lcd.setCursor(4, 1);
lcd.print(minute , DEC);
}
if (minute > 9)
{
lcd.setCursor(3, 1);
lcd.print(minute , DEC);
}
lcd.setCursor(5, 1);
lcd.print(":");
if (second < 10)
{
lcd.setCursor(6, 1);
lcd.print("0");
lcd.setCursor(7, 1);
lcd.print(second, DEC);
}
if (second > 9)
{
lcd.setCursor(6, 1);
lcd.print(second, DEC);
}
if (dayOfMonth < 10)
{
lcd.setCursor(9, 1);
lcd.print("0");
lcd.setCursor(10, 1);
lcd.print(dayOfMonth, DEC);
lcd.setCursor(11, 1);
lcd.print("/");
}
if (dayOfMonth > 9)
{
lcd.setCursor(9, 1);
lcd.print(dayOfMonth, DEC);
lcd.setCursor(11, 1);
lcd.print("/");
}
if (month < 10)
{
lcd.setCursor(12, 1);
lcd.print("0");
lcd.setCursor(13, 1);
lcd.print(month, DEC);
lcd.setCursor(14, 1);
lcd.print("/");
}
if (month > 9)
{
lcd.setCursor(12, 1);
lcd.print(month, DEC);
lcd.setCursor(14, 1);
lcd.print("/");
}
lcd.setCursor(15, 1);
lcd.print(year, DEC);
switch (dayOfWeek) {
case 1:
lcd.setCursor(0, 2);
lcd.print("Today is Sunday");
break;
case 2:
lcd.setCursor(0, 2);
lcd.print("Today is Monday");
break;
case 3:
lcd.setCursor(0, 2);
lcd.print("Today is Tuesday");
break;
case 4:
lcd.setCursor(0, 2);
lcd.print("Today is Wednesday");
break;
case 5:
lcd.setCursor(0, 2);
lcd.print("Today is Thursday");
break;
case 6:
lcd.setCursor(0, 2);
lcd.print("Today is Friday");
break;
case 7:
lcd.setCursor(0, 2);
lcd.print("Today is Saturday");
break;
}
}