help with DS3231 and relay module

Hello,
I want to simulate day and night light using a relay module and a DS3231 for a greenhouse.
The problem is when the power goes out, the relay doesn’t work unit arrives at the next specific hour.
How can I say for example turn the light on from 20-6 and turn it off from 6-20?
Thank you.

#include <Wire.h>
#include "RTClib.h"
RTC_DS3231 rtc;
int relay = 3;

void setup () {
    
    Serial.begin(9600);  // Baud Rate
    Wire.begin();
    rtc.begin();
    pinMode(relay, OUTPUT);
  // Relay acts as Output
    
}

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(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();

    Serial.println();
    
    delay(1000);
    
   
       
 if (now.hour() == 19 &&  now.minute()== 00) {
    digitalWrite (relay, LOW);
    Serial.print("Day");
    }
      
if (now.hour() == 6 &&  now.minute()== 00) {
    digitalWrite (relay, HIGH);
    Serial.print("Night");
    }
   
}

Try this:

 if (now.hour() >= 19 &&  now.minute()>= 00) {

resp.

if (now.hour() >= 6 &&  now.minute()>= 00) {

You have to check the range of values that correspond to “night” or else it is “day”

#include <Wire.h>
#include "RTClib.h"
RTC_DS3231 rtc;
int relay = 3;

void setup () {

  Serial.begin(9600);  // Baud Rate
  Wire.begin();
  rtc.begin();
  pinMode(relay, OUTPUT);
  // Relay acts as Output

}

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(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println();

  delay(1000);
  if (now.hour() < 6 or now.hour() >= 20) {
    digitalWrite (relay, HIGH);
    Serial.println("night");
  }
  else {
    digitalWrite (relay, LOW);
    Serial.println("Day");
  }
}

Please do not post the same question in multiple forums. It wastes people's time

Railroader:
Try this:

 if (now.hour() >= 19 &&  now.minute()>= 00) {

resp.

if (now.hour() >= 6 &&  now.minute()>= 00) {

That will not work. if hour happens to be 21, both conditions would evaluate true so both would execute but the result would be the second one (daytime) would linger since it comes afterwards

@ghaemiart

TOPIC MERGED.

Could you take a few moments to Learn How To Use The Forum.
Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum.

blh64:
That will not work. if hour happens to be 21, both conditions would evaluate true so both would execute but the result would be the second one (daytime) would linger since it comes afterwards

blh64:
That will not work. if hour happens to be 21, both conditions would evaluate true so both would execute but the result would be the second one (daytime) would linger since it comes afterwards

Correct. Just to give OP a hint.

if (now.hour() >= 6 && now.hour()< 19) daytime();
else
  nighTime()

Railroader:
Correct. Just to give OP a hint.

if (now.hour() >= 6 && now.hour()< 19) daytime();

else
  nighTime()

I think this is the right code that will work.
thank you.

Thanks!
I used a similiar piece of code to handle the change from "winter time" to "summer time" here in Europe.
That used the month data. No big difference.

this code will recognize 6 AM to 7 PM as the day time.

if (now.hour() >= 6 && now.hour()< 19) daytime();
else
  nighTime()

but I wonder when I change 19 to 00 and want to say the day is from 6 Am to 12 PM it will confuse.

if (now.hour() >= 6 && now.hour()< 00) daytime();
else
  nighTime()

is this because 00 is less than 6? so how should I show 12 PM? is it correct to say 24 instead of 00?

Go for 11 hour and 59 minutes, PM!

Railroader:
Go for 11 hour and 59 minutes, PM!

thank you…

Or you could flip the checks around so:

if (now.hour() >= 0 && now.hour() < 6) {
  nighTime();
} else {
  dayTime();
}

Night time would then be between 0:00 and 5:59, and daytime from 06:00 to 23:59.

markd833:
Or you could flip the checks around so:

if (now.hour() >= 0 && now.hour() < 6) {

nighTime();
} else {
  dayTime();
}



Night time would then be between 0:00 and 5:59, and daytime from 06:00 to 23:59.

Also a good, working approach!

markd833:
Or you could flip the checks around so:

if (now.hour() >= 0 && now.hour() < 6) {

nighTime();
} else {
 dayTime();
}



Night time would then be between 0:00 and 5:59, and daytime from 06:00 to 23:59.

very nice. exactly what I need. thanks

You realize that now.hour() will always be greater than or equal to 0 so the first condition will always be true and can be reduced to

ghaemiart:
very nice. exactly what I need. thanks

You realize that now.hour() will always be greater than or equal to 0 so the first condition will always be true and can be reduced to

if (now.hour() < 6) {
  nighTime();
} else {
  dayTime();
}