If won't activate from RTC Time

I am trying to make the relay turn on and off when the RTC display certain times.

This is the piece of code that is problematic.

///// RTC CODE /////

  
  DateTime now = rtc.now();
  Wire.beginTransmission(0x68);
  Wire.write(0);
  Wire.endTransmission();
  Wire.requestFrom(0x68, 8);
  byte second = Wire.read();
  byte minute = Wire.read();
  byte hour = Wire.read();
  byte wkDay = Wire.read();
  byte day = Wire.read();
  byte month = Wire.read();
  byte year = Wire.read();
  byte ctrl = Wire.read();

  lcd.setCursor(0, 0);
  if (day < 10)
    lcd.print("0"); 
  lcd.print(day, HEX); 
  lcd.print("/"); 
  if (month < 10)
    lcd.print("0"); 
  lcd.print(month, HEX); 
  lcd.print("/"); 
  lcd.print("20"); 
  if (year < 10)
    lcd.print("0"); 
  lcd.println(year, HEX); 
  lcd.println();

  lcd.setCursor(0, 1);
  if (hour < 10)
    lcd.print("0"); 
  lcd.print(hour, HEX); 
  lcd.print(":"); 
  if (minute < 10)
    lcd.print("0"); 
  lcd.print(minute, HEX); 
  lcd.print(":"); 
  if (second < 10)
    lcd.print("0"); 
  lcd.println(second, HEX); 
  delay(500);


if((hour, HEX) == OnRelayHour && (minute, HEX) == OnRelayMin)
for (y= 0; y <x*60/7; y++)
{ digitalWrite(RELAY_PIN, HIGH);
 delay(rt);
digitalWrite(RELAY_PIN, LOW);
 delay(rt);
 }
 {
  delay(rt);
digitalWrite(RELAY_PIN, LOW);

Here is my full sketch

//YWROBOT
//Compatible with the Arduino IDE 1.0
//Library version:1.1
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <RTClib.h>

LiquidCrystal_I2C lcd(0x27,20,4);  // set the LCD address to 0x27 for a 16 chars and 2 line display
RTC_DS1307 rtc;


// Relay Delay Time //

int rt = 1000;

//On Time and Off Time

 int OnRelayHour = 20;
 int OnRelayMin = 20;

 int OffRelayHour = 20;
 int OffRelayMin = 21;

int x = OffRelayMin - OnRelayMin;
int y = 0;

// End of On and Off

// Relay Pin Allocation
const int RELAY_PIN = 3;

const int pushbtn1 = 9; 
const int pushbtn2 = 8;
const int pushbtn3 = 7;
const int pushbtn4 = 6;
////////////////************* Manual Button Code *********************///////////////////////
const int manualButton = 5;
////////////////************* Manual Button Code *********************///////////////////////

////////////////************* Power On Start Time Code *********************///////////////////////
/*
int hour=12, minute=59, second=50, pis=0, mls=0, lastTime=0, now;
int hourAlarm=0, minuteAlarm=0, secondAlarm=0;
int hourStartTime2=0, minuteStartTime2=0, secondStartTime2=0;
int mode=0;
int flag = 0, flagAlarm = 0;  //indicates 0 = AM, 1 = PM
////////////////************* Power On Start Time Code *********************///////////////////////

int btnstate1; //indicates pushbtn's state
int btnstate2;
int btnstate3;
int btnstate4;

void setup() {
  Wire.begin(); 
  lcd.init();
  lcd.backlight();
  lcd.begin(16, 2);
  pinMode(pushbtn2, INPUT);
  pinMode(pushbtn3, INPUT);
  pinMode(pushbtn4, INPUT);
  pinMode(pushbtn1, INPUT);
  ////////////////************* Manual Button Code *********************///////////////////////
  pinMode(manualButton, INPUT);
  pinMode(13, OUTPUT);
////////////////************* Manual Button Code *********************///////////////////////
  Serial.begin(9600);


}

void loop(){
  /*
  if(mode==0)clock();
  else if(mode==1)setTime();
  else if(mode==2)setAlarm();
  else if(mode==3)setTimer();
  else if(mode==4)setAlarm();
*/
////////////////************* Manual Button Code *********************///////////////////////
digitalWrite(3, digitalRead(5));
////////////////************* Manual Button Code *********************///////////////////////


///// RTC CODE /////

  
  DateTime now = rtc.now();
  Wire.beginTransmission(0x68);
  Wire.write(0);
  Wire.endTransmission();
  Wire.requestFrom(0x68, 8);
  byte second = Wire.read();
  byte minute = Wire.read();
  byte hour = Wire.read();
  byte wkDay = Wire.read();
  byte day = Wire.read();
  byte month = Wire.read();
  byte year = Wire.read();
  byte ctrl = Wire.read();

  lcd.setCursor(0, 0);
  if (day < 10)
    lcd.print("0"); 
  lcd.print(day, HEX); 
  lcd.print("/"); 
  if (month < 10)
    lcd.print("0"); 
  lcd.print(month, HEX); 
  lcd.print("/"); 
  lcd.print("20"); 
  if (year < 10)
    lcd.print("0"); 
  lcd.println(year, HEX); 
  lcd.println();

  lcd.setCursor(0, 1);
  if (hour < 10)
    lcd.print("0"); 
  lcd.print(hour, HEX); 
  lcd.print(":"); 
  if (minute < 10)
    lcd.print("0"); 
  lcd.print(minute, HEX); 
  lcd.print(":"); 
  if (second < 10)
    lcd.print("0"); 
  lcd.println(second, HEX); 
  delay(500);


if((hour, HEX) == OnRelayHour && (minute, HEX) == OnRelayMin)
for (y= 0; y <x*60/7; y++)
{ digitalWrite(RELAY_PIN, HIGH);
 delay(rt);
digitalWrite(RELAY_PIN, LOW);
 delay(rt);
 }
 {
  delay(rt);
digitalWrite(RELAY_PIN, LOW);


}
}




  ///// RTC END CODE ///////

I also realise that millis() is probably better to use however I am having difficulties implementing this successful into my code.

Thanks for any help.

Oops

Interesting syntax ...

Your topic was MOVED to its current forum category as it is more suitable than the original

if((hour, HEX) == OnRelayHour && (minute, HEX) == OnRelayMin)

Why have you used the HEX qualifier here ?

When you read data from ds1307/3231 RTC like this, the data is stored in what's called "binary coded decimal" (BCD) format. That's like hexadecimal except that the "A"-"F" digits are never used. So after "09" comes "10" but that's not decimal 10, it's hex 0x10, which is 16 in decimal. That's why it's printed to LCD or serial monitor like this:

lcd.println(second, HEX);

because printing it as though it was a hex number makes it appear to be a decimal number. The 16 appears as "10" which is what you want.

When you want to compare the current time with a specific time to take some action, you can use the same trick. So instead of

int OnRelayHour = 20;

meaning 8pm, you can use

int OnRelayHour = 0x20;

Doing it like this means the number is already in BCD, ready to be compared with the hour value read from the RTC, and you can check like this:

if(hour == OnRelayHour && minute  == OnRelayMin)
2 Likes

It was the only syntax Arduino IDE would accept. Clearly incorrect but I am not sure how to fix.

No.

This has worked.

Thanks for sharing your knowledge and explaination.

My next step is to use Millis however this is proving to be more difficult.

With the simulation the LCD is freezing while the relay is functioning.

https://wokwi.com/projects/337713813095711315

I see a for-loop with delays in it. Where is your attempt with millis()?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.