#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int mq7Pin = 35; // GPIO35 for MQ-7
const int mq6Pin = 26; // GPIO26 for MQ-6
const int pirPin = 27; // GPIO27 for PIR sensor
const int buzzer = 25;
const int led = 13;
const int TRIG = 2;
const int ECHO = 4;
const float SOUND_SPEED = 0.034; // Kecepatan suara dalam cm/us
const float CM_TO_INCH = 0.393701; // Konversi cm ke inch
long duration;
float distanceCm, distanceInch;
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
pinMode(buzzer, OUTPUT);
pinMode(led, OUTPUT);
pinMode(TRIG, OUTPUT); // Harus diatur sebagai OUTPUT
pinMode(ECHO, INPUT); // Harus diatur sebagai INPUT
// Start the serial communication
Serial.begin(115200);
// Initialize the LCD
lcd.init();
lcd.backlight();
// Initialize the PIR sensor pin
pinMode(pirPin, INPUT);
// Print a message to the LCD
lcd.setCursor(0, 0);
lcd.print("Aplikasi");
lcd.setCursor(0, 1);
lcd.print("Pendeteksi");
delay(1500);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Kebocoran");
lcd.setCursor(0, 1);
lcd.print("Gas");
delay(1500);
}
void loop() {
// Read the analog values from the sensors
int mq7Value = analogRead(mq7Pin);
int mq6Value = analogRead(mq6Pin);
// Clears the TRIG pin
digitalWrite(TRIG, LOW);
delayMicroseconds(2);
// Sets the TRIG on HIGH state for 10 microseconds
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
// Reads the ECHO pin, returns the sound wave travel time in microseconds
duration = pulseIn(ECHO, HIGH);
// Calculate the distance
distanceCm = duration * SOUND_SPEED / 2;
distanceInch = distanceCm * CM_TO_INCH;
// Print the sensor values to the Serial Monitor
Serial.print("MQ-7 Value: ");
Serial.print(mq7Value);
Serial.print(" MQ-6 Value: ");
Serial.println(mq6Value);
// Prints the distance in the Serial Monitor
Serial.print("Distance (cm): ");
Serial.println(distanceCm);
// Check for dangerous gas levels (you can adjust these thresholds)
bool gasDanger = (mq7Value > 300 || mq6Value > 200); // Example thresholds
// Read PIR sensor value
int gerakanPir = digitalRead(pirPin);
// Clear the LCD for new display
lcd.clear();
// Tampilkan nilai gas dan status pada Serial Monitor
Serial.print("MQ-7: ");
Serial.print(mq7Value);
Serial.print(", MQ-6: ");
Serial.println(mq6Value);
Serial.print("Jarak orang: ");
Serial.println(distanceCm);
// Cek bahaya gas dan tampilkan pesan yang sesuai
if (gasDanger) { // Jika bahaya gas terdeteksi
if (gerakanPir == HIGH || distanceCm <= 200) { // Ada gerakan atau jarak <= 200 cm
Serial.println("Bahaya! Gas Bocor, ada orang!!");
digitalWrite(led, HIGH);
tone(buzzer, 700); // Bunyi buzzer ketika bahaya gas dan ada gerakan
lcd.setCursor(0, 0);
lcd.print("Bahaya!");
lcd.setCursor(0, 1);
lcd.print("Gas Bocor!!!");
delay(4000); // Tunda 4 detik
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Selamatkan");
lcd.setCursor(0, 1);
lcd.print("Diri Anda");
delay(4000);
} else { // Bahaya gas, tapi tidak ada gerakan atau jarak lebih dari 200 cm
Serial.println("Bahaya Gas, tapi tidak ada gerakan");
digitalWrite(led, HIGH);
tone(buzzer, 700); // Bunyi buzzer karena bahaya gas
lcd.setCursor(0, 0);
lcd.print("Bahaya!");
lcd.setCursor(0, 1);
lcd.print("Gas Bocor!!!");
delay(4000);
lcd.clear();
}
} else { // Jika tidak ada bahaya gas
Serial.println("Gas Aman");
digitalWrite(led, LOW);
noTone(buzzer); // Matikan bunyi alarm
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Status: Aman");
lcd.setCursor(0, 1);
lcd.print("Gas Aman");
delay(4000);
}
}