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

/*****************************************************************
   Déclaration des Capteurs
 *****************************************************************/
//relay Lumiere
//const int RougeHaut      = 2;
//const int BleuHaut       = 3;

const int FloBas     = 2;
const int FullBas    = 3;
const int CroBas     = 4;

const int FloHaut     = 5;
const int FullHaut    = 6;
const int CroHaut     = 7;

//relay Ventilation
const int DehumPin        = 6;
const int Extracteur      = 7;
const int VentilHaut      = 8;
const int VentilBas       = 9;

//relay Arrosage
const int Arro1            = 10;
const int Arro2            = 11;
const int Evacuation      = 12;
const int Melange         = 13;

//Capteur niveau eau
const int NiveauEauRecup  = 14;
const int NiveauEauAqua   = 15;

//Capteur Temp
#define ONE_WIRE_TEMP 16
OneWire oneWireTemp(ONE_WIRE_TEMP);
DS18B20 Temp(&oneWireTemp);

//capteur Hum air
#define DHTTYPE DHT22
#define DHTPIN 17
DHT dht(DHTPIN, DHTTYPE);

//Capteur hum sol
const int HumidSolHaut = 18;
const int HumidSolBas  = 19;

//interupteur
const int interType  = 22;
const int interLum  = 24;
const int interONExt  = 26;
const int interONVentil  = 28;
const int interOFFExt  = 30;
const int interONAro  = 32;
const int interONVid  = 34;
const int interOFFAro  = 36;
const int inter9  = 38;
const int inter10 = 40;

//RTC
DS3231 Clock;

/*****************************************************************
   Déclaration des Variables globals
 *****************************************************************/

//Variable lumiere
int newDay = 1; // Variable pour savoir si nouveau jour.
int HA;  //heure allumage en minute
int HE;  //heure extinction en minute (Ha +12Hou18H +/-15min)

//Variable Extraction
int tmpExtract = 0, humExtract;
int extract;

//Variabe Arrosage
const int delaySecArro = 20;
const int delaySecEvacuation = 30;
int SolBas, SolHaut;
int HeuArrosage;
/***********************************************************************
     Setup
 **********************************************************************/

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

  /***********************************************************************
    init des pins
  * *********************************************************************/
  //PIN Lumiere
  /*pinMode(RougeHaut, OUTPUT);
    pinMode(BleuHaut, OUTPUT);

    //Fullbas
    //FloBas
    //CroBas
    /*
    pinMode(FullBas, OUTPUT);
    pinMode(FloBas, OUTPUT);
    pinMode(CroBas, OUTPUT);*/

  //digitalWrite(RougeHaut, HIGH);
  //digitalWrite(RougeBas, HIGH);
  //digitalWrite(BleuHaut, HIGH);
  //digitalWrite(BleuBas, HIGH);

  //Pin ventilation
  pinMode(DehumPin, OUTPUT);
  pinMode(Extracteur, OUTPUT);
  pinMode(VentilHaut, OUTPUT);
  pinMode(VentilBas, OUTPUT);
  digitalWrite(DehumPin, HIGH);
  digitalWrite(Extracteur, HIGH);
  digitalWrite(VentilHaut, HIGH);
  digitalWrite(VentilBas, HIGH);

  //Pin Arrosage
  pinMode(Arro1, OUTPUT);
  pinMode(Arro2, OUTPUT);
  pinMode(Evacuation, OUTPUT);
  pinMode(Melange, OUTPUT);
  digitalWrite(Arro1, HIGH);
  digitalWrite(Arro2, HIGH);
  digitalWrite(Evacuation, HIGH);
  digitalWrite(Melange, HIGH);

  pinMode(NiveauEauRecup, INPUT);

  int lum = 20;
  analogWrite(FullBas, lum);
  analogWrite(FloBas, 0);
  analogWrite(CroBas, 0);

  analogWrite(FullHaut, lum);
  analogWrite(FloHaut, 0);
  analogWrite(CroHaut, 0);


}
/*
  const int Arro1            = 10;
  const int Arro2            = 11;
  const int Evacuation      = 12;
  const int Melange         = 13;
*/
int incomingByte = 0; // for incoming serial data
// a marche 1     97
// z arret 1      122
// q marche 2     113
// s arret 2      115

int luminosite = 0;
void loop() {
  //Fullbas
  //FloBas
  //CroBas

  //delay(5000);


  int full = 0;
  int flo = full;
  int cro = 0;

  /*analogWrite(FullBas, full);
    analogWrite(FloBas, flo);
    analogWrite(CroBas, cro);

    analogWrite(FullHaut, full);
    analogWrite(FloHaut, flo);
    analogWrite(CroHaut, cro);*/

  /*digitalWrite(Melange, LOW);
    delay(500);
    digitalWrite(Melange, HIGH);
    delay(500);//*/


  if (Serial.available() > 0) {
    incomingByte = Serial.read();


    switch (incomingByte) {
      case ('a'):
        Serial.println("MArche1");
        digitalWrite(Arro1, LOW);
        break;
      case ('z'):
        Serial.println("Arret1");
        digitalWrite(Arro1, HIGH);
        break;
      case ('q'):
        Serial.println("MArche2");
        digitalWrite(Arro2, LOW);
        break;
      case ('s'):
        Serial.println("Arret2");
        digitalWrite(Arro2, HIGH);
        break;
      case ('w'):
        Serial.println("vide");
        digitalWrite(Evacuation, LOW);
        break;
      case ('x'):
        Serial.println("arretvide");
        digitalWrite(Evacuation, HIGH);
        break;
      default:
        break;
    }
  }//*/

  //Serial.println(digitalRead(NiveauEauRecup));
}
