#include <LiquidCrystal.h>
#include "DHT.h" //library sensor yang telah diimportkan
#define DHTPIN 6    //Pin apa yang digunakan
#define DHTTYPE DHT22 
#define buzzer 5
#define pirPin 4
#define servo 3
int statusPir = LOW;
int gerakanPir; 

#include <Servo.h>
Servo myservo;
int sudut;

int pos = 0;
 
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal lcd (12, 11, 10 , 9, 8, 7);


void setup() {
  myservo.attach(3);
  Serial.begin(9600); //baud komunikasi serial
  Serial.println("Pengujian DHT11!"); //penulisan di serial monitor
  dht.begin(); //prosedur memulai pembacaan module sensor
  lcd.begin(16, 2); 
  lcd.clear();
  lcd.setCursor(4,0);
  lcd.print(" SENSOR ");
  lcd.setCursor(4,1);
  lcd.print("  SUHU  ");
  delay(100);
  lcd.print("      ");
  
  pinMode(buzzer, OUTPUT);
  pinMode(pirPin, INPUT); 
}
 
void loop() {
float suhu = dht.readTemperature();
float f = dht.readTemperature(true);
float t = dht.readTemperature();
float h = dht.readHumidity();
delay(1000);

 gerakanPir = digitalRead(pirPin); 
  if(gerakanPir==HIGH){ 
    tone(buzzer, 10);
    delay(500);

    if(statusPir==LOW){ 
      Serial.println("Ada Gerakan!!!"); 
      statusPir=HIGH; 
    }
  }
  else { 
    noTone(buzzer);
    if(statusPir==HIGH){ 
      Serial.println("Tidak Ada Gerakan!"); 
      statusPir=LOW;
    }


if (suhu <=16) 
  {
    lcd.print("  DINGIN  "); 
  }

else if (suhu <=29)
  {
    lcd.print("  Lembab  ");
  for (pos = 0; pos <= 100; 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 = 100; 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
  }
  }

else if (suhu >=30)
  {
    lcd.print("  PANAS  ");
    tone(buzzer, 10);
    delay(5000);
    noTone(buzzer);
  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
  }
  }


//Menampilkan di LCD
  lcd.setCursor(0,0);
  lcd.println("Suhu =");
  lcd.print(t);
  lcd.println(" C");
  lcd.setCursor(0,1);
  lcd.println("Lembab =");
  lcd.print(h);
  lcd.println(" %");

//Menampilkan di serial monitor
  Serial.println("Temperature = ");
  Serial.print(t);
  Serial.println(" C");
  Serial.println("Humadity = ");
  Serial.print(h);
  Serial.println(" %");
}
}