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

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);

#define DHTPIN 2
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);

void setup() {

  Serial.begin(9600);

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { 
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); 
  }
  
  // just intro
  pinMode(8, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(3, OUTPUT);
  dht.begin();
}

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

  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (isnan(h) || isnan(t)) {
    Serial.println(F("Failed to read from DHT Sensor!"));//print tanpa lewat ram
    return;
  }

  if(t < 29){ 
    display.clearDisplay();
    display.setTextColor(WHITE);
    display.setTextSize(1);
    display.setCursor(30,20);  display.println(F("DHT RENDAH !"));
    display.setCursor(30,30);  display.print(t);
    display.display();
    digitalWrite(11, HIGH);
    digitalWrite(10, LOW);
    digitalWrite(8, LOW);
    
  }
  else if(t >= 29 && t < 35){
    display.clearDisplay();
    display.setTextColor(WHITE);
    display.setTextSize(1);
    display.setCursor(30,20);  display.println(F("SUHU SEDANG !"));
    display.setCursor(30,30);  display.print(t);
    display.display();
    digitalWrite(10, HIGH);
    digitalWrite(8, LOW);
    digitalWrite(11, LOW);
  }
  else if(t > 35){
    display.clearDisplay();
    display.setTextColor(WHITE);
    display.setTextSize(1);
    display.setCursor(30,20);  display.println(F("SUHU PANAS !"));
    display.setCursor(30,30);  display.print(t);
    display.display();
    digitalWrite(8, HIGH);
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
    tone(3, 1000);
    delay(100);
    noTone(3);
    delay(100);
  }
  else{
  }
}