Help with using RTC "hour" from DS3231 as an integer value

Hello,
I am currently working on a project to automate heating and ventilation control of a small greenhouse. One issue I am having is working through the control logic for night vs. day. For the same project, some folks use a photocell sensor to judge night vs day, but this seems that it can be a bit troublesome.

My thought was to use the "hour" value from the RTC to define night and day for my code logic would be pretty simple. I am not worried about setting this as a constant even thought length of day changes throughout the year, don't need that much accuracy in the time i will actually be growing.

So how can I use the RTC to set a variable for hours (0-24) to be able to do this?

Here is the code I have been working on, so far it only has very simple logic for fan and vent control but still working on adding more:

// include the library code:
#include <LiquidCrystal.h>    //library for LCD
#include <DHT.h>              //Library for temp sensors
#include <RunningAverage.h>   //Library for calculating temp average vs instantaneous
//#include <Arduino.h>        //
#include <Wire.h>             //
#include <DS3231.h>           //Needed for real time clock


// define inputs and outputs
#define DHTPIN 9              //Internal temp DHT22 sensor
#define DHTPIN2 2             //External temp DHT22 sensor
#define DHTTYPE DHT22         //sets to use correct DHT library
#define DHTTYPE2 DHT22        //sets to use correct DHT library


//set variables
DHT dht (DHTPIN, DHTTYPE);    //sets internal temp/humidity as dht
DHT dhttwo (DHTPIN2, DHTTYPE2); //sets extermal temp/humidity as dhttwo


//code to set up RTC
DS3231 clock;                   //sets up clock
RTCDateTime dt;                 //sets dt as time variable
int Hour;          


//code to setup running average calculation
RunningAverage internalHumidity (5);  //internal hum average
RunningAverage internalTemp(5);       //internal temp average
RunningAverage externalHumidity(5);   //external hum average
RunningAverage externalTemp(5);       //external temp average
int itempRaw = 0;               //sets internal temp Raw
int ihumRaw = 0;                //sets internal humidityRaw
int etempRaw = 0;               //sets external temp Raw
int ehumRaw = 0;                //sets external humidity Raw
int tempI = 0;                  //variable for averaged internal temp
int tempE = 0;                  //variable for averaged external temp
int humidityI = 0;              //variable for averaged internal humidity
int humidityE = 0;              //variable for averaged external humidity
const byte nsum = 5;            //




//set variable type
int chk;                        //


//greenhouse setpoints
int heatTempV = 70; //setpoint temp in F when vent would open
int heatTempF = 71; //setpoint temp in F when fan turns on
int humV = 40;       //setpoint humidity in % when vent opens
int humF = 60;       //setpoint humidity in % when fan kicks on


//digital outputs
int fan = 0;      //this variable is for displaying via LCD or serial fan status
int vent = 0;     //this variable is for displaying via LCD or serial fan status


const int rs = 3, en = 4, d4 = 5, d5 = 6, d6 = 7, d7 = 8;  //set LCD pins
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);                    //start LCD


void setup() {
 
    //setting up outputs
  pinMode (43, OUTPUT);     //output trigger for vent
  pinMode (41, OUTPUT);     //output trigger for fan


  lcd.begin(20, 4);           //starts LCD
  Serial.begin(9600);         //opens serial port
  dht.begin ();               //starts int temp sensor
  dhttwo.begin ();            //starts ext temp sensor
  clock.begin ();             //starts rtc
  //clock.setDateTime (__DATE__, __TIME__); //sets clock from computer, only need to do this once unless battery dies on RTC or time is wrong


}
void loop() {
  delay(500);
 
  //Setting up the values to be read
  ihumRaw = dht.readHumidity();              //reads internal humidity
  itempRaw = dht.readTemperature (true);     //sets up to read int temp in F, for C remove "true"
  ehumRaw = dhttwo.readHumidity ();          //sets up to read ext humidity
  etempRaw = dhttwo.readTemperature (true);  //sets up to read ext temp in F, for C remove "true"
  
  //setting how large the variables can be
  ihumRaw = constrain (ihumRaw, 0, 100);      //limits value to 0-100
  ehumRaw = constrain (ehumRaw,0,100);        //limits value to 0-100
  itempRaw = constrain (itempRaw,-50,100);    //limits value to -50 to 100
  etempRaw = constrain (etempRaw,-50,100);    //limits value to -50 to 100


  //assinging the actual values to the variables
  if (ehumRaw > 1)  
    externalHumidity.addValue(ehumRaw);
  if (etempRaw > 1)   
    externalTemp.addValue(etempRaw);
  if (ihumRaw > 1)   
    internalHumidity.addValue(ihumRaw);
   if (itempRaw > 1)  
    internalTemp.addValue(itempRaw);


  //calculating the average and setting it to variables to be used in further logic and displayed
  humidityI = internalHumidity.getAverage();
  humidityE = externalHumidity.getAverage();
  tempI = internalTemp.getAverage();
  tempE = externalTemp.getAverage();   


 //Calculate heat index
 float hif = dht.computeHeatIndex (tempE, humidityE);


 
 //setting up clock to get time
  dt = clock.getDateTime ();  //sets dt to get the clock time




  /*______________________________________________Serial debugging_________________________________________________________
    //Serial print code for debugging
 Serial.println(clock.dateFormat("M jS y, h:ia", dt));
  Serial.print("Raw Int Humidity: ");
  Serial.print(ihumRaw);
  Serial.print(" %, Raw Int Temp: ");
  Serial.print (itempRaw);
  Serial.println(" Farenheit");
  Serial.print("Avg Int Humidity: ");
  Serial.print (humidityI);
  Serial.print(" %, Avg Int Temp: ");
  Serial.print (tempI);
  Serial.println(" Farenheit"); 
  Serial.print("Raw Ext Humidity: ");
  Serial.print(ehumRaw);
  Serial.print(" %, Raw Ext Temp: ");
  Serial.print (etempRaw);
  Serial.println(" Farenheit");
  Serial.print("Avg Ext Humidity: ");
  Serial.print (humidityE);
  Serial.print(" %, Avg Ext Temp: ");
  Serial.print (tempE);
  Serial.print(" Farenheit");
  Serial.println(" ");
  Serial.println(" ");
*/


 //_________________________________________________LCD Display Code_________________________________________
  lcd.clear ();           
  lcd.setCursor(0,0);     
  lcd.print("IT:");
  lcd.print(tempI);
  lcd.print(" ");
  lcd.print((char)223);
  lcd.print("F |");
  lcd.print(clock.dateFormat("  h:ia", dt));
  lcd.setCursor(0,1);
  lcd.print("IH:");
  lcd.print(humidityI);
  lcd.print(" %  |  ");
  lcd.print("VENT:");
  if (vent == 1)
  {
    lcd.print("OPN");
  }
  else if (vent == 0)
  {
    lcd.print("CSD");
  }
  lcd.setCursor(0,2);
  lcd.print("ET:");
  lcd.print(tempE);
  lcd.print(" ");
  lcd.print((char)223);
  lcd.print("F |  ");
  lcd.print("FAN :");
  if (fan == 1)
  {
    lcd.print("ON");
  }
  else if (fan == 0)
  {
    lcd.print("OFF");
  }
  lcd.setCursor(0,3);
  lcd.print("EH:");
  lcd.print(humidityE);
  lcd.print(" %  |");
  lcd.print("  WATR:OFF");
  delay (1000);


//_______________________________________Logic Control___________________________________________________
//vent logic
if (humidityE > humV + 1)
{
    digitalWrite (43, HIGH);
    vent = 1;
    }
else if (humidityE < humV + 1)
{
    digitalWrite (43, LOW);
    vent = 0;
}
//fan logic
if (humidityE > humF + 1)
{
    digitalWrite (41, HIGH);
    fan = 1;
}
else if (humidityE < humF + 1)
{
    digitalWrite (41, LOW);
    fan = 0;
} 
}

Are you using this libary? If so, there's a function getHour() that'll return 24Hr or 12Hr.
Look at the header.

Thanks, using that library after multiple attempts and looking at other code tonight I was able to get what I needed.

To control day night logic I've added the following to the code to, tod is for "time of day"

//setting up clock to get time
 Hour = now.hour();           //sets Hour as the 0-23 value

// day-night logic
if (Hour >= 7 && Hour <= 19)
    tod = 0;
else
  tod = 1;

tod is for "time of day"

How does "time of day" relate to true or false? Making the type boolean, and naming the variable isDay, with values of true or false WOULD make sense. isDay = false means that it is not daytime. isDay = true means that it IS daytime.

there is a low overhead sunrise/sunset library that you may want to try.

You can use sunMoon.h library as attached. It is very accurate (Astronomical-clock). DS3232RTC.h will work with this library. I am also struggling to do the same if you can handle this library.

I use this simple logic with LDR which is giving the result as suspected but after 24:00 it will gose error

 time_t sRise = sm.sunRise();
 time_t sSet  = sm.sunSet();

if (RTC.get() > sRise && RTC.get() < sSet && lt > 680 )
  {
    Serial.println("Day");
    lcd.setCursor(4, 3);
    lcd.print("Day");  
  }
  else if (RTC.get() > sSet && lt <590 || RTC.get() > sSet && lt <590)
  {
    Serial.println("Night ");
    lcd.setCursor(4, 3);
    lcd.print("Night");
  }
  else
  {
    Serial.println("Error ");
    lcd.setCursor(4, 3);
    lcd.print("Error");
  }

sunMoon-master.zip (16 KB)