#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Define the analog input pins for ESP32
const int mq7Pin = 35; // GPIO35
const int mq6Pin = 26; // GPIO26
#define pirPin 26 // deklarasi pin dari pir
#define ledPin 3 // deklarasi pin led
int statusPir = LOW; // kondisi awal PIR
int gerakanPir; // untuk menyimpan pembacaan PIR
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
bool dangerDisplayed = false; // Untuk melacak apakah peringatan sudah ditampilkan
void setup() {
// Start the serial communication
Serial.begin(9600);
// Initialize the LCD
lcd.init();
lcd.backlight();
// Print an initial message to the LCD
lcd.setCursor(0, 0);
lcd.print("Monitoring Gas...");
delay(2000); // Display the message for 2 seconds
lcd.clear();
}
void loop() {
// Read the analog values from the sensors
int mq7Value = analogRead(mq7Pin);
int mq6Value = analogRead(mq6Pin);
// Print the sensor values to the Serial Monitor
Serial.print("MQ-7 Value: ");
Serial.print(mq7Value);
Serial.print("\t MQ-6 Value: ");
Serial.println(mq6Value);
// Check for dangerous gas levels
if (mq7Value > 500 || mq6Value > 200) {
if (!dangerDisplayed) { // Jika peringatan belum ditampilkan
lcd.clear(); // Bersihkan LCD
lcd.setCursor(0, 0);
lcd.print("Bahaya!");
lcd.setCursor(0, 1);
lcd.print("Gas Bocor");
dangerDisplayed = true; // Set status peringatan
}
} else {
// Jika level gas aman, reset tampilan
if (dangerDisplayed) {
dangerDisplayed = false; // Reset status peringatan
}
}
// Baca kondisi dari sensor PIR
gerakanPir = digitalRead(pirPin); // Baca nilai dari PIR
if (gerakanPir == HIGH) { // Jika mendeteksi gerakan
Serial.print("Ada Orang...");
// Jika gas bocor juga terdeteksi
if (dangerDisplayed) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("CEPAT MENGHINDAR!");
lcd.setCursor(0, 1);
lcd.print("Gas Bocor!");
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Cepat keluar!");
lcd.setCursor(0, 1);
lcd.print("Ada Orang!");
}
// Menyalakan led ketika terdapat gerakan
if (statusPir == LOW) { // Jika status PIR low, maka ada orang
Serial.println("Ada Orang!!!"); // Tampilkan di serial monitor
statusPir = HIGH; // Ubah status dari low ke high
}
} else { // Jika PIR tidak mendeteksi gerakan
if (statusPir == HIGH) { // Jika status PIR high
Serial.println("Tidak Ada Gerakan!"); // Tampilkan di serial monitor
statusPir = LOW; // Ubah status dari high ke low
}
}
// Add a small delay to prevent excessive updates
delay(1000);
}