#include "DHT.h"
#include "Servo.h"
#include "SevSeg.h"

// DHT 22  (AM2302), AM2321
#define DHTPIN 2  //DHT22 on the floor 
#define DHTPIN_T1 3 //DHT22 on the first tube  
#define DHTPIN_T2 4 //DHT22 on the second tube
#define DispSeg1 12 //DHT22 on the first tube  
#define DispSeg2 13 //DHT22 on the first tube      
#define DHTTYPE DHT22  

//Simple serve
#define ServoPinT1 A1 // on the first tube
#define ServoPinT2 A0 // on the second tube
#define HotTempPin A2 


DHT dht(DHTPIN, DHTTYPE);
DHT dhtTube1(DHTPIN_T1, DHTTYPE);
DHT dhtTube2(DHTPIN_T2, DHTTYPE);

Servo servoTube1;
Servo servoTube2;   
SevSeg sevseg; 

byte pins[] = {
  5, // A
  6, // B
  7, // C
  8, // D
  9, // E
  10, // F
  11, // G
};


void setup() {
  byte numDigits = 2;
  byte digitPins[] = {DispSeg1, DispSeg2};
  bool resistorsOnSegments = false; // 'false' means resistors are on digit pins
  byte hardwareConfig = COMMON_ANODE; // See README.md for options
  bool updateWithDelays = false; // Default 'false' is Recommended
  bool leadingZeros = true; // Use 'true' if you'd like to keep the leading zeros
  bool disableDecPoint = true; // Use 'true' if your decimal point doesn't exist or isn't connected

  Serial.begin(115200);
  dht.begin();
  dhtTube1.begin();
  dhtTube2.begin();
  servoTube1.attach(ServoPinT1);
  servoTube2.attach(ServoPinT2);
  pinMode(HotTempPin, INPUT);
  sevseg.begin(hardwareConfig, numDigits, digitPins, pins, resistorsOnSegments, updateWithDelays, leadingZeros, disableDecPoint);
  sevseg.setBrightness(90);
}

bool In(float val, float source, float precision){
    float start = source - (source * precision);
    float end = source + (source * precision);
    
    if(val >= start && val <= end) return true;
    else return false;
}

void loop() {

  int humiditySet = (analogRead(HotTempPin) / 1023.0) * 99;

  sevseg.setNumber(humiditySet,1);
  sevseg.refreshDisplay();

  float temperature = dht.readTemperature();
  float humidity = dht.readHumidity();

  float temperatureTube1= dhtTube1.readTemperature();
  float temperatureTube2= dhtTube2.readTemperature();

  // Check if any reads failed and exit early (to try again).
  if (isnan(temperature) || isnan(humidity) || isnan(temperatureTube1) || isnan(temperatureTube2)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }

  if(humidity >= humiditySet)
  {
    bool firstEdge = In(temperature, temperatureTube1, .2f);
    bool secondEdge = In(temperature, temperatureTube2, .2f);

    if(firstEdge) servoTube1.write(180);
    if(secondEdge) servoTube2.write(180);

    if(!firstEdge && !secondEdge)
    {
      servoTube1.write(180);
      servoTube2.write(180);
    }
  }
  else
  {
    servoTube1.write(90);
    servoTube2.write(90);
  }


  Serial.print(F("Humidity: "));
  Serial.print(humidity);
  Serial.print(F("%  Temperature: "));
  Serial.print(temperature);
  Serial.println(F("°C "));

  Serial.print(F("%  Temperature1: "));
  Serial.print(temperatureTube1);
  Serial.println(F("°C "));

  Serial.print(F("%  Temperature2: "));
  Serial.print(temperatureTube2);
  Serial.println(F("°C "));

}