#include "DHTesp.h"
#include <LiquidCrystal_I2C.h>
const int DHT_PIN = 15;
const int BUZZER = 18;
const int LED_RED = 5;
const int BUTTON_PIN = 13; // Assuming button is connected to pin 13
DHTesp dhtSensor;
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Custom characters for background effect
byte backgroundChar[8] = { 0b11111, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111 }; // Full block
byte emptyChar[8] = { 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000 }; // Empty block
void setup() {
Serial.begin(115200);
pinMode(BUZZER, OUTPUT); // Buzzer
pinMode(LED_RED, OUTPUT); // Red LED
pinMode(BUTTON_PIN, INPUT_PULLUP); // Button as input with internal pull-up
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
lcd.init();
lcd.backlight();
// Create custom characters
lcd.createChar(0, backgroundChar); // Create background character
lcd.createChar(1, emptyChar); // Create empty character
}
void loop() {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
// Display temperature and humidity on Serial Monitor
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.println("---");
// Clear the LCD and set background
lcd.clear();
// Set background effect
for (int i = 0; i < 16; i++) {
lcd.setCursor(i, 0);
lcd.write(byte(0)); // Write the background character
}
// Display temperature on the LCD
lcd.setCursor(0, 1);
lcd.print("Temp: " + String(data.temperature, 2) + " C");
delay(1000);
// Clear the second line and display message based on temperature
lcd.setCursor(0, 1);
bool buttonPressed = digitalRead(BUTTON_PIN) == LOW; // Button pressed when LOW
if (data.temperature < 20) {
lcd.print("AMAN");
digitalWrite(LED_RED, LOW);
noTone(BUZZER);
}
else if (data.temperature > 32 && !buttonPressed) {
lcd.print("UDARA TDK BAIK");
digitalWrite(LED_RED, HIGH);
tone(BUZZER, 165); // Buzzer alert
}
else if (data.temperature > 20 && data.temperature < 32) {
lcd.print("UDARA BAGUS");
digitalWrite(LED_RED, LOW);
noTone(BUZZER);
}
// Override buzzer if the button is pressed
if (buttonPressed) {
noTone(BUZZER);
digitalWrite(LED_RED, LOW); // Optional: turn off LED if button pressed
}
delay(2000); // Adjust as needed
}