
#include <OneWire.h>
#include <DS18B20.h>  //DS18B20_RT by rob tillaart
#include <DS3231.h>   //DS3231 by andrew wickert
#include <Wire.h>

//pin relay
const int Rouge1Pin       = 3;
const int Rouge2Pin       = 4;
const int Bleu1Pin        = 5;
const int Bleu2Pin        = 6;
const int Bleu3Pin        = 7;
const int DehumPin        = 8;
const int VentilPin       = 9;
const int ExtracteurPin   = 10;
const int ArroFlo         = 11;
const int ArroCroi        = 12;
const int Evacuation      = 13;

//pin inter
const int inter1  = 22; //inter extinction lumiere
const int inter2  = 24; //inter marche forcé ventilation
const int inter3  = 26;
const int inter4  = 28;
const int inter5  = 30;
const int inter6  = 32;
const int inter7  = 34;
const int inter8  = 36;
const int inter9  = 38;
const int inter10 = 40;

//pin Capteur
#define ONE_WIRE_TEMP 14
// humide air 15
const int HumidSolCro = 46;
const int HumidSolFlo = 48;


//Variable lumiere
int newDay = 1; // Variable pour savoir si nouveau jour.
int HAFlo;  //heure allumage floraison en minute (9H45 ou 10H15)
int HEFlo;  //heure extinction floraison en minute (HaFlo +12H +/-15min)
int HACro;  //heure allumage croissance en minute (4H45 ou 5H15)
int HECro;  //heure extinction croissance en minute (HaFlo +18H +/-15min)

OneWire oneWireTemp(ONE_WIRE_TEMP);
DS18B20 Temp(&oneWireTemp);
DS3231 Clock;

void setup() {
  Serial.begin(9600);


  /***********************************************************************
                         init des pins
  * *********************************************************************
  */

  pinMode(Rouge1Pin, OUTPUT);
  pinMode(Rouge2Pin, OUTPUT);
  pinMode(Bleu1Pin, OUTPUT);
  pinMode(Bleu2Pin, OUTPUT);
  pinMode(Bleu3Pin, OUTPUT);
  pinMode(DehumPin, OUTPUT);
  pinMode(VentilPin, OUTPUT);
  pinMode(ExtracteurPin, OUTPUT);
  pinMode(ArroFlo, OUTPUT);
  pinMode(ArroCroi, OUTPUT);
  pinMode(Evacuation, OUTPUT);

  pinMode(inter1, INPUT);   //extinction lumiere
  pinMode(inter2, INPUT);   //inter marche forcé ventilation
  pinMode(inter3, INPUT);   //60%/70%
  pinMode(inter4, INPUT);   //croissance mere
  pinMode(inter5, INPUT);
  pinMode(inter6, INPUT);   //Arrosage1 on
  pinMode(inter7, INPUT);   //Arrosage2 on
  pinMode(inter8, INPUT);   //pompe vidange on
  pinMode(inter9, INPUT);   //Arrosage flo auto
  pinMode(inter10, INPUT);  //Arrosage Cro auto

  pinMode(HumidSolCro, INPUT);
  pinMode(HumidSolFlo, INPUT);

  /***********************************************************************
                         init des capteurs
  * *********************************************************************
  */
  Wire.begin();
  Temp.begin();
  Temp.setResolution(12);
}


void loop() {

  /***********************************************************************
                         Gestion des capteurs
  * *********************************************************************
  */
  //temperature
  Temp.requestTemperatures();
  while (!Temp.isConversionComplete());
  float Temper = Temp.getTempC();

  //heure
  bool h24, am;
  int heure = Clock.getHour(h24, am );
  int minu = Clock.getMinute();
  int HMin = heure * 60 + minu; // heure en minute

  //Hydrometrie

  /***********************************************************************
                          Gestion de la lumiere
   * *********************************************************************
  */


  if (newDay == 1) {
    newDay = 0;
    HAFlo = random (585, 615);
    HEFlo = HAFlo + 705 + random(30);
    HACro = random (285, 315);
    HECro = HACro + 1065 + random(30);
  }

  if (HMin == 0)
    newDay = 1;

  if (HMin >= HAFlo & HMin < HEFlo) {
    digitalWrite(Rouge1Pin, LOW);
    digitalWrite(Rouge2Pin, LOW);
  }
  else {
    digitalWrite(Rouge1Pin, HIGH);
    digitalWrite(Rouge2Pin, HIGH);
  }

  if (HMin >= HACro & HMin < HECro) {
    digitalWrite(Bleu1Pin, LOW);
    digitalWrite(Bleu2Pin, LOW);
    digitalWrite(Bleu3Pin, LOW);
  }
  else {
    digitalWrite(Bleu1Pin, HIGH);
    digitalWrite(Bleu2Pin, HIGH);
    digitalWrite(Bleu3Pin, HIGH);
  }


  /***********************************************************************
                         Gestion de l'extraction
  * *********************************************************************
  */
  if (minu == 0 | minu == 1 | Temper >= 27)
    digitalWrite(ExtracteurPin, LOW);
  else
    digitalWrite(ExtracteurPin, HIGH);


  delay(10000);
}
