#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
#include <LiquidCrystal_I2C.h> // Library untuk LCD I2C
#define DHTPIN 17 // Pin yang terhubung dengan DHT22
#define DHTTYPE DHT22 // Tipe DHT (DHT22 atau DHT11)
#define LDRPIN 25 // Pin yang terhubung dengan LDR
#define BUZZERPIN 15 // Pin yang terhubung dengan buzzer
#define RED_PIN 33 // Pin yang terhubung dengan LED merah
#define GREEN_PIN 32 // Pin yang terhubung dengan LED hijau
#define BLUE_PIN 35 // Pin yang terhubung dengan LED biru
// Inisialisasi LCD I2C
LiquidCrystal_I2C lcd(0x27, 16, 2); // Alamat I2C dan ukuran LCD (16x2)
DHT dht(DHTPIN, DHTTYPE);
const int trigPin = 26;
const int echoPin = 27;
const int buzzerPin = 15;
//define sound speed in cm/uS
#define SOUND_SPEED 0.034
#define CM_TO_INCH 0.393701
long duration;
float distanceCm;
float temperature;
int ldrValue;
enum DisplayStatus {
TEMP,
LDR,
DISTANCE
};
DisplayStatus currentStatus = TEMP; // Mulai dengan menampilkan temperatur
bool buzzerState = false; // Status bunyi buzzer
// Inisialisasi pin LED RGB
const int RED = RED_PIN;
const int GREEN = GREEN_PIN;
const int BLUE = BLUE_PIN;
void setup() {
Serial.begin(115200); // Starts the serial communication
Wire.begin();
// Inisialisasi LCD I2C
lcd.init();
lcd.clear();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("404 TEAM ");
lcd.setCursor(0,1);
delay(5000);
lcd.clear();
dht.begin();
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(buzzerPin, OUTPUT); // Sets the buzzerPin as an Output
// Inisialisasi pin LED RGB sebagai output
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
}
void loop() {
// Switch between different display statuses every 5 seconds
switch (currentStatus) {
case TEMP:
temperature = dht.readTemperature();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print(" C");
// Kontrol LED RGB berdasarkan suhu
if (temperature < 40) {
// Atur LED menjadi merah jika suhu di bawah 40 derajat
analogWrite(RED, 255); // Merah penuh
analogWrite(GREEN, 0); // Hijau mati
analogWrite(BLUE, 0); // Biru mati
} else if (temperature >= 40 && temperature <= 50) {
// Atur LED menjadi kuning jika suhu dalam rentang 40-50 derajat
analogWrite(RED, 255); // Merah penuh
analogWrite(GREEN, 255); // Hijau penuh
analogWrite(BLUE, 0); // Biru mati
} else {
// Atur LED menjadi biru jika suhu di atas 50 derajat
analogWrite(RED, 0); // Merah mati
analogWrite(GREEN, 0); // Hijau mati
analogWrite(BLUE, 255); // Biru penuh
}
break;
case LDR:
ldrValue = analogRead(LDRPIN);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("LDR: ");
lcd.print(ldrValue);
break;
case DISTANCE:
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculate the distance
distanceCm = duration * SOUND_SPEED/2;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Dist: ");
lcd.print(distanceCm);
lcd.print(" cm");
// Check if distance is more than 300 cm or less than 50 cm and activate buzzer
if (distanceCm > 300 || distanceCm < 50) {
buzzerState = true; // Set status bunyi buzzer menjadi true
} else {
buzzerState = false; // Set status bunyi buzzer menjadi false
}
// Buzzer berbunyi jika berada di atas 300 cm atau di bawah 50 cm
if (buzzerState) {
digitalWrite(buzzerPin, HIGH); // Bunyikan buzzer
} else {
digitalWrite(buzzerPin, LOW); // Matikan buzzer
}
break;
}
// Print distance in Serial Monitor
Serial.print("Distance (cm): ");
Serial.println(distanceCm);
// Move to the next display status
currentStatus = static_cast<DisplayStatus>((currentStatus + 1) % 3);
delay(2000); // Switch display status every 2 seconds
}