//https://wokwi.com/projects/351526903244391000
//Group Tuesday Morning B01

#include "DHT.h"
#include <LiquidCrystal.h>
#include <Servo.h>

#define DHTPIN 2     
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321

DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
Servo myservo;  // create servo object to control a servo
Servo myservo1;
// twelve servo objects can be created on most boards
int pos = 0;    // variable to store the servo position

void setup() {
  Serial.begin(115200);
  Serial.println(F("Arduino Uno DHT22 project"));

  dht.begin();

  lcd.begin(16, 2);
  // you can now interact with the LCD, e.g.:
  lcd.print("Arduino DHT22");
  myservo.attach(3);  // attaches the servo on pin 3 to the servo object
  myservo1.attach(5);  // attaches the servo on pin 3 to the servo object
}

void loop() {
  float temperature = dht.readTemperature();
  float humidity = dht.readHumidity();

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

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

  lcd.clear();
  lcd.print("Humidity:");
  lcd.print(humidity);
  //delay(2000);
  //lcd.clear();
  lcd.setCursor(0, 2);
  lcd.print("Temperature:");
  lcd.print(temperature);
  //tone(13, 262, 250); // Plays 262Hz tone for 0.250 seconds
  if (temperature >= 40) { //temperature==40
    tone(13, 262, 1250); // Plays 262Hz tone for 0.250 seconds
    Serial.print(F("Hot "));

    for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
      myservo.write(pos);              // tell servo to go to position in variable 'pos'
      delay(15);                       // waits 15ms for the servo to reach the position
    }
    for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
      myservo.write(pos);              // tell servo to go to position in variable 'pos'
      delay(15);                       // waits 15ms for the servo to reach the position
    }
  }

  if (temperature <= 10) {
    tone(13, 262, 1250); // Plays 262Hz tone for 0.250 seconds
    Serial.print(F("Cold "));

    for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
      myservo1.write(pos);              // tell servo to go to position in variable 'pos'
      delay(15);                       // waits 15ms for the servo to reach the position
    }
    for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
      myservo1.write(pos);              // tell servo to go to position in variable 'pos'
      delay(15);                       // waits 15ms for the servo to reach the position
    }

  }

  // Wait a few seconds between measurements.
  delay(2000);
}