#include <DHT.h>

#define pinDHT 8
#define DHTTYPE DHT22

#define pinBlue 13
#define pinGreen 12
#define pinOrange 11
#define pinRed 10
#define pinPot A0

#define pinBuzz 6
#define pinAlarm 8

int count;
int countLED_ON;
int countLED_OFF;

DHT dht(pinDHT,DHTTYPE);

void setup() {
  // put your setup code here, to run once:
  count=0;
  countLED_ON=0;
  countLED_OFF=0;

  Serial.begin(9600);
  
  dht.begin();

  //IMPOSTO PIN OUTPUT
  pinMode(pinBlue, OUTPUT);
  pinMode(pinGreen, OUTPUT);
  pinMode(pinOrange, OUTPUT);
  pinMode(pinRed, OUTPUT);
  
  pinMode(pinBuzz, OUTPUT);
  pinMode(pinAlarm, OUTPUT);
  
  //IMPOSTO PIN INPUT
  pinMode(pinPot, INPUT);

  //COREOGRAFIA INIZIALE!!
  
  //ACCENDO IN SUCCESSIONE
  digitalWrite(pinBlue, HIGH);
  delay(50);
  digitalWrite(pinGreen, HIGH);
  delay(50);
  digitalWrite(pinOrange, HIGH);
  delay(50);
  digitalWrite(pinRed, HIGH);
  delay(100);

  //tone(pinBuzz, 500, 500);

  //SPENGO TUTTI
  digitalWrite(pinBlue, LOW);
  digitalWrite(pinGreen, LOW);
  digitalWrite(pinOrange, LOW);
  digitalWrite(pinRed, LOW);
delay(150);

  //ACCENDO TUTTI
  digitalWrite(pinBlue, HIGH);
  digitalWrite(pinGreen, HIGH);
  digitalWrite(pinOrange, HIGH);
  digitalWrite(pinRed, HIGH);
delay(150);

  //SPENGO TUTTI
  digitalWrite(pinBlue, LOW);
digitalWrite(pinGreen, LOW);
  digitalWrite(pinOrange, LOW);
  digitalWrite(pinRed, LOW);
delay(150);

  //ACCENDO TUTTI
  digitalWrite(pinBlue, HIGH);
  digitalWrite(pinGreen, HIGH);
  digitalWrite(pinOrange, HIGH);
  digitalWrite(pinRed, HIGH);
delay(150);

  //SPENGO TUTTI
  digitalWrite(pinBlue, LOW);
  digitalWrite(pinGreen, LOW);
  digitalWrite(pinOrange, LOW);
  digitalWrite(pinRed, LOW);


  digitalWrite(pinAlarm, LOW);

  delay(300);


}

void loop() {
  // put your main code here, to run repeatedly:

//LEGGO IL POTENZIOMETRO
float valPot=analogRead(pinPot);

if (valPot==0){ //SE E' A ZERO
  digitalWrite(pinBlue, HIGH);
  digitalWrite(pinGreen, LOW);
  digitalWrite(pinOrange, LOW);
  digitalWrite(pinRed, LOW);
}
else if (valPot<=(1023/3)){//SE E' FINO A 1/3
          digitalWrite(pinBlue, HIGH);
          digitalWrite(pinGreen, HIGH);
          digitalWrite(pinOrange, LOW);
          digitalWrite(pinRed, LOW);
}
else if (valPot<=(2*1023/3)){//SE E' FINO A 2/3
          digitalWrite(pinBlue, HIGH);
          digitalWrite(pinGreen, HIGH);
          digitalWrite(pinOrange, HIGH);
          digitalWrite(pinRed, LOW);
}
else{//SE E' OLTRE A 2/3
          digitalWrite(pinBlue, HIGH);
          digitalWrite(pinGreen, HIGH);
          digitalWrite(pinOrange, HIGH);
          digitalWrite(pinRed, HIGH);
}


//delay(1);
count++;
Serial.print("count=");
Serial.println(count);
float TempSet=(valPot*80)/1023;
Serial.print("Temp set:");
Serial.println(TempSet);
float TempReal=dht.readTemperature();
Serial.print("Temp REAL:");
Serial.println(TempReal);

    digitalWrite(pinAlarm, HIGH);

if (TempReal>=TempSet){
    countLED_ON++;
    Serial.print("count_ON=");
    Serial.println(countLED_ON);
    //tone(pinBuzz, 1000, 100);
    digitalWrite(pinAlarm, HIGH);
}
else{ countLED_OFF++;
Serial.print("count_OFF=");
    Serial.println(countLED_OFF);
      //digitalWrite(pinAlarm, LOW);
}

}