How to check a if a status has passed

Hello All

My code uses a RTC and i am using this code but slightly less cluttered

I am using this to turn lights on and of at set times, so far the code works fine however if the adruino resets it keeps the lights off untill alarm1 has come back round maybe 18 hours later.

Alarm1 turn the lights on
Alarm2 turns them off

I need help as i cant figure out how to get the code to look and see if it is in between the alarm1 and 2 and if so turn them back on

Hydrocode2017.ino (10.1 KB)

Post the code in line within code tags, don't attach - can't see it on mobile phone

Ok this is the code

#include <DHT.h> // Air Temp Humid Sensor
#include <OneWire.h>
#include <DallasTemperature.h> // Water Temp Sensor
#include <LiquidCrystal_I2C.h>
#include <Wire.h>     // enables uno SCL SDL pins
#include <DS3231.h> // RTC 
const int lightingPin = 8;
const int fanPin= 12;
int fanMode = 0;
int cycleState =0;
int val =0;
int ph_pin = A3; // Analoge input from ph sensor pin Po
unsigned long currentTime;
unsigned long previousTime = 0;
int marker = 0;
DS3231 clock;
RTCDateTime dt; // dt get date time
int lightWatch = 0;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Lcd Display ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


// set the LCD address to 0x3F for a 16 chars 2 line display
// Set the pins on the I2C chip used for LCD connections:
//                    addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Air Temperature & Humidity sensor ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#define DHTPIN 2     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor
int chk;
float hum;  //Stores humidity value
float temp; //Stores temperature value


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Water Temperature sensor ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#define ONE_WIRE_BUS 7
// Setup a oneWire instance to communicate with any OneWire devices 
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
 
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);





//################################################################# SETUP #################################################################################################
void setup()   
{
  Serial.begin(9600);
  lcd.begin(16,2);         // initialize the lcd for 16 chars 2 lines, turn on backlight
  lcd.backlight();         // Turns backlight LCD on
  Wire.begin();            // enables uno SCL SDL pins
  sensors.begin();         // water temp sensor
  pinMode(lightingPin, OUTPUT);
  digitalWrite (lightingPin, LOW);
  clock.begin();          // Initialize DS3231
  dht.begin();            // temp humid sensor 
  pinMode (fanPin, OUTPUT);
  digitalWrite (fanPin, HIGH);
  clock.armAlarm1(false);
  clock.clearAlarm1();
  clock.armAlarm2(false);
  clock.clearAlarm2();
 
             

  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Real time clock Settings ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  
 //clock.setDateTime(2017, 4, 10, 07, 58, 0);      // Set date/time (Year, Month, Day, Hour, Minute, Second) "//" Out if clock is already set

  //~~~~~~~~~~~~~~~~~~~~~~~~~Alarm Timer Settings "Dont forget up update DAY/NIGHT SET TIMES in loop for quick reference ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  
  clock.setAlarm1(0, 01, 00, 00, DS3231_MATCH_H_M_S);    // Set Alarm 1 time and date
  clock.setAlarm2(0,18, 00, DS3231_MATCH_H_M);          // Set Alarm 2 time and date

     
  checkAlarms();      // Check alarm settings
}
void checkAlarms() {
  RTCAlarmTime a1;  
  RTCAlarmTime a2;
  
  if (clock.isArmed1()){
    a1 = clock.getAlarm1();

    Serial.print("Alarm1 is triggered ");
   
    switch (clock.getAlarmType1()) {
     
      case DS3231_MATCH_H_M_S:
        Serial.print("when hours, minutes and seconds match: ");
        Serial.println(clock.dateFormat("__ H:i:s", a1));
        break;
    }
  }
  else {
    Serial.println("Alarm1 is disarmed.");
  }

  if (clock.isArmed2()) {
    a2 = clock.getAlarm2();

    Serial.print("Alarm2 is triggered ");
    
    switch (clock.getAlarmType2())
    {
      
      case DS3231_MATCH_H_M:
        Serial.print("when hours and minutes match:");
        Serial.println(clock.dateFormat("__ H:i:s", a2));
        break;
     
    }
  }
  else {
    Serial.println("Alarm2 is disarmed.");
  }
}
 //########################################################################## LOOP ################################################################################################### 
 
void loop(){
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ OUTPUT REAL TIME CLOCK ON SERIAL ~~~~~~~~~~~~~~~~~~~~~~~~~~

 dt = clock.getDateTime();
 Serial.println(clock.dateFormat("d-m-Y H:i:s - l", dt));
{
  
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ALARM RELAY ACTION ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
  
  if(clock.isAlarm1()) {
    Serial.println("ALARM 1 TRIGGERED!");
    digitalWrite (lightingPin, HIGH); 
  }
  if(clock.isAlarm2()) {
    Serial.println("ALARM 2 TRIGGERED!");
    digitalWrite (lightingPin, LOW);
  }  

  
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Fan Mode ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  
  fanMode = digitalRead (fanPin);
   
    if (temp <=20)
    {
  Serial.println ("Fan OFF");
  digitalWrite (fanPin, HIGH);
    }
   else if (temp >=21)
   {
    Serial.println ("Fan On");
    digitalWrite (fanPin, LOW);
   }

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Day Night Cycle Sensor ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  cycleState = digitalRead (lightingPin);

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Water Temp Sensor ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  sensors.requestTemperatures();

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Air and Humid Sensor ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  hum = dht.readHumidity(); //Read data and store it to variables hum and temp
  temp= dht.readTemperature();

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Real Time Clock Read ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 dt = clock.getDateTime();

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ PH Sensor Data ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  int measure = analogRead(ph_pin);
  double volt = 5 / 1024.0 * measure; // digital to voltage conversion
  float Po = 7 + ((2.510 - volt) / 0.18);


}  
}

If you look at the code you provided in the link, they have a variable RTCDateTime dt; and they initialize it this way dt = clock.getDateTime(); in the loop.

A RTCDateTime variable is a structure looking like this (you can see that in the DS3231.h of the library or in the doc)

struct RTCDateTime
{
    uint16_t year;
    uint8_t month;
    uint8_t day;
    uint8_t hour;
    uint8_t minute;
    uint8_t second;
    uint8_t dayOfWeek;
    uint32_t unixtime;
};

So in your setup(), after you have initialized access to the clock, if you were to have such a dt variable, then you would know what time it is: dt.hour and dt.minute (and dt.second if you want that precision)

To compare two moment of the day say for example 12h15 and 13h22 best is to put everything in minutes (or seconds) elapsed since midnight

12h15 = 1260+15 = 745 minutes since midnight and 13h22 = 1360+22=802 minutes since midnight ==> as 802 is greater than 745 you know that 13h22 is later in the day than 12h15 (wow :slight_smile: )

(Careful if you want to have second precision, then you need to convert in seconds but then it might not fit in an integer type, you will need a long usigned long t0 = dt.hour * 3600[color=red][b]ul[/b][/color] + dt.minute * 60 + dt.second==> note the ul to tell the compiler to do the math using usigned long and not just integers which is what it would use by default)

Thank you J-M-L,

much appreciated, I am just trying to get my head around this, i dont understand where the 3600 comes from?

As i understand this if id did this it should work for the lights to come on at 1am and off at 6pm

int alarmState = dt.hour*60 + dt.minute;

if (alarmState >= 60 <=1080){
digitalWrite (lightingPin, HIGH);
}
if (alarmState >= 1080){
digitalWrite (lightingPin, LOW);
}

should work?

I have put the pieces together but it does not seem to work
here is the code now

#include <DHT.h> // Air Temp Humid Sensor
#include <OneWire.h>
#include <DallasTemperature.h> // Water Temp Sensor
#include <LiquidCrystal_I2C.h>
#include <Wire.h>     // enables uno SCL SDL pins
#include <DS3231.h> // RTC 
const int lightingPin = 8;
const int fanPin= 12;
int fanMode = 0;
int cycleState =0;
int val =0;
int ph_pin = A3; // Analoge input from ph sensor pin Po
unsigned long currentTime;
unsigned long previousTime = 0;
int marker = 0;
DS3231 clock;
RTCDateTime dt; // dt get date time
int alarmState = dt.hour*60 + dt.minute;


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Lcd Display ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


// set the LCD address to 0x3F for a 16 chars 2 line display
// Set the pins on the I2C chip used for LCD connections:
//                    addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Air Temperature & Humidity sensor ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#define DHTPIN 2     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor
int chk;
float hum;  //Stores humidity value
float temp; //Stores temperature value


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Water Temperature sensor ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#define ONE_WIRE_BUS 7
// Setup a oneWire instance to communicate with any OneWire devices 
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
 
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);





//################################################################# SETUP #################################################################################################
void setup()   
{
  Serial.begin(9600);
  lcd.begin(16,2);         // initialize the lcd for 16 chars 2 lines, turn on backlight
  lcd.backlight();         // Turns backlight LCD on
  Wire.begin();            // enables uno SCL SDL pins
  sensors.begin();         // water temp sensor
  pinMode(lightingPin, OUTPUT);
  digitalWrite (lightingPin, LOW);
  clock.begin();          // Initialize DS3231
  clock.getDateTime();    // get the Time and Date
  dht.begin();            // temp humid sensor 
  pinMode (fanPin, OUTPUT);
  digitalWrite (fanPin, HIGH);
  clock.armAlarm1(false);
  clock.clearAlarm1();
  clock.armAlarm2(false);
  clock.clearAlarm2();
  
             

  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Real time clock Settings ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  
 //clock.setDateTime(2017, 4, 10, 07, 58, 0);      // Set date/time (Year, Month, Day, Hour, Minute, Second) "//" Out if clock is already set

  //~~~~~~~~~~~~~~~~~~~~~~~~~Alarm Timer Settings "Dont forget up update DAY/NIGHT SET TIMES in loop for quick reference ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  
  clock.setAlarm1(0, 05, 00, 00, DS3231_MATCH_H_M_S);    // Set Alarm 1 time and date
  clock.setAlarm2(0,23, 00, DS3231_MATCH_H_M);          // Set Alarm 2 time and date

     
  checkAlarms();      // Check alarm settings
}
void checkAlarms() {
  RTCAlarmTime a1;  
  RTCAlarmTime a2;
  
  if (clock.isArmed1()){
    a1 = clock.getAlarm1();

    Serial.print("Alarm1 is triggered ");
   
    switch (clock.getAlarmType1()) {
     
      case DS3231_MATCH_H_M_S:
        Serial.print("when hours, minutes and seconds match: ");
        Serial.println(clock.dateFormat("__ H:i:s", a1));
        break;
    }
  }
  else {
    Serial.println("Alarm1 is disarmed.");
  }

  if (clock.isArmed2()) {
    a2 = clock.getAlarm2();

    Serial.print("Alarm2 is triggered ");
    
    switch (clock.getAlarmType2())
    {
      
      case DS3231_MATCH_H_M:
        Serial.print("when hours and minutes match:");
        Serial.println(clock.dateFormat("__ H:i:s", a2));
        break;
     
    }
  }
  else {
    Serial.println("Alarm2 is disarmed.");
  }
}
 //########################################################################## LOOP ################################################################################################### 
 
void loop(){
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ OUTPUT REAL TIME CLOCK ON SERIAL ~~~~~~~~~~~~~~~~~~~~~~~~~~

 dt = clock.getDateTime();
 Serial.println(clock.dateFormat("d-m-Y H:i:s - l", dt));
{
  
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ALARM RELAY ACTION ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 if (alarmState >= 300 < 1380){
  digitalWrite (lightingPin, HIGH);
 }
 if (alarmState >= 1380 < 300){
  digitalWrite (lightingPin, LOW);
 }
  
  if(clock.isAlarm1()) {
    Serial.println("ALARM 1 TRIGGERED!");
    digitalWrite (lightingPin, HIGH); 
  }
  if(clock.isAlarm2()) {
    Serial.println("ALARM 2 TRIGGERED!");
    digitalWrite (lightingPin, LOW);
  }    
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Fan Mode ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  
  fanMode = digitalRead (fanPin);
   
    if (temp <=20)
    {
  Serial.println ("Fan OFF");
  digitalWrite (fanPin, HIGH);
    }
   else if (temp >=21)
   {
    Serial.println ("Fan On");
    digitalWrite (fanPin, LOW);
   }

if (alarmState >= 60 <=1080){Wrong way to check a range.

Try

 if (alarmState >= 60 && alarmState <=1080){
if (alarmState >= 60 <=1080){

alarmState will be
a) less than, or
b) greater than or equal to 60.
So, that will evaluate true (1) or false (0).

1 is always less than 1080.
0 is always less than 1080.

Overall, always true.

Got it!!! THANK YOU

the code works fine this is the updated!

#include <DHT.h> // Air Temp Humid Sensor
#include <OneWire.h>
#include <DallasTemperature.h> // Water Temp Sensor
#include <LiquidCrystal_I2C.h>
#include <Wire.h>     // enables uno SCL SDL pins
#include <DS3231.h> // RTC 
const int lightingPin = 8;
const int fanPin= 12;
int fanMode = 0;
int cycleState =0;
int val =0;
int ph_pin = A3; // Analoge input from ph sensor pin Po
unsigned long currentTime;
unsigned long previousTime = 0;
int marker = 0;
DS3231 clock;
RTCDateTime dt; // dt get date time
int alarmState = 0;


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Lcd Display ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


// set the LCD address to 0x3F for a 16 chars 2 line display
// Set the pins on the I2C chip used for LCD connections:
//                    addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Air Temperature & Humidity sensor ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#define DHTPIN 2     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor
int chk;
float hum;  //Stores humidity value
float temp; //Stores temperature value


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Water Temperature sensor ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#define ONE_WIRE_BUS 7
// Setup a oneWire instance to communicate with any OneWire devices 
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
 
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);





//################################################################# SETUP #################################################################################################
void setup()   
{
  Serial.begin(9600);
  lcd.begin(16,2);         // initialize the lcd for 16 chars 2 lines, turn on backlight
  lcd.backlight();         // Turns backlight LCD on
  Wire.begin();            // enables uno SCL SDL pins
  sensors.begin();         // water temp sensor
  pinMode(lightingPin, OUTPUT);
  digitalWrite (lightingPin, LOW);
  clock.begin();          // Initialize DS3231
  clock.getDateTime();    // get the Time and Date
  dht.begin();            // temp humid sensor 
  pinMode (fanPin, OUTPUT);
  digitalWrite (fanPin, HIGH);
  clock.armAlarm1(false);
  clock.clearAlarm1();
  clock.armAlarm2(false);
  clock.clearAlarm2();
  
             

  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Real time clock Settings ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  
  //clock.setDateTime(2017, 4, 10, 07, 58, 0);      // Set date/time (Year, Month, Day, Hour, Minute, Second) "//" Out if clock is already set

  //~~~~~~~~~~~~~~~~~~~~~~~~~Alarm Timer Settings "Dont forget up update DAY/NIGHT SET TIMES in loop for quick reference ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Dont forget to update alarmState as lighting failsafe ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

  
  clock.setAlarm1(0, 05, 00, 00, DS3231_MATCH_H_M_S);    // Set Alarm 1 time and date
  clock.setAlarm2(0,23, 00, DS3231_MATCH_H_M);          // Set Alarm 2 time and date

     
  checkAlarms();      // Check alarm settings
}
void checkAlarms() {
  RTCAlarmTime a1;  
  RTCAlarmTime a2;
  
  if (clock.isArmed1()){
    a1 = clock.getAlarm1();

    Serial.print("Alarm1 is triggered ");
   
    switch (clock.getAlarmType1()) {
     
      case DS3231_MATCH_H_M_S:
        Serial.print("when hours, minutes and seconds match: ");
        Serial.println(clock.dateFormat("__ H:i:s", a1));
        break;
    }
  }
  else {
    Serial.println("Alarm1 is disarmed.");
  }

  if (clock.isArmed2()) {
    a2 = clock.getAlarm2();

    Serial.print("Alarm2 is triggered ");
    
    switch (clock.getAlarmType2())
    {
      
      case DS3231_MATCH_H_M:
        Serial.print("when hours and minutes match:");
        Serial.println(clock.dateFormat("__ H:i:s", a2));
        break;
     
    }
  }
  else {
    Serial.println("Alarm2 is disarmed.");
  }
}
 //########################################################################## LOOP ################################################################################################### 
 
void loop(){
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ OUTPUT REAL TIME CLOCK ON SERIAL ~~~~~~~~~~~~~~~~~~~~~~~~~~

 dt = clock.getDateTime();
 Serial.println(clock.dateFormat("d-m-Y H:i:s - l", dt));
{
  
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ALARM RELAY ACTION ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  alarmState = dt.hour*60 + dt.minute; 

// take the hours in 24 hour clock and times the hour by 60 then add the minutes so 2:15am will be (2*60+15=135)

  if (alarmState >= 300 && alarmState < 1380){
    digitalWrite (lightingPin, HIGH);
 }
  if (alarmState >= 1380 && alarmState < 300){
    digitalWrite (lightingPin, LOW);
 }
  
  if(clock.isAlarm1()) {
    Serial.println("ALARM 1 TRIGGERED!");
    digitalWrite (lightingPin, HIGH); 
  }
  if(clock.isAlarm2()) {
    Serial.println("ALARM 2 TRIGGERED!");
    digitalWrite (lightingPin, LOW);
  }    
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Fan Mode ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  
  fanMode = digitalRead (fanPin);
   
    if (temp <24)
    {
    Serial.println ("Fan OFF");
    digitalWrite (fanPin, HIGH);
    }
   else if (temp >=24)
   {
    Serial.println ("Fan On");
    digitalWrite (fanPin, LOW);
   }
  if (temp >=28)
  {
    digitalWrite (lightingPin, LOW);
    Serial.print ("WARNING TENT OVERHEAT");
  }

once again thank you for all your help ;D

E235:
much appreciated, I am just trying to get my head around this, i dont understand where the 3600 comes from?

3600 is the number of seconds in 1 hour (60 minutes in 1 hour x 60 seconds per minute)

in your code  if (alarmState >= 300 && alarmState < 1380){I would recommend that you declare as const unsigned int (or const unsigned long if one day you go to second precision) the two constant values 300 and 1380 at the begining of the code, so that you can easily remember what they are

AWOL:

if (alarmState >= 60 <=1080){

alarmState will be
a) less than, or
b) greater than or equal to 60.
So, that will evaluate true (1) or false (0).

1 is always less than 1080.
0 is always less than 1080.

Overall, always true.

Thank you AWOL that makes sense!

J-M-L:
3600 is the number of seconds in 1 hour (60 minutes in 1 hour x 60 seconds per minute)

in your code

  if (alarmState >= 300 && alarmState < 1380){

I would recommend that you declare as const unsigned int (or const unsigned long if one day you go to second precision) the two constant values 300 and 1380 at the begining of the code, so that you can easily remember what they are

Thank you J-m-l really useful information much appreciated!