////////////////////////////////////////////////////////////////////////////////
// Name:       RS232uno-01                                                    //
// Platform:   Arduino UNO R3                                                 //
// Created by: HARB rboek2@gmail.com November 2019 GPL copyrights             //
// http://robotigs.com/robotigs/includes/bots_header.php?idbot=22             //
// This program is made to switch 2 relais: 1 timer & 1 temperature           //
// Relais 1: Relay1 can switch growleds on for any set period                 //
//           per day through an internal clock.                               //
//           http://robotigs.nl/robotigs/includes/parts_header.php?idpart=289 //
// Relais 2: Maintain a set temperature in a space. This is f.e.              //
//           to optimize for propagating seeds.                               //
//           http://robotigs.nl/robotigs/includes/parts_header.php?idpart=180 //
// The settings can be adjusted with any browser from any device and will be  //
// communicated to this robot by Ethernet.                                    //
//           http://robotigs.nl/robotigs/includes/parts_header.php?idpart=53  //
////////////////////////////////////////////////////////////////////////////////




////////////////////////////////////////////////////////////////////////////////
// EEPROM MEMORY MAP:                                                         //
// Start End  Number Description                                              //
// 0000  0000    1        Never use this memory location to be AVR compatible //
// 0001  0001    1   WERKVERLICHTING hobbyLightProg: 1=off 2=on 3=auto RELAY1 //
// 0002  0002    1     If LDR reaches this kasLightON*10 switch        RELAY1 //
// 0003  0003    1     kasLightSecs*10 werkverlichting on              RELAY1 //
// 0004  0004    1   VERWARMING hobbyHeatProg: 1=off 2=on 3=auto       RELAY2 //
// 0005  0005    1     Celsius hobbyHeatON/10 verwarming switch on     RELAY2 //
// 0006  0006    1     Celsius hobbyHeatOFF/10 verwarming switch off   RELAY2 //
// 0007  0007    1   GROEILED hobbyLedProg: 1=off 2=on 3=auto          RELAY3 //
// 0008  0008    1     Hours kasLedHours around noon to switch on      RELAY3 //
// 0009  0009    1   WATER hobbyWaterProg: 1=off 2=on 3=auto           RELAY4 //
// 0010  0010    1     hobbyWaterSecs*10 to keep watering              RELAY4 //
// 0011  0011    1   AUDIO hobbyAudioProg: 1=off 2=on 3=auto           RELAY5 //
// 0012  0012    1     hobbyAudioMins*10 to keep audio playing         RELAY5 //
////////////////////////////////////////////////////////////////////////////////




// SET PRECOMPILER OPTIONS *****************************************************
  //Initialse conditional compiling, uncomment to include, comment to exclude --
  // Do comment for runtime versions
  //#define RS232                 //Uncomment to include Serial Monitor sections

  //Define the needed header files for the precompiler, no charge if not used --
  #include <EEPROM.h>                       //Needed for read or write in EEPROM
             // http://robotigs.nl/robotigs/includes/parts_header.php?idpart=312
  #include <RTClib.h>        //Manipulates clock DS1307 via I2C needs Wire.h lib
             // http://robotigs.nl/robotigs/includes/parts_header.php?idpart=289
  #include <Wire.h>                //Needed ao by RTClib: Two Wire Interface lib
              // http://robotigs.nl/robotigs/includes/parts_header.php?idpart=31
  #include <OneWire.h>    //Library can be installed through Arduino IDE DS18B20
             // http://robotigs.nl/robotigs/includes/parts_header.php?idpart=180

  //Define PINS ----------------------------------------------------------------
  OneWire term1(8);           //Connects to pin 8, but may be any DIO pin DS1820
  #define ledRedPin     3         //3 Colour LED, which PWM pin connects RED LED
  #define ledGrePin     5       //3 Colour LED, which PWM pin connects GREEN LED
  #define ledBluPin     6        //3 Colour LED, which PWM pin connects BLUE LED
  #define Relay1Pin     8                   //220Vac switch DIO pin TIMER RELAY1
  #define Relay2Pin    A3             //220Vac switch DIO pin TEMPERATURE RELAY2

  //Define DATABASE VARIABLES --------------------------------------------------
  String hobbyKlok      = "2019-02-01 23:59:59";               //DateTime DS1307
  int    hobbyRelay1    = HIGH;  //WERKVERLICHTING status high=off low=on RELAY1
  int    hobbyRelay2    = HIGH;       //VERWARMING status high=off low=on RELAY2

  //Define DATABASE & EEPROM variables -----------------------------------------
  int    hobbyLightProg = 1; //WERKVERLICHTING program: 1=off 2=on 3=auto RELAY1
  int    hobbyLightON   = 43;           //If LDR reaches this treshold*10 RELAY1
  int    hobbyLightSecs = 30;             //Seconds*10 werkverlichting on RELAY1
  int    hobbyHeatProg  = 1;      //VERWARMING program: 1=off 2=on 3=auto RELAY2
  int    hobbyHeatON    = 10;           //Celsius/10 verwarming switch on RELAY2
  int    hobbyHeatOFF   = 30;          //Celsius/10 verwarming switch off RELAY2
  int    hobbyLEDProg   = 1;        //GROEILED program: 1=off 2=on 3=auto RELAY3
  int    hobbyLEDHours  = 14;            //Hours around noon to switch on RELAY3
  int    hobbyWaterProg = 1;           //WATER program: 1=off 2=on 3=auto RELAY4
  int    hobbyWaterSecs = 6;                //Seconds*10 to keep watering RELAY4
  int    hobbyAudioProg = 1;          //AUDIO programm: 1=off 2=on 3=auto RELAY5
  int    hobbyAudioMins = 6;           //Minutes*10 to keep audio playing RELAY5

  // Define variables ----------------------------------------------------------
  byte    present        = 0;   //Used for oneWire, present = ds.reset() DS18B20
  byte    i;                  //Used for oneWire, loopcounter byte array DS18B20
  byte    data[12];           //Used for oneWire to store data read from DS18B20
  byte    type1_s        = 0;        //Type 0 = ok, except old DS1820=1, DS18B20
  byte    addr1[8];              //Array with first 8 bytes, inc/address DS18B20
  float   DS1820temp     =  0.0;         //Temperature in Celsius SENSOR DS18B20
  
  int    ledOnBoardVal  = LOW;   //You choose HIGH-on or LOW-off for LED_BUILTIN
  int    ledbrillance   = 50;        //Brightness of all LED colors PWM TEST LED
  byte   ledmsWait      = 5;            //Test your patience during the TEST LED
  
  byte   ledRedPWM      = 4;         //Current brightness 1-4 PWM TOGGLE RED LED
  byte   ledGrePWM      = 20;     //Current brightness 1-20 PWM TOGGLE GREEN LED
  byte   ledBluPWM      = 1;        //Current brightness 1-3 PWM TOGGLE BLUE LED
  
  word   readCounter    = 0;      //Read sensors if counted down to zero SENSORS
  word   readTimer      = 999;    //Fill readCounter after reaching zero SENSORS
  char   ssid[]         = "Ranonkel9";                //Network SSID (name) WIFI
  char   pass[]         = "Kat14_-5";                    //Network password WIFI
  String html           = "";                //HTML Response preapaired INTERNET
  int    command        = 0;            //Which user command to perform INTERNET
  String commandStr     = "";                   //Create receive string INTERNET
  int    bodyLength     = 0;                       //HTML answer length INTERNET
  char   buf[100];                //Needed to display the date/time stamp DS1307
  int    currenthour;      //Compare with starthours and finishhours for GROWLED
  int    starthours;                                   //Switch ON clock GROWLED
  int    finishhours;                                 //Switch OFF clock GROWLED
  static unsigned long wateringOFFtimer = millis()+ 300000;           //WATERING
  static unsigned long LightOFFtimer = millis()+ 300000;         //WORKING LIGHT
  String tmp            = "";                             //Can be used anywhere
  String tmpa           = "";                             //Can be used anywhere
  int    tmp1;                                            //Can be used anywhere
  int    tmp2;                                            //Can be used anywhere

  //Initialize OBJECTS ---------------------------------------------------------
  DS1307 rtc;                         //Initialize Real Time Clock object DS1307
//END OF PRECOMPILER OPTIONS ---------------------------------------------------


void setup() { //Setup runs once ***********************************************
  disable_jtag();       //Disable jtag to free port C, enabled by default SYSTEM

  //EEPROMfirstTime();   //First time use only, write factory settings to EEPROM
  hobbyLightProg = EEPROM.read(1);    //WERKVERLICHTING 1=off 2=on 3=auto RELAY1
  hobbyLightON   = EEPROM.read(2);      //If LDR reaches this treshold*10 RELAY1
  hobbyLightSecs = EEPROM.read(3);        //Seconds*10 werkverlichting on RELAY1
  hobbyHeatProg  = EEPROM.read(4); //VERWARMING program 1=off 2=on 3=auto RELAY2
  hobbyHeatON    = EEPROM.read(5);      //Celsius/10 verwarming switch on RELAY2
  hobbyHeatOFF   = EEPROM.read(6);     //Celsius/10 verwarming switch off RELAY2
  hobbyLEDProg   = EEPROM.read(7);  //GROEILED program: 1=off 2=on 3=auto RELAY3
  hobbyLEDHours  = EEPROM.read(8);       //Hours around noon to switch on RELAY3
  hobbyWaterProg = EEPROM.read(9);     //WATER program: 1=off 2=on 3=auto RELAY4
  hobbyWaterSecs = EEPROM.read(10);         //Seconds*10 to keep watering RELAY4
  hobbyAudioProg = EEPROM.read(11);   //AUDIO programm: 1=off 2=on 3=auto RELAY5
  hobbyAudioMins = EEPROM.read(12);    //Minutes*10 to keep audio playing RELAY5
  calculateGrowLED();               //Calculate start and finisch clock GROEILED

  Serial.begin(57600);         //Nothing more needed for the Serial Monitor WIFI
  pinMode(LED_BUILTIN, OUTPUT);  //Arduino boards contain an onboard LED_BUILTIN
  pinMode(ledRedPin, OUTPUT);           //Make the LED connection output RED LED
  pinMode(ledGrePin, OUTPUT);         //Make the LED connection output GREEN LED
  pinMode(ledBluPin, OUTPUT);          //Make the LED connection output BLUE LED
  pinMode(Relay1Pin, OUTPUT);                //Make the switch output pin RELAY1
  pinMode(Relay2Pin, OUTPUT);                //Make the switch output pin RELAY2
  analogWrite(ledRedPin, 0);             //Set the initial brightness of RED LED
  analogWrite(ledGrePin, 0);           //Set the initial brightness of GREEN LED
  analogWrite(ledBluPin, 0);            //Set the initial brightness of BLUE LED

  //Start objects --------------------------------------------------------------
  //DS1820_init();      //Determins the type of DS1820 and reads properties DS1820
  //Wire.begin();                 //Start the Two Wire Interface object I2C DS1307
  //writeI2CRegister8bit(0x20, 6);     //Reset sensor to tell it is a slave DS1307
  //rtc.begin();    //Initialize Wire.begin first. Start the object running DS1307
  //rtc.adjust(DateTime(__DATE__, __TIME__));      //Set to time compiled DS1307
  
  //Test hardware and software -------------------------------------------------
  //test_RELAY();                            //Switches ON for 2 seconds all RELAY
  test_LEDs();       //PWM fade in and fade out for 3 colorLEDs on board ALL LED
} //End of setup ---------------------------------------------------------------



void loop() { //KEEP ON RUNNING THIS LOOP FOREVER ******************************
  DateTime now = rtc.now();  //Read the current time into the object from DS1307
  strncpy(buf,"YYYY-MM-DD hh:mm:ss\0",100);  //Format string for the time DS1307
  hobbyKlok = now.format(buf);      //Format the timestap into a variable DS1307
  strncpy(buf,"hh\0",100);                  //Format string for the time GROWLED
  tmp = now.format(buf);           //Format the timestap into a variable GROWLED
  currenthour = tmp.toInt();            //Convert to value for switching GROWLED
  readSensors();          //Read several sensors at timed intervals only SENSORS
  setActuators();                                //Calculate and set all OUTPUTS
  http_check();     //See if we received a http request and reply if so INTERNET
} //End of void loop() ----------------------- KEEP ON RUNNING THIS LOOP FOREVER



void readSensors() { //Read several sensors at timed intervals only ************
  if (readCounter == 0){       //Only perform measurements if counted down TIMER
    readCounter  =  readTimer;                         //RESET the counter TIMER
    DS1820_read();                //Reads the temperature in Celsius from DS1802
    refreshAnswer();                  //Replace the old answer by a new one DATA
    toggleGreenLed();                          //Toggles ON or OFF the GREEN LED
    setActuators();                              //Calculate and set all OUTPUTS
  }else{                                //Meaning counter was not yet zero TIMER
    readCounter--;                        //Decrement of the timer counter TIMER
  } //End of if (moistureCnt1 == 0)Perform measurements if counted down    TIMER
} //Exit readSensors -----------------------------------------------------------




void setActuators(){ //Calculate and set all OUTPUTS ***************************
  setRelay1();                //WERKVERLICHTING switch, calculate and set RELAY1
  setRelay2();                     //VERWARMING switch, calculate and set RELAY2
} //Exit setActuators ----------------------------------------------------------




void http_check(void) { //See if we received a http request and reply if so ****
  
  commandStr = "";                                        //Reset receive string
  while (Serial.available() > 0) {   //Check if any request is made by a browser
    commandStr = String(commandStr + Serial.readString());       //Read incoming
  } //End of if (Serial.available() > 0)              Entire block has been read

  if (commandStr != "") {                     //Did we really receive a request?
    if (isDigit(commandStr[5])){              //Check if we received any command
      tmp = commandStr.substring(5, 9);              //Extract command 0001-9999
      command = tmp.toInt();            //Translate the function to a executable
      switch (command) {                         //Go to the according procedure


        case 11: //************************ Instructie 11 => WERKVERLICHTING off
          hobbyLightProg = 1;          //0=unknown, 1=off, 2=on, 3=auto, set OFF
          EEPROM.write(1, 1);                         //Write 1 byte into EEPROM
          setRelay1();                        //Switch, calculate and set RELAY1
        break; //End of case 11:            Instructie 11 => WERKVERLICHTING off

        case 12: //************************* Instructie 12 => WERKVERLICHTING on
          hobbyLightProg = 2;           //0=unknown, 1=off, 2=on, 3=auto, set ON
          EEPROM.write(1, 2);                         //Write 1 byte into EEPROM
          setRelay1();                        //Switch, calculate and set RELAY1
        break; //End of case 12:             Instructie 12 => WERKVERLICHTING on

        case 13: //*********************** Instructie 13 => WERKVERLICHTING auto
          hobbyLightProg = 3;         //0=unknown, 1=off, 2=on, 3=auto, set AUTO
          EEPROM.write(1, 3);                         //Write 1 byte into EEPROM
          setRelay1();                        //Switch, calculate and set RELAY1
        break; //End of case 13:           Instructie 13 => WERKVERLICHTING auto



        case 21: //***************************** Instructie 21 => VERWARMING off
           hobbyHeatProg = 1;          //0=unknown, 1=off, 2=on, 3=auto, set OFF
           EEPROM.write(4, 1);                        //Write 1 byte into EEPROM
           setRelay2();                       //Switch, calculate and set RELAY2
         break; //End of case 21:                Instructie 21 => VERWARMING off

        case 22: //****************************** Instructie 22 => VERWARMING on
           hobbyHeatProg = 2;           //0=unknown, 1=off, 2=on, 3=auto, set ON
           EEPROM.write(4, 2);                        //Write 1 byte into EEPROM
           setRelay2();                       //Switch, calculate and set RELAY2
         break; //End of case 22:                 Instructie 22 => VERWARMING on

        case 23: //**************************** Instructie 23 => VERWARMING auto
           hobbyHeatProg = 3;         //0=unknown, 1=off, 2=on, 3=auto, set AUTO
           EEPROM.write(4, 3);                        //Write 1 byte into EEPROM
           setRelay2();                       //Switch, calculate and set RELAY2
         break; //End of case 23:               Instructie 23 => VERWARMING auto



        case 209: //************************ Adjust clock with given time DS1307
          hobbyKlok = "";    //Formatteer zowel nieuw antwoord alsook zetformaat
            tmp = commandStr.substring(10, 14);       //Extract jaar naar string
            hobbyKlok = tmp;                        //Voeg toe aan http response
            hobbyKlok += "-";                       //Voeg toe aan http response
            int jaar = tmp.toInt();            //Maak een bruikbare klok setting

            tmp = commandStr.substring(15, 17);      //Extract maand naar string
            hobbyKlok += tmp;
            hobbyKlok += "-";
            int maand = tmp.toInt();           //Maak een bruikbare klok setting

            tmp = commandStr.substring(18, 20);        //Extract dag naar string
            hobbyKlok += tmp;
            hobbyKlok += " ";
            int dag = tmp.toInt();             //Maak een bruikbare klok setting
              
            tmp = commandStr.substring(21, 23);        //Extract uur naar string
            hobbyKlok += tmp;
            hobbyKlok += ":";
            int uur = tmp.toInt();             //Maak een bruikbare klok setting

            tmp = commandStr.substring(24, 26);     //Extract minuut naar string
            hobbyKlok += tmp;
            hobbyKlok += ":";
            int minuut = tmp.toInt();          //Maak een bruikbare klok setting

            tmp = commandStr.substring(27, 29);    //Extract seconde naar string
            hobbyKlok += tmp;
            int seconde = tmp.toInt();         //Maak een bruikbare klok setting

            rtc.adjust(DateTime(jaar, maand, dag, uur, minuut, seconde));
            // <----------------------SET TIME
         break; //End of case 209:           Adjust clock with given time DS1307
                     
      } //End of switch (command) the list with possible precedures ------------
    } //End of if (isDigit(Request[6])         /Check if we received any command

    sendHttpResponse();                               //Send a HTTP respnse WIFI
  } //End of if (Request != "") {              //Did we really receive a request
} //Exit http_check, end of See if we received a http request and reply if so---




void setRelay1(){ //WERKVERLICHTING switch, calculate and set RELAY1 **********
  switch (hobbyLightProg) {       //Frontlight program: 1=off 2=on 3=auto RELAY1

    case 1:                              //Program = 1 = Set WERKVERLICHTING OFF
      hobbyRelay1 = HIGH;     //Status HIGH=off or LOW=on WERKVERLICHTING RELAY1
    break;                     //End of Program = 1 = Set program FRONTLIGHT OFF
    
    case 2:                               //Program = 2 = Set WERKVERLICHTING ON
      hobbyRelay1 = LOW;        //Status HIGH=off or LOW=on WORKING LIGHT RELAY1
    break;                           //End of Program = 2 = Set WORKING LIGHT ON
    
    case 3:                             //Program = 3 = Set WERKVERLICHTING AUTO
      if (10 > hobbyLightON and LOW == HIGH) {                //Treshold then ON
        hobbyRelay1 = LOW;   //Status HIGH=off or LOW=on WORKING LIGHT ON RELAY1
        LightOFFtimer = millis() + hobbyLightSecs*1000L;   //Set SWTICH OFF TIME
      }  //End of                        If measurement reaches treshold then ON
    break;                            //End of Program = 3 = Set FRONTLIGHT AUTO
    
  }                                               //End of switch frontLightProg
  digitalWrite(Relay1Pin, hobbyRelay1);         //Switches WORKING LIGHTS RELAY1
} //Exit setRelay1 -------------------------------------------------------------




void setRelay2(){ //AIR HEATER switch, calculate and set RELAY2 ****************
  switch (hobbyHeatProg) {        //AIR HEATER program: 1=off 2=on 3=auto RELAY2

    case 1:                           //Program = 1 = Set program AIR HEATER OFF
      hobbyRelay2 = HIGH;          //Status HIGH=off or LOW=on AIR HEATER RELAY2
    break;                     //End of Program = 1 = Set program AIR HEATER OFF

    case 2:                            //Program = 2 = Set program AIR HEATER ON
      hobbyRelay2 = LOW;           //Status HIGH=off or LOW=on AIR HEATER RELAY2
    break;                      //End of Program = 2 = Set program AIR HEATER ON

    case 3:                          //Program = 3 = Set program AIR HEATER AUTO
      if (100 < hobbyHeatON){                  //If treshold measurement TURN ON
        hobbyRelay2 = LOW;      //Status HIGH=off or LOW=on AIR HEATER ON RELAY2
      } //End of                                 If treshold measurement TURN ON
      if (10 > hobbyHeatOFF){                 //If treshold measurement TURN OFF
        hobbyRelay2 = HIGH;     //Status HIGH=off or LOW=on AIR HEATER ON RELAY2
      } //End of                                If treshold measurement TURN OFF
    break;                    //End of Program = 3 = Set program AIR HEATER AUTO

  }                                                   //End of switch AIR HEATER
  digitalWrite(Relay2Pin, hobbyRelay2);                        //Switches RELAY2
} //Exit setRelay2 -------------------------------------------------------------








void sendHttpResponse() { //Send a HTTP respnse WIFI ***************************
  refreshAnswer();                    //Replace the old answer by a new one DATA
  Serial.println("HTTP/1.1 200 OK");          //Start answer to the request WIFI
  Serial.println("Connection: close");       //Close after html is finished WIFI
  Serial.print("Content-Length: ");     //Finish html after amount of chars WIFI
  Serial.println (bodyLength);   //Name the amount of calculated characters WIFI
  Serial.println("Content-Type: text/html");      //Needed to be compatible WIFI
  Serial.println(" /n \n");                     //Needed to end the headers WIFI
  Serial.println(html);      //Broadcast the message to be shown in browser WIFI
} //Exit sendHttpResponse ------------------------------------------------------



void refreshAnswer(void) { //Replace the old answer by a new one WIFI **********
                 //First but not least tell the client our current sensor values
  html =  String(hobbyKlok) + " ";         //DateTime 2019-02-01 23:59:59 DS1307
  html += String(DS1820temp) + " ";      //Temperature in Celsius SENSOR DS18B20
  
  //Next tell the client about the status of all relay
  html += String(hobbyRelay1) + " ";    //WERKVERLICHTING high=off low=on RELAY1
  html += String(hobbyRelay2) + " ";         //VERWARMING high=off low=on RELAY2

  //Now tell the current EEPROM values
  html += String(hobbyLEDProg) + " ";         //GrowLED 1=off 2=on 3=auto RELAY1
  html += String(hobbyLEDHours) + " ";  ///Hours around noon to switch on RELAY1
  
  html += String(hobbyHeatProg) + " ";    //VERWARMING: 1=off 2=on 3=auto RELAY2
  html += String(hobbyHeatON) + " ";     //Celsius kasHeatON/10 switch on RELAY2
  html += String(hobbyHeatOFF) + " ";  //Celsius kasHeatOFF/10 switch off RELAY2
  //Eventually here can follow some current counters              if interesting

  bodyLength = html.length();  //Calculate the number of characters to sent WIFI
} //Exit refreshAnswer ---------------------------------------------------------




void writeI2CRegister8bit(int addr, int value){  //Reset sensor DHT22 **********
  Wire.beginTransmission(addr);
  Wire.write(value);
  Wire.endTransmission();
} //Exit writeI2CRegister8bit --------------------------------------------------




void calculateGrowLED() {             //Calculate start and finish clock GROWLED
  starthours  = 12 - (hobbyLEDHours / 2);     //Calculate switch ON time GROWLED
  finishhours = 12 + (hobbyLEDHours / 2);    //Calculate switch OFF time GROWLED
} //Exit calculateGrowLED ------------------------------------------------------




void EEPROMfirstTime() { //First time use only, write factory settings to EEPROM
  EEPROM.write( 1, 1);  //WERKVERLICHTING kasLightProg: 1=off 2=on 3=auto RELAY1
  EEPROM.write( 2, 10);        //If LDR reaches this kasLightON*10 switch RELAY1
  EEPROM.write( 3, 11);              //kasLightSecs*10 werkverlichting on RELAY1
  EEPROM.write( 4, 1);        //VERWARMING kasHeatProg: 1=off 2=on 3=auto RELAY2
  EEPROM.write( 5, 50);       //Celsius kasHeatON/10 verwarming switch on RELAY2
  EEPROM.write( 6, 70);     //Celsius kasHeatOFF/10 verwarming switch off RELAY2
  EEPROM.write( 7, 1);           //GROEILED kasLedProg: 1=off 2=on 3=auto RELAY3
  EEPROM.write( 8, 10);      //Hours kasLedHours around noon to switch on RELAY3
  EEPROM.write( 9, 1);            //WATER kasWaterProg: 1=off 2=on 3=auto RELAY4
  EEPROM.write(10, 6);                 //kasWaterSecs*10 to keep watering RELAY4
  EEPROM.write(11, 1);            //AUDIO kasAudioProg: 1=off 2=on 3=auto RELAY5
  EEPROM.write(12, 6);            //kasAudioMins*10 to keep audio playing RELAY5
} //Exit EEPROMfirstTime -------------------------------------------------------




void DS1820_read(void) { //Reads the temperature from DS1820 in Celsius ********
  term1.reset();                              //Reset whatever still was running
  term1.select(addr1);                      //Set the parameters for the library
  term1.write(0x44);       //Start conversion, with parasite power on at the end
  delay(800);     //Maybe 750ms is enough, maybe not, takes a lot of time though
  present = term1.reset();              //We assume that the conversion is ready
  term1.select(addr1);                      //Set the parameters for the library
  term1.write(0xBE);                                          // Read Scratchpad
  for ( i = 0; i < 9; i++) {                                   //We need 9 bytes
    data[i] = term1.read();
  }
  int16_t raw = (data[1] << 8) | data[0];                      //Rotate the data
  DS1820temp = (float)raw / 16.0;      //Untill they are in the correct position
} //Exit DS1820_read -----------------------------------------------------------



void DS1820_init(void) { //Determins the type of DS1820 thermometer1 ***********
  if (!term1.search(addr1)) {
    term1.reset_search();
    delay(250);
    return;
  }
  if (OneWire::crc8(addr1, 7) != addr1[7]) {
      return;
  }
  switch (addr1[0]) {         //The first ROM byte indicates which  tupe of chip
    case 0x10:
      type1_s = 1;
      break;
    case 0x28:
      type1_s = 0;
      break;
    case 0x22:
      type1_s = 0;
      break;
    default:
      return;
  } 
  term1.reset();
  term1.select(addr1);
  term1.write(0x44, 1);    //Start conversion, with parasite power on at the end
  delay(800);     //Maybe 750ms is enough, maybe not, takes a lot of time though
  present = term1.reset();
  term1.select(addr1);    
  term1.write(0xBE);                                           //Read Scratchpad
  for ( i = 0; i < 9; i++) {                                   //We need 9 bytes
    data[i] = term1.read();
  }

  int16_t raw = (data[1] << 8) | data[0];
  if (type1_s) {
    raw = raw << 3; // 9 bit resolution default
    if (data[7] == 0x10) {       // "count remain" gives full 12 bit resolution
      raw = (raw & 0xFFF0) + 12 - data[6];
    }
  } else {     //// default is 12 bit resolution, 750 ms conversion time
    byte cfg = (data[4] & 0x60);     
    // at lower res, the low bits are undefined, so let's zero them
    if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
    else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
  }
  DS1820temp = (float)raw / 16.0;
} //Exit DS1820_init -----------------------------------------------------------







void test_RELAY(){ //Switches ON for 2 seconds all RELAY ***********************
  digitalWrite(Relay1Pin, LOW);                         //Switches ON the RELAY1
  delay (2000);                                             //Wait for 2 seconds
  digitalWrite(Relay1Pin, HIGH);                       //Switches OFF the RELAY1
  digitalWrite(Relay2Pin, LOW);                         //Switches ON the RELAY2
  delay (2000);                                             //Wait for 2 seconds
  digitalWrite(Relay2Pin, HIGH);                       //Switches OFF the RELAY2
} //End of test_Relay(){ Switches ON for 2 seconds the RELAY -------------------




void test_LEDs(void){ //PWM fade in and fade out for 4 LEDs on board ***********
  tmp1 = 0;                           //Reset the counter to set PWM up and down

  while (tmp1 < ledbrillance){    //Show LED until maximum brillance reached RED
    analogWrite(ledRedPin, tmp1);             //Set LED to desired PWM value RED
    tmp1++;                                //Increase the brightness by one step
    delay (ledmsWait);                //Built in a pause to make changes visible
  } //End of while (tmp1 < brillance)           Show LED until maximum brillance
  while (tmp1 > 0){      //Show LED until minimum brillance has been reached RED
    analogWrite(ledRedPin, tmp1);             //Set LED to desired PWM value RED
    tmp1--;                                //Decrease the brightness by one step
    delay (ledmsWait);                //Built in a pause to make changes visible
  }//End of while (tmp1 > 0)                    Show LED until minimum brillance
  analogWrite(ledRedPin, 0);                   //Red LED off PWM value = off RED

  while (tmp1 < ledbrillance){  //Show LED until maximum brillance reached GREEN
    analogWrite(ledGrePin, tmp1);           //Set LED to desired PWM value GREEN
    tmp1++;                                //Increase the brightness by one step
    delay (ledmsWait);                //Built in a pause to make changes visible
  } //End of while (tmp1 < brillance)           Show LED until maximum brillance
  while (tmp1 > 0){    //Show LED until minimum brillance has been reached GREEN
    analogWrite(ledGrePin, tmp1);           //Set LED to desired PWM value GREEN
    tmp1--;                                //Decrease the brightness by one step
    delay (ledmsWait);                //Built in a pause to make changes visible
  }//End of while (tmp1 > 0)                    Show LED until minimum brillance
  analogWrite(ledGrePin, 0);               //Green LED off PWM value = off GREEN

  while (tmp1 < ledbrillance){   //Show LED until maximum brillance reached BLUE
    analogWrite(ledBluPin, tmp1);            //Set LED to desired PWM value BLUE
    tmp1++;                                //Increase the brightness by one step
    delay (ledmsWait);                //Built in a pause to make changes visible
  } //End of while (tmp1 < brillance)           Show LED until maximum brillance
  while (tmp1 > 0){     //Show LED until minimum brillance has been reached BLUE
    analogWrite(ledBluPin, tmp1);            //Set LED to desired PWM value BLUE
    tmp1--;                                //Decrease the brightness by one step
    delay (ledmsWait);                //Built in a pause to make changes visible
  }//End of while (tmp1 > 0)                    Show LED until minimum brillance
  analogWrite(ledBluPin, 0);           //Set LED to desired PWM value = off BLUE

  while (tmp1 < ledbrillance){    //Show LED until maximum brillance LED_BUILTIN
    analogWrite(LED_BUILTIN, tmp1);       //Set to desired PWM value LED_BUILTIN
    tmp1++;                                //Increase the brightness by one step
    delay (ledmsWait);                //Built in a pause to make changes visible
  } //End of while (tmp1 < brillance)           Show LED until maximum brillance
  while (tmp1 > 0){       //Show LED until minimum brillance reached LED_BUILTIN
    analogWrite(LED_BUILTIN, tmp1);       //Set to desired PWM value LED_BUILTIN
    tmp1--;                                //Decrease the brightness by one step
    delay (ledmsWait);                //Built in a pause to make changes visible
  }//End of while (tmp1 > 0)                    Show LED until minimum brillance
  analogWrite(LED_BUILTIN, 0);  //Set LED to desired PWM value = off LED_BUILTIN
} //Exit test_LEDs -------------------------------------------------------------




void toggleGreenLed(void){ //Toggles ON or OFF the green LED *******************
  if (ledGrePWM > 0) {                        //Test if the led is on>0 or off=0
    analogWrite(ledGrePin, 0);                 //Set the brightness of LED GREEN
    ledGrePWM = 0;                        //Store the brightness set to this led
  }else{                                  //The led is off so we will turn it on
    analogWrite(ledGrePin,ledbrillance);       //Set the brightness of LED GREEN
    ledGrePWM = ledbrillance;             //Store the brightness set to this led
  } //End of if (ledGrePWM > 0)                 Test if the led is on>0 or off=0
} //Exit toggleGreenLed --------------------------------------------------------




void toggleBlueLed(void){ //Toggles ON or OFF the blue LED *********************
  if (ledBluPWM > 0) {                        //Test if the led is on>0 or off=0
    analogWrite(ledBluPin, 0);                  //Set the brightness of LED BLUE
    ledBluPWM = 0;                        //Store the brightness set to this led
  }else{                                  //The led is off so we will turn it on
    analogWrite(ledBluPin, ledbrillance);       //Set the brightness of LED BLUE
    ledBluPWM = ledbrillance;             //Store the brightness set to this led
  } //End of if (ledBluPWM > 0)                 Test if the led is on>0 or off=0
} //Exit toggleBlueLed ---------------------------------------------------------




void toggle_ledOnBoard(void){ //Toggles the LED_BUILTIN on-board LED on or off *
  ledOnBoardVal = !ledOnBoardVal;                                 //Toggle value
  digitalWrite(LED_BUILTIN, ledOnBoardVal);     //Set Arduino boards onboard LED
} //Exit toggle_ledBin ---------------------------------------------------------




void disable_jtag(void) { //Disable jtag to free port C, enabled by default ****
#if defined(JTD)                           //Not all AVR controller include jtag
  MCUCR |= ( 1 << JTD );                                //Write twice to disable
  MCUCR |= ( 1 << JTD );                                       //So stutter once
#endif                                            //End of conditional compiling
} //Exit jtag_disable ----------------------------------------------------------





////////////////////////////////////////////////////////////////////////////////
// PIN ALLOCATIONS TABLE ARDUINO UNO                                          //
// Board -Atmel- PIN - IDE - Function          - Connection               ALT //
//                                                                            //
// CONNECTIONS RAILS TOP LEFT: DIGITAL PWM<~> ******************************* //
// SCL   -  28 - PC5 -19/A5- ADC5/SCL/PCINT13  - Clock DS1307             TWI //
// SDA   -  27 - PC4 -18/A4- ADC4/SDA/PCINT12  - Clock DS1307             TWI //
// AREF  -  21 - REF -     - AREF              -                              //
// GND   -  22 - GND -     - GND               -                              //
// 13    -  19 - PB5 -  13 - SCK/PCINT5        - Lan ENC28J60 SCK LEDBTIN SPI //
// 12    -  18 - PB4 -  12 - MISO/PCINT4       - Lan ENC28J60 MISO        SPI //
// ~11   -  17 - PB3 -  11 - MOSI/OC2A/PCINT3  - Lan ENC28J60 MOSI        PWM //
// ~10   -  16 - PB2 -  10 - SS/OC1B/PCINT2    - Lan ENC28J60 SS          PWM //
// ~9    -  15 - PB1 -   9 - OC1A/PCINT1       -                          PWM //
// 8     -  14 - PB0 -   8 - PCINT0/CLK0/ICP1  - DS1820 Temperature       DIO //
//                                                                            //
// CONNECTIONS RAILS TOP RIGHT: DIGITAL PWM<~> ****************************** //
// 7     -  13 - PD7 -   7 - PCINT23/AIN1      -                          DIO //
// ~6    -  12 - PD6 -   6 - PCINT22/OCA0/AIN0 - LED blue                 PWM //
// ~5    -  11 - PD5 -   5 - PCINT21/OC0B/T1   - LED green                PWM //
// 4     -   6 - PD4 -   4 - PCINT20/XCK/T0    -                          INT //
// ~3    -   5 - PD3 -   3 - PCINT19/OC2B/INT1 - LED red                  PWM //
// ~2    -   4 - PD2 -   2 - PCINT18/INT0      -                          INT //
// TX->1 -   3 - PD1 -   1 - PCINT17/TXD       - Serial monitor           TXD //
// RX<-0 -   2 - PD0 -   0 - PCINT16/RCD       - Serial Monitor           RCD //
//                                                                            //
// CONNECTIONS RAILS BOTTOM LEFT: POWER ************************************* //
// 5V    -   7 - VCC -     - VCC               -                          VCC //
// RES   -   1 - RES -     - PCINT14/RESET     -                          RES //
// 3.3V  -     -     -     -                   -                              //
// 5V    -     -     -     -                   -                              //
// GND   -     -     -     -                   -                              //
// GND   -     -     -     -                   -                              //
// Vin   -     -     -     -                   -                              //
//                                                                            //
// CONNECTIONS RAILS BOTTOM RIGHT: ANALOG IN ******************************** //
// A0    -  23 - PC0 -A0/14- ADC0/PCINT8       -                          ADC //
// A1    -  24 - PC1 -A1/15- ADC1/PCINT9       -                          ADC //
// A2    -  25 - PC2 -A2/16- ADC2/PCINT10      -                          ADC //
// A3    -  26 - PC3 -A3/17- ADC3/PCINT12      -                          ADC //
// A4    -  27 - PC4 -A4/18- ADC4/SDA/PCINT12  - Clock DS1307             TWI //
// A5    -  28 - PC5 -A5/19- ADC5/SCL/PCINT13  - Clock DS1307             TWI //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////




////////////////////////////////////////////////////////////////////////////////
// EEPROM MEMORY MAP:                                                         //
// Start End  Number Description                                              //
// 0000  0000    1        Never use this memory location to be AVR compatible //
// 0001  0001    1   WERKVERLICHTING hobbyLightProg: 1=off 2=on 3=auto RELAY1 //
// 0002  0002    1     If LDR reaches this kasLightON*10 switch        RELAY1 //
// 0003  0003    1     kasLightSecs*10 werkverlichting on              RELAY1 //
// 0004  0004    1   VERWARMING hobbyHeatProg: 1=off 2=on 3=auto       RELAY2 //
// 0005  0005    1     Celsius hobbyHeatON/10 verwarming switch on     RELAY2 //
// 0006  0006    1     Celsius hobbyHeatOFF/10 verwarming switch off   RELAY2 //
// 0007  0007    1   GROEILED hobbyLedProg: 1=off 2=on 3=auto          RELAY3 //
// 0008  0008    1     Hours kasLedHours around noon to switch on      RELAY3 //
// 0009  0009    1   WATER hobbyWaterProg: 1=off 2=on 3=auto           RELAY4 //
// 0010  0010    1     hobbyWaterSecs*10 to keep watering              RELAY4 //
// 0011  0011    1   AUDIO hobbyAudioProg: 1=off 2=on 3=auto           RELAY5 //
// 0012  0012    1     hobbyAudioMins*10 to keep audio playing         RELAY5 //
////////////////////////////////////////////////////////////////////////////////



////////////////////////////////////////////////////////////////////////////////
// FUSES (can always be altered by using the STK500)                          //
// On-Chip Debug Enabled: off                            (OCDEN=0)            //
// JTAG Interface Enabled: off                           (JTAGEN=0)           //
// Preserve EEPROM mem through the Chip Erase cycle: On  (EESAVE = 0)         //
// Boot Flash section = 2048 words, Boot startaddr=$3800 (BOOTSZ=00)          //
// Boot Reset vector Enabled, default address=$0000      (BOOTSTR=0)          //
// CKOPT fuse (operation dependent of CKSEL fuses        (CKOPT=0)            //
// Brown-out detection level at VCC=2,7V;                (BODLEVEL=0)         //
// Ext. Cr/Res High Freq.; Start-up time: 16K CK + 64 ms (CKSEL=1111 SUT=11)  //
//                                                                            //
// LOCKBITS (are dangerous to change, since they cannot be reset)             //
// Mode 1: No memory lock features enabled                                    //
// Application Protect Mode 1: No lock on SPM and LPM in Application Section  //
// Boot Loader Protect Mode 1: No lock on SPM and LPM in Boot Loader Section  //
////////////////////////////////////////////////////////////////////////////////
//345678911234567892123456789312345678941234567895123456789612345678971234567898