Showing posts with label nano. Show all posts
Showing posts with label nano. Show all posts

Arduino Grow Light Timer Six Switch/Relay Dual on/off Control V3.0 no oled/button version


 //*****************************************************************************************************************  
 //*****************************************************************************************************************  
   
   
 //************<<<< Arduino Nano, DS3231 Real Time Clock & Six Relay Module Lights Timer V3.0 >>>>******************  
   
   
 //*****************************************************************************************************************  
   
 //*****************************************************************************************************************  
 /*  
 Sketch uses 5722 bytes (18%) of program storage space. Maximum is 30720 bytes.  
 Global variables use 286 bytes (13%) of dynamic memory, leaving 1762 bytes for local variables. Maximum is 2048 bytes.  
   
  ************** UPDATED ON Oct 13 2018 *************  
   
 */  
   
   
 // no oled version  
 // six lights/relay control each with two on/off routines  
 // for oled version   
   
 //*****************************************************************************************************************  
   
   
 //*********************<<<< PLEASE VISIT / SUBSCRIBE MY YOUTUBE CHANNEL >>>>***************************************  
   
 // comment for more info  
   
   
 //*********************<<<<  Youtube.com/CrazyGuyOfficial  >>>>*****************************************  
   
   
   
 //*****************************************************************************************************************  
   
   
 //*****************************************************************************************************************  
 // INCLUDE LIBRARIES  
 //*****************************************************************************************************************  
   
   
   
   
   
   
 #include <Time.h>  // Time Manipulation   
 #include <Wire.h>  
 #include <DS1307RTC.h> // DS1307 RTC   
   
   
   
   
   
   
   
   
 //**********************//**********************//**********************  
 //  PINS AND CONNECTIONS  
 //**********************//**********************//**********************  
 const byte lightBlink  = 3 ;  
   
 const byte lightOne   = 5 ;  
 const byte lightTwo   = 6 ;  
 const byte lightThree  = 4 ;  
 const byte lightFour  = 7 ;  
 const byte lightFive   = 2 ;  
 const byte lightSix   = 8 ;  
   
   
   
 // DS3231 RTC "SDA to A4", "SCL to A5" "Vcc to 5V arduino pin Gnd to Gnd"  
 // Add more names/pins for more relays  
   
   
 //##################################################  
   
 // Light Timer ON/OFF Time Settings DEFAULT  
   
 //##################################################  
   
   
   
 byte OnOne1   = 14 ; // Hour when Light 1 will turn ON (24 hr format NO zeros before single digits)  
 byte OffOne1  = 8 ; // Hour when Light 1 will turn OFF (24 hr format NO zeros before single digits)  
 byte OnOne2   = 14 ;  // Hour when Light 1 will turn ON (24 hr format NO zeros before single digits)  
 byte OffOne2  = 8 ;  // Hour when Light 1 will turn OFF (24 hr format NO zeros before single digits)
   
   
 byte OnTwo1   = 20 ;  
 byte OffTwo1  = 8 ;  
 byte OnTwo2   = 20 ;  
 byte OffTwo2  = 8 ;  
   
 byte OnThree1  = 14 ;  
 byte OffThree1 = 8 ;  
 byte OnThree2  = 14 ;  
 byte OffThree2 = 8 ;  
   
 byte OnFour1  = 14 ;  
 byte OffFour1  = 8 ;  
 byte OnFour2  = 14 ;  
 byte OffFour2  = 8 ;  
   
 byte OnFive1  = 14 ;  
 byte OffFive1  = 8 ;  
 byte OnFive2  = 14 ;  
 byte OffFive2  = 8 ;  
   
 byte OnSix1   = 14 ;  
 byte OffSix1  = 8 ;  
 byte OnSix2   = 14 ;  
 byte OffSix2  = 8 ;  
   
   
   
 //*****************************************************************************************************************  
   
 bool RTCerr = 0;  
   
 bool LightOneSts   = 0 ;  
   
 bool LightTwoSts   = 0 ;  
   
 bool LightThreeSts  = 0 ;  
   
 bool LightFourSts  = 0 ;  
   
 bool LightFiveSts  = 0 ;  
   
 bool LightSixSts   = 0 ;  
   
   
 bool ppOne1     = 0 ;  
 bool ppOne2     = 0 ;  
   
 bool ppTwo1      = 0 ;  
 bool ppTwo2      = 0 ;  
   
 bool ppThree1      = 0 ;  
 bool ppThree2      = 0 ;  
   
 bool ppFour1      = 0 ;  
 bool ppFour2      = 0 ;  
   
 bool ppFive1      = 0 ;  
 bool ppFive2     = 0 ;  
   
 bool ppSix1      = 0 ;  
 bool ppSix2      = 0 ;  
   
 unsigned long SecsOne = 0;  
   
   
   
 //##################  
 //## RTC ADDRESS ##  
 //##################  
   
   
 #define DS1307_ID 0x68 // I2c Address of the RTC    
   
 byte zero = 0x00; //workaround for issue #527  
   
 //#################################  
   
   
   
   
   
 //*****************************************************************************************************************  
 // SETUP BEGINS  
 //*****************************************************************************************************************  
   
   
 void setup()  
 {  
  digitalWrite(lightOne,  HIGH);  
  digitalWrite(lightTwo,  HIGH); // turn the relay "OFF" and turning the pin "LOW" turns the relay "ON"  
  digitalWrite(lightThree, HIGH);  
  digitalWrite(lightFour, HIGH);  
  digitalWrite(lightFive, HIGH); // turn the relay "OFF" and turning the pin "LOW" turns the relay "ON"  
  digitalWrite(lightSix,  HIGH);  
   
   
  pinMode(lightBlink, OUTPUT);  
   
  pinMode(lightOne,  OUTPUT);  
  pinMode(lightTwo,  OUTPUT);  
  pinMode(lightThree, OUTPUT);  
  pinMode(lightFour, OUTPUT);  
  pinMode(lightFive, OUTPUT);  
  pinMode(lightSix,  OUTPUT);  
   
  //**********************//**********************//***********************//**********************  
  //Check to see if the RTC is present.if yes then Set the ARDUINO's INTERNAL clock accordingly  
  //**********************//**********************//***********************//**********************  
   
   
  Wire.beginTransmission(DS1307_ID);  
  Wire.write((uint8_t)0x00);  
   
  setSyncProvider(RTC.get);   //Yes it did, Sync the time from the RTC  
  setSyncInterval(20);  
   
   
   
  if (timeStatus() != timeSet)  
   RTCerr = 1;  
  else  
   RTCerr = 0;  
   
   
   
    
   
   
   
  Delay15S();  
   
 }  
   
 //*****************************************************************************************************************  
 //        SETUP END  
 //*****************************************************************************************************************  
   
   
   
 void Delay15S()  
 {  
  delay(1500);  
   
 }  
   
   
 //*****************************************************************************************************************  
   
   
 //     LOOP BEGINS  
   
   
 //*****************************************************************************************************************  
   
   
   
   
 void loop()  
 {  
    
   
   
   
  //#####################################  
  //####### LIGHTs ON/OFF ###############  
  //#####################################  
   
   
   
  //########## LIGHT ONE PP1##########################  
   
  if (OffOne1 > OnOne1)  
  {  
   
   
   if (hour() >= OnOne1 && hour () <= OffOne1 - 1)  
   {  
    TurnLightOneOn1();  
   }  
   
   else  
   {  
    TurnLightOneOff1();  
   
   }  
   
  }  
   
   
  //**********************************************************  
   
   
  if (OffOne1 < OnOne1 )  
  {  
   
   
   if (hour() >= OnOne1 || hour () <= OffOne1 - 1)  
   {  
    TurnLightOneOn1();  
   }  
   
   
   
   else  
   {  
    TurnLightOneOff1();  
   
   }  
  }  
   
   
  //########## LIGHT ONE PP2 ##########################  
   
  if (OffOne2 > OnOne2)  
  {  
   
   
   if (hour() >= OnOne2 && hour () <= OffOne2 - 1)  
   {  
    TurnLightOneOn2();  
   
   
   }  
   
   else  
   {  
    TurnLightOneOff2();  
   
   }  
   
  }  
   
   
  //**********************************************************  
   
   
  if (OffOne2 < OnOne2 )  
  {  
   
   
   if (hour() >= OnOne2 || hour () <= OffOne2 - 1)  
   {  
    TurnLightOneOn2();  
   
   
   }  
   
   
   
   else  
   {  
    TurnLightOneOff2();  
   
   
   }  
  }  
   
  //####################################################################  
   
  //########## LIGHT TWO pp1 ##########################  
   
  if (OffTwo1 > OnTwo1)  
  {  
   
   
   if (hour() >= OnTwo1 && hour () <= OffTwo1 - 1)  
   {  
    TurnLightTwoOn1();  
   
   }  
   
   else  
   {  
   
    TurnLightTwoOff1();  
   
   }  
   
  }  
   
   
  //**********************************************************  
   
   
  if (OffTwo1 < OnTwo1)  
  {  
   
   
   if (hour() >= OnTwo1 || hour () <= OffTwo1 - 1)  
   {  
    TurnLightTwoOn1();  
   
   
   }  
   
   
   
   else  
   {  
    TurnLightTwoOff1();  
   
   
   }  
  }  
  //########## LIGHT TWO pp2 ##########################  
   
  if (OffTwo2 > OnTwo2)  
  {  
   
   
   if (hour() >= OnTwo2 && hour () <= OffTwo2 - 1)  
   {  
    TurnLightTwoOn2();  
   }  
   
   else  
   {  
    TurnLightTwoOff2();  
   
   }  
   
  }  
   
   
  //**********************************************************  
   
   
  if (OffTwo2 < OnTwo2)  
  {  
   
   
   if (hour() >= OnTwo2 || hour () <= OffTwo2 - 1)  
   {  
    TurnLightTwoOn2();  
   
   }  
   
   
   
   else  
   {  
    TurnLightTwoOff2();  
   
   
   }  
  }  
   
   
   
  //####################################################################  
   
  //########## LIGHT THREE pp 1 ##########################  
   
  if (OffThree1 > OnThree1)  
  {  
   
   
   if (hour() >= OnThree1 && hour () <= OffThree1 - 1)  
   {  
    TurnLightThreeOn1();  
   }  
   
   else  
   {  
    TurnLightThreeOff1();  
   
   
   }  
   
  }  
   
   
  //**********************************************************  
   
   
  if (OffThree1 < OnThree1)  
  {  
   
   
   if (hour() >= OnThree1 || hour () <= OffThree1 - 1)  
   {  
    TurnLightThreeOn1();  
   
   }  
   
   
   
   else  
   {  
   
    TurnLightThreeOff1();  
   
   
   
   
   }  
  }  
  //########## LIGHT THREE pp 2 ##########################  
   
  if (OffThree2 > OnThree2)  
  {  
   
   
   if (hour() >= OnThree2 && hour () <= OffThree2 - 1)  
   {  
    TurnLightThreeOn2();  
   }  
   
   else  
   {  
    TurnLightThreeOff2();  
   
   }  
   
  }  
   
   
  //**********************************************************  
   
   
  if (OffThree2 < OnThree2)  
  {  
   
   
   if (hour() >= OnThree2 || hour () <= OffThree2 - 1)  
   {  
    TurnLightThreeOn2();  
   
   }  
   
   
   
   else  
   {  
   
    TurnLightThreeOff2();  
   
   }  
  }  
   
  //####################################################################  
   
  //########## LIGHT FOUR pp1 ##########################  
   
  if (OffFour1 > OnFour1)  
  {  
   
   
   if (hour() >= OnFour1 && hour () <= OffFour1 - 1)  
   {  
    TurnLightFourOn1();  
   }  
   
   else  
   {  
    TurnLightFourOff1();  
   
   
   }  
   
  }  
   
   
  //**********************************************************  
   
   
  if (OffFour1 < OnFour1)  
  {  
   
   
   if (hour() >= OnFour1 || hour () <= OffFour1 - 1)  
   {  
    TurnLightFourOn1();  
   
   }  
   
   
   
   else  
   {  
    TurnLightFourOff1();  
   
   
   }  
  }  
  //########## LIGHT FOUR pp2 ##########################  
  if (OffFour2 > OnFour2)  
  {  
   
   
   if (hour() >= OnFour2 && hour () <= OffFour2 - 1)  
   {  
    TurnLightFourOn2();  
   
   }  
   
   else  
   {  
    TurnLightFourOff2();  
   
   }  
   
  }  
   
   
  //**********************************************************  
   
   
  if (OffFour2 < OnFour2)  
  {  
   
   
   if (hour() >= OnFour2 || hour () <= OffFour2 - 1)  
   {  
    TurnLightFourOn2();  
   
   }  
   
   
   
   else  
   {  
    TurnLightFourOff2();  
   
   
   }  
  }  
   
  //####################################################################  
   
  //########## LIGHT FIVE pp1 ##########################  
   
  if (OffFive1 > OnFive1)  
  {  
   
   
   if (hour() >= OnFive1 && hour () <= OffFive1 - 1)  
   {  
    TurnLightFiveOn1();  
   }  
   
   else  
   {  
    TurnLightFiveOff1();  
   
   
   }  
   
  }  
   
   
  //**********************************************************  
   
   
  if (OffFive1 < OnFive1)  
  {  
   
   
   if (hour() >= OnFive1 || hour () <= OffFive1 - 1)  
   {  
    TurnLightFiveOn1();  
   
   }  
   
   
   
   else  
   {  
    TurnLightFiveOff1();  
   
   }  
  }  
   
  //########## LIGHT FIVE pp2 ##########################  
   
  if (OffFive2 > OnFive2)  
  {  
   
   
   if (hour() >= OnFive2 && hour () <= OffFive2 - 1)  
   {  
    TurnLightFiveOn2();  
   
   }  
   
   else  
   {  
    TurnLightFiveOff2();  
   
   
   
   }  
   
  }  
   
   
  //**********************************************************  
   
   
  if (OffFive2 < OnFive2)  
  {  
   
   
   if (hour() >= OnFive2 || hour () <= OffFive2 - 1)  
   {  
    TurnLightFiveOn2();  
   
   }  
   
   
   
   else  
   {  
    TurnLightFiveOff2();  
   
   
   }  
  }  
   
   
  //####################################################################  
  //########## LIGHT SIX pp1 ##########################  
   
  if (OffSix1 > OnSix1)  
  {  
   
   
   if (hour() >= OnSix1 && hour () <= OffSix1 - 1)  
   {  
    TurnLightSixOn1();  
   }  
   
   else  
   {  
    TurnLightSixOff1();  
   
   
   
   }  
   
  }  
   
   
  //**********************************************************  
   
   
  if (OffSix1 < OnSix1)  
  {  
   
   
   if (hour() >= OnSix1 || hour () <= OffSix1 - 1)  
   {  
    TurnLightSixOn1();  
   
   }  
   
   
   
   else  
   {  
    TurnLightSixOff1();  
   
   }  
  }  
   
  //########## LIGHT SIX pp2 ##########################  
   
  if (OffSix2 > OnSix2)  
  {  
   
   
   if (hour() >= OnSix2 && hour () <= OffSix2 - 1)  
   {  
    TurnLightSixOn2();  
   }  
   
   else  
   {  
   
   
    TurnLightSixOff2();  
   
   
   
   }  
   
  }  
   
   
  //**********************************************************  
   
   
  if (OffSix2 < OnSix2)  
  {  
   
   
   if (hour() >= OnSix2 || hour () <= OffSix2 - 1)  
   {  
    TurnLightSixOn2();  
   
   }  
   
   
   
   else  
   {  
    TurnLightSixOff2();  
   }  
  }  
   
  //####################################################################  
   
   
   
  //#####################################  
  // Blink Led And Call RTC TEST  
  //#####################################  
   
  if (millis() / 1000 - SecsOne > 10 ) //  
  {  
   digitalWrite (lightBlink, HIGH); // blink status LED  
   
   
   rtctest();  
   
   SecsOne = millis() / 1000; // then reset the timer  
   
   
   
  }  
   
   
 }  
   
   
   
   
   
 //**********************//**********************//**********************  
 //        LOOP END  
 //**********************//**********************//**********************  
 void TurnLightOneOn1()  
 {  
  if (!LightOneSts)  
  {  
   digitalWrite(lightOne, LOW);  
   
   LightOneSts = 1 ;  
   
   Delay15S();  
   
  }  ppOne1 = 1 ;  
   
 }  
   
   
   
 void TurnLightOneOff1()  
 {  
  if (!ppOne2)  
  {  
   digitalWrite(lightOne, HIGH);  
   LightOneSts = 0 ;  
  }  
  ppOne1 = 0 ;  
   
   
 }  
   
   
   
   
   
 void TurnLightOneOn2()  
 {  
   
  if (!LightOneSts)  
  {  
   digitalWrite(lightOne, LOW);  
   LightOneSts = 1 ;  
   
   Delay15S();  
   
  }  ppOne2 = 1 ;  
   
   
   
 }  
   
 void TurnLightOneOff2()  
 {  
  if (!ppOne1)  
  {  
   digitalWrite(lightOne, HIGH);  
   LightOneSts = 0 ;  
  }  
  ppOne2 = 0 ;  
 }  
   
   
   
   
   
   
 void TurnLightTwoOn1()  
 {  
  if (!LightTwoSts)  
  {  
   digitalWrite(lightTwo, LOW);  
   
   LightTwoSts = 1 ;  
   
   Delay15S();  
   
  }  ppTwo1 = 1 ;  
   
 }  
   
   
   
 void TurnLightTwoOff1()  
 {  
  if (!ppTwo2)  
  {  
   digitalWrite(lightTwo, HIGH);  
   LightTwoSts = 0 ;  
  }  
  ppTwo1 = 0 ;  
   
   
 }  
   
 void TurnLightTwoOn2()  
 {  
   
  if (!LightTwoSts)  
  {  
   digitalWrite(lightTwo, LOW);  
   LightTwoSts = 1 ;  
   
   Delay15S();  
   
  }  ppTwo2 = 1 ;  
   
 }  
   
   
 void TurnLightTwoOff2()  
 {  
   
  if (!ppTwo1)  
  {  
   digitalWrite(lightTwo, HIGH);  
   LightTwoSts = 0 ;  
  }  
  ppTwo2 = 0 ;  
 }  
   
   
 void TurnLightThreeOn1()  
 {  
   
  if (!LightThreeSts)  
  {  
   digitalWrite(lightThree, LOW);  
   LightThreeSts = 1 ;  
   
   Delay15S();  
   
  }  ppThree1 = 1 ;  
   
 }  
   
   
   
 void TurnLightThreeOff1()  
 {  
  if (!ppThree2)  
  {  
   digitalWrite(lightThree, HIGH);  
   LightThreeSts = 0 ;  
  }  
  ppThree1 = 0 ;  
 }  
   
   
 void TurnLightThreeOn2()  
 {  
   
  if (!LightThreeSts)  
  {  
   digitalWrite(lightThree, LOW);  
   LightThreeSts = 1 ;  
   Delay15S();  
   
  } ppThree2 = 1 ;  
 }  
   
   
 void TurnLightThreeOff2()  
 {  
  if (!ppThree1)  
  {  
   digitalWrite(lightThree, HIGH);  
   LightThreeSts = 0 ;  
  }  
  ppThree2 = 0 ;  
   
 }  
   
 void TurnLightFourOn1()  
 {  
  if (!LightFourSts)  
  {  
   digitalWrite(lightFour, LOW);  
   LightFourSts = 1 ;  
   
   Delay15S();  
   
  }  
  ppFour1 = 1;  
   
 }  
 void TurnLightFourOff1()  
 {  
  if (!ppFour2)  
  {  
   
   digitalWrite(lightFour, HIGH);  
   LightFourSts = 0 ;  
  }  
  ppFour1 = 0;  
   
 }  
 void TurnLightFourOn2()  
 {  
  if (!LightFourSts)  
  {  
   digitalWrite(lightFour, LOW);  
   LightFourSts = 1 ;  
   Delay15S();  
   
  }  ppFour2 = 1;  
   
 }  
   
 void TurnLightFourOff2()  
 {  
  if (!ppFour1)  
  {  
   
   digitalWrite(lightFour, HIGH);  
   LightFourSts = 0 ;  
  }  
  ppFour2 = 0;  
   
 }  
   
 void TurnLightFiveOn1()  
 {  
  if (!LightFiveSts)  
  {  
   digitalWrite(lightFive, LOW);  
   LightFiveSts = 1 ;  
   
   Delay15S();  
   
  }  ppFive1 = 1;  
   
   
   
 }  
   
 void TurnLightFiveOff1()  
 {  
   
   
  if (!ppFive2)  
  {  
   
   
   digitalWrite(lightFive, HIGH);  
   LightFiveSts = 0 ;  
  }  
  ppFive1 = 0;  
   
   
   
   
   
 }  
 void TurnLightFiveOn2()  
 {  
   
  if (!LightFiveSts)  
  {  
   digitalWrite(lightFive, LOW);  
   LightFiveSts = 1 ;  
   Delay15S();  
   
  }  ppFive2 = 1;  
   
 }  
 void TurnLightFiveOff2()  
 {  
  if (!ppFive1)  
  {  
   
   
   digitalWrite(lightFive, HIGH);  
   LightFiveSts = 0 ;  
  }  
  ppFive2 = 0;  
 }  
   
   
   
   
 void TurnLightSixOn1()  
 {  
  if (!LightSixSts)  
  {  
   digitalWrite(lightSix, LOW);  
   LightSixSts = 1 ;  
   
   Delay15S();  
   
  }  
  ppSix1 = 1;  
   
 }  
   
 void TurnLightSixOff1()  
 {  
   
  if (!ppSix2)  
   
  {  
   
   
   digitalWrite(lightSix, HIGH);  
   LightSixSts = 0 ;  
  }  
  ppSix1 = 0;  
   
   
 }  
   
 void TurnLightSixOn2()  
 {  
   
  if (!LightSixSts)  
  {  
   digitalWrite(lightSix, LOW);  
   LightSixSts = 1 ;  
   Delay15S();  
   
  }  ppSix2 = 1;  
   
 }  
 void TurnLightSixOff2()  
 {  
  if (!ppSix1)  
   
  {  
   
   
   digitalWrite(lightSix, HIGH);  
   LightSixSts = 0 ;  
  }  
  ppSix2 = 0;  
   
 }  
   
   
   
 //#####################  
 // RTC Test ##########  
 //#####################  
 void rtctest()  
 {  
    
  if (Wire.endTransmission() == 0) //Did the RTC respond?  
   
  {  
   
   
   
   RTCerr = 0;    // set RTC error status boolean to false  
   digitalWrite (lightBlink, LOW); // blink status LED  
   
  }  
   
  else  
  {  
   
   RTCerr = 1;  
   digitalWrite (lightBlink, HIGH);  
   
  }  
   
 }  
   
   
   

D.I.Y. Arduino Timer Auto Lights Switch using arduino Nano,Ds3231rtc,relays v1.0 [OUT-DATED]


//*****************************************************************************************************************
//*****************************************************************************************************************


//************<<<< Arduino Nano, DS3231 Real Time Clock & Two Relay Module Lights Timer V1.0 >>>>******************


//*****************************************************************************************************************

//*****************************************************************************************************************





//UPDATED ON DEC 2017







//*****************************************************************************************************************


//*********************<<<< PLEASE VISIT / SUBSCRIBE MY YOUTUBE CHANNEL >>>>***************************************




//*********************<<<<       Youtube.com/CrazyGuyOfficial        >>>>*****************************************



//*****************************************************************************************************************











//**********************//**********************//**********************
//        PINS AND CONNECTIONS
//**********************//**********************//**********************

const byte lightone   =  5;       // Light one relay (LOW Level Relay)
const byte lighttwo   =  6 ;       // Light Two Relay
const byte lightthree =  4 ;       // Status Led    // 13 for arduino builtin LED 

       // DS3231 RTC "SDA to A4",  "SCL to A5"  "Vcc to 5V arduino pin Gnd to Gnd"


//**********************//**********************//**********************
//       Timer ON/OFF Time Settings
//**********************//**********************//**********************


const int OnTime  =  14 ;  // Hour when Light will turn ON (24 hr format NO zeros before single digits)
const int OffTime =  8 ;   // Hour when Light will turn OFF (24 hr format NO zeros before single digits)






//*****************************************************************************************************************


bool LightStatus  =  0 ;       






//*****************************************************************************************************************
// INCLUDE LIBRARIES
//*****************************************************************************************************************






#include <Time.h>        // Time Manipulation

#include <Wire.h>
#include <DS1307RTC.h>   // DS1307 RTC
#include <TimeAlarms.h>  // time alarm library //Changed Number of  alarms " dtNBR_ALARMS to 30 "
                         //"BY EDITING TIMEALARM.H FILE IN LIBRARIES FOLDER.
                         //increasing this number increases size of the sketch too





#define DS1307_ID 0x68    //Address of the RTC




//*****************************************************************************************************************
// SETUP BEGINS
//*****************************************************************************************************************


void setup() {

  pinMode(lightone,   OUTPUT);
  pinMode(lighttwo,   OUTPUT);
  pinMode(lightthree, OUTPUT);




  // THIS WILL PREVENT STARTUP RELAY TRIGGERING PROBLEM



  digitalWrite (lightone, HIGH);
  digitalWrite (lighttwo, HIGH);   // turn the relay "OFF" and turning the pin "LOW" turns the relay "ON"



















  //**********************//**********************//***********************//**********************
  //Check to see if the RTC is present.if yes then Set the ARDUINO's INTERNAL clock accordingly
  //**********************//**********************//***********************//**********************


  // when using timer library the Arduino's "internal clock " is used for getting "current time"
  // Arduino's internal clock is NOT so accurate and gets off time so we use time from RTC (which IS
  //accurate atleast for our use) and we sync Arduino's internal Clock with the time from RTC and we
  //have to do this periodically (atleast once in every 24 hrs )so our tasks can be completed according
  //to the correct time // we can put the sync command in the Loop and Alarm Functions too

  Wire.beginTransmission(DS1307_ID);
  Wire.write((uint8_t)0x00);


    setSyncProvider(RTC.get);         //Yes it did, Sync the time from the RTC

    setSyncInterval(60);

  
  
 
   



    Serial.begin(9600);              
    
  


















  //***********************//**********************//***********************//**********************

  //  TIMER ALARM THAT RUNS OVER AND OVER AFTER SECONDS (used for status led)

  //***********************//**********************//***********************//**********************



  Alarm.timerRepeat(10, Repeats);  // timer for every 10 seconds blinking LIGHT  (status LED)













}

//*****************************************************************************************************************
//                             SETUP END
//*****************************************************************************************************************










//***********************//**********************//***********************//**********************
//************// Function Called When Repeating Timer alarm triggers //*******************
//***********************//**********************//***********************//**********************
void Repeats() {


  Serial.println("******10 second timer******");

  digitalWrite(lightthree, HIGH); 

  Alarm.delay(30);

  digitalWrite(lightthree, LOW);

  
  
  digitalClockDisplay();      // Calls the function to Display  the Clock IN SERIAL MONITOR



if (LightStatus ==1) {
  
  Serial.println ("< Lights ON >");
  
  
  
  
  }
  
  
  else {
    
      Serial.println ("< Lights OFF >");

    
  }



}










//*****************************************************************************************************************


//                    LOOP BEGINS


//*****************************************************************************************************************




void loop() {


  Alarm.delay (2000);




  if (hour() == 0 && minute() == 0 && second() == 0)  // at 00:00:00   MIDNIGHT UPDATE the Arduino's INTERNAL Clock



  {
    setSyncProvider(RTC.get);                         //It's time, sync  the Arduino's INTERNAL Clock  to the RTC
  }










  //**********************************************************

  // Turn Lights On/Off

  //**********************************************************

  if (OffTime > OnTime) {     // Same Day


    if (hour() >= OnTime && hour () <= OffTime - 1)   { 
                              
                              // if current hour is equal or greater than ON time
                              // AND equal to or less than OFF time minus 1  then turn the
                              // lights ON (by turning pins LOW because of using LOW level Relays)
                              // Minus 1 from OFF time because we are not using Minutes here
                              // For Example turn off time is "16" (4 PM) the hour number will be "16" from
                              // 16:00 till 16:59 (4:00 till 4:59)

      digitalWrite(lightone, LOW);


      Alarm.delay (1000);


      digitalWrite(lighttwo, LOW);

       LightStatus  =  1 ; 
    
    
    }

    

    else {

      digitalWrite(lightone, HIGH);

      digitalWrite(lighttwo, HIGH);


      LightStatus  =  0 ;
   
    
    }

 
  
  }


  //**********************************************************


  if (OffTime < OnTime) {     // Different Day



    if (hour() >= OnTime || hour () <= OffTime - 1) {        
      
                              // if current hour is equal or greater than ON time
                              // OR equal to or less than OFF time minus 1  then turn the
                              // lights ON (by turning pins LOW because of using LOW level Relays)
                              // Minus 1 from OFF time because we are not using Minutes here
                              // For Example turn off time is "16" (4 PM) the hour number will be "16" from
                              // 16:00 till 16:59 (4:00 till 4:59)
                              // Minutes,Seconds even day,date,year can be used by adding code


      digitalWrite(lightone, LOW);



      Alarm.delay (1000);



      digitalWrite(lighttwo, LOW);



      LightStatus  =  1;


    }



    else {





      digitalWrite(lightone, HIGH);





      digitalWrite(lighttwo, HIGH);


      
      LightStatus  =  0 ;




    }


  
  
  
  
  
  }







  //*****************************************************************************************************************

  //    DAILY TIMED ALARMS THAT TRIGGER ON SPECIFIED TIMES

  // these alarms will trigger once and will do "stuff" mentioned in the alarm functions at the end of the loop


  //  IF Alarm function is used to Turn Pins ON/OFF it will but there's a Reset/Power Dwn/Up cycle it will not work after 
  // the Specified Alarm time has passed and pin will go back to it's initial state untill alarm changes it again 
  //*****************************************************************************************************************





  Alarm.alarmRepeat(3, 0, 0, ONAlarm); // daily Alarm 1  an alarm on ""03:00:00"" in the morning



  Alarm.alarmRepeat(4, 0, 0, ONAlarm); // daily Alarm 2  an alarm on ""04:00:00""  in the morning



  Alarm.alarmRepeat(5, 0, 0, ON2Alarm); // daily Alarm 3



  Alarm.alarmRepeat(6, 0, 0, ONAlarm); // daily Alarm 4



  Alarm.alarmRepeat(7, 0, 0, ONAlarm); // daily Alarm 5



  Alarm.alarmRepeat(8, 0, 0, ON2Alarm); // daily Alarm 6










  Alarm.alarmRepeat(9, 0, 0, ONAlarm); // daily Alarm 7



  Alarm.alarmRepeat(10, 0, 0, ONAlarm); // daily Alarm 8


  Alarm.alarmRepeat(11, 0, 0, ON2Alarm); // daily Alarm 9



  Alarm.alarmRepeat(12, 0, 0, ONAlarm); // daily Alarm 10


  Alarm.alarmRepeat(13, 0, 0, ONAlarm); // daily Alarm 11



  Alarm.alarmRepeat(14, 0, 0, ON2Alarm); // daily Alarm 12








  Alarm.alarmRepeat(15, 0, 0, ONAlarm); // daily Alarm 13



  Alarm.alarmRepeat(16, 0, 0, ONAlarm); // daily Alarm 14



  Alarm.alarmRepeat(17, 0, 0, ON2Alarm); // daily Alarm 15



  Alarm.alarmRepeat(18, 0, 0, ONAlarm); // daily Alarm 16



  Alarm.alarmRepeat(19, 0, 0, ONAlarm); // daily Alarm 17



  Alarm.alarmRepeat(20, 0, 0, ON2Alarm); // daily Alarm 18



  Alarm.alarmRepeat(21, 0, 0, OFFAlarm); // daily Alarm 19



  Alarm.alarmRepeat(22, 0, 0, OFFAlarm); // daily Alarm 20




  Alarm.alarmRepeat(23, 0, 0, OFF2Alarm); // daily Alarm 21



  Alarm.alarmRepeat(0, 0, 0, OFFAlarm); // daily Alarm 22



  Alarm.alarmRepeat(1, 0, 0, OFFAlarm); // daily Alarm 23



  Alarm.alarmRepeat(2, 0, 0, OFF2Alarm); // daily Alarm 24






}





//**********************//**********************//**********************
//                             LOOP END
//**********************//**********************//**********************








//**********************//**********************//**********************//**********************

//Functions called when daily time specific alarms (above in the loop) triggers:

//***********************************************//**********************//**********************


void ONAlarm() {


}


void ON2Alarm() {



  setSyncProvider(RTC.get);        //It's time, sync  the Arduino's INTERNAL Clock  to the RTC

  Serial.println("TIME SYNCHRONIZED");


}


void OFFAlarm() {

}



void OFF2Alarm() {


  setSyncProvider(RTC.get);       //It's time, sync  the Arduino's INTERNAL Clock  to the RTC

  Serial.println("TIME SYNCHRONIZED");

}





//**********************//**********************//**********************//**********************

//Function for digital clock display (in the SERIAL MONITOR ) 

//**********************//**********************//**********************//**********************




void digitalClockDisplay() {    // digital clock display of the time 24hr format






  Serial.print(hour());

  printDigits(minute());

  printDigits(second());

  Serial.println("");

  Serial.println(dayStr(weekday()));

  Serial.print(monthStr(month()));

  Serial.print("  ");

  Serial.print(day());

  Serial.print("  ");

  Serial.println(year());
}




void printDigits(int digits) {
  Serial.print(":");
  if (digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

//***********************************************//**********************//**********************



// Un comment the function below and comment the above only one can be used in the code or change names

/*void digitalClockDisplay()      //// digital clock display of the time 12hr format

  {


  Serial.print(hourFormat12());


  printDigits(minute());

  printDigits(second());

  Serial.println("");

  Serial.println(dayStr(weekday()));

  Serial.print(monthStr(month()));

  Serial.print("  ");

  Serial.print(day());

  Serial.print("  ");

  Serial.println(year());

  //Alarm.delay(1000);

  }



  void printDigits(int digits)
  {

  Serial.print(":");


  if (digits < 10) {


    Serial.print('0');


  }


  Serial.print(digits);


  }
*/