#include "DHT.h"
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define LED_merah 2
#define LED_kuning 3
#define LED_biru 4
#define DHTPIN 5
#define buzzer 9
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);
Adafruit_SSD1306 display(128, 64, &Wire, -1);

void setup() {
  pinMode(LED_merah, OUTPUT);
  pinMode(LED_kuning, OUTPUT);
  pinMode(LED_biru, OUTPUT);
  pinMode(buzzer, OUTPUT);
  Serial.begin(9600);
  dht.begin();

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.setTextColor(1);
  display.setTextSize(1);
  display.display();
}

void loop() {
  delay(2000);
  display.clearDisplay();
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (isnan(h) || isnan(t)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }

  display.setCursor(20,0);
  display.print("suhu: ");
  display.print(t);
  display.print(" C");

  if(t>35){
    digitalWrite(LED_merah, HIGH); 
    digitalWrite(LED_kuning, LOW);
    digitalWrite(LED_biru, LOW);
    display.setCursor(0, 20);
    display.print("Suhu Terlalu Panas");
    tone(buzzer, 2000, 500);
  }
  else if(t>29){
     digitalWrite(LED_kuning, HIGH);
     digitalWrite(LED_merah, LOW);
     digitalWrite(LED_biru, LOW);
     display.setCursor(0,20);
     display.print("Suhu Normal");
  }
  else{
     digitalWrite(LED_kuning, LOW);
     digitalWrite(LED_merah, LOW);
     digitalWrite(LED_biru, HIGH);
     display.setCursor(0,20);
     display.print("Suhu Terlalu Dingin");
  }

  display.display();
}