//Muhammad Irfan Adi Saputra
//TK21A
//NIM: 21316010
//Soal TIPE A
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Wire.h>
#include "DHT.h"
#include <NewPing.h>
#define ledldr 26
#define ledsuhu 27
#define buzzer 14
#define DHTPIN 2
#define DHTTYPE DHT22
#define LDR_PIN 19
#define TRIG_PIN 5
#define ECHO_PIN 18
#define jarak_max 200
NewPing jarak(TRIG_PIN, ECHO_PIN, jarak_max);
DHT suhu(DHTPIN, DHTTYPE);
// choose the pin for the LED
int inputPin = 4; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0;
#define OLED_ADDR 0x3C // OLED display address (check with your display)
#define OLED_SDA 21 // OLED display SDA pin
#define OLED_SCL 22 // OLED display SCL pin
#define Width_l 128
#define Height_l 64
#define Reset 4
Adafruit_SSD1306 layar(Width_l, Height_l, &Wire, OLED_ADDR);
void readSuhu(){
float temp = suhu.readTemperature();
float kbb = suhu.readHumidity();
layar.setTextSize(0.2);
layar.setCursor(0, 0);
if (temp>=30){
digitalWrite(ledsuhu, HIGH);
layar.print("Temp: ");
layar.print(temp);
layar.print(" C");
//Kelembaban
//ayar.setTextSize(0.08);
//layar.setCursor(1, 10);
//layar.print("Hum: ");
//layar.print(kbb);
//layar.print(" %");
}else{
digitalWrite(ledsuhu, LOW);
layar.print("SuhuNormal");
}
}
void readLDR(){
layar.setTextSize(0.2);
layar.setCursor(1, 25);
layar.print("Ruangan: ");
if (digitalRead(LDR_PIN) == HIGH){
digitalWrite(ledldr, HIGH);
layar.print("Malam");
}else {
digitalWrite(ledldr, LOW);
layar.print("Siang ");
}
}
void readPIR(){
val = digitalRead(inputPin);
layar.setTextSize(0.2);
layar.setCursor(1, 40);
layar.print("Kondisi: ");
if (val == HIGH && pirState == LOW) { // check if the input is HIGH
layar.print("Terdeteksi");
pirState = HIGH;
delay(100);
} else if(val == HIGH && pirState == HIGH) {
layar.print("Normal");
pirState = LOW;
delay(100);
}
}
void readDistance(){
unsigned int jaraks = jarak.ping_cm();
layar.setTextSize(0.2);
layar.setCursor(1, 15);
layar.print("Jarak: ");
if(jaraks<=100){
tone(buzzer, 1000);
layar.print(jaraks);
layar.print("CM");
layar.print(", bahaya");
}else{
layar.print("Aman");
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(LDR_PIN, INPUT);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(ledldr, OUTPUT);
pinMode(ledsuhu, OUTPUT);
pinMode(buzzer, OUTPUT);
suhu.begin();
layar.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
layar.clearDisplay();
layar.setTextColor(SSD1306_WHITE);
}
void loop() {
readSuhu();
readDistance();
readLDR();
readPIR();
layar.display();
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
layar.clearDisplay();
}