#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Ganti ke 0x3F kalau perlu
const int mqDigitalPin = 2;
const int buzzerPin = 7;
const int redPin = 3;
const int greenPin = 5;
const int bluePin = 6;
bool isCommonCathode = true;
void setup() {
pinMode(mqDigitalPin, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
lcd.init(); // Penting: gunakan init(), bukan begin()
lcd.backlight(); // Aktifkan lampu LCD
lcd.setCursor(0, 0);
lcd.print("Status Gas:");
}
void loop() {
int statusGas = digitalRead(mqDigitalPin);
if (statusGas == LOW) {
Serial.println("🚨 Gas terdeteksi!");
digitalWrite(buzzerPin, HIGH);
setColor(255, 0, 0);
lcd.setCursor(0, 1);
lcd.print("Deteksi Gas! ");
} else {
Serial.println("✅ Gas aman");
digitalWrite(buzzerPin, LOW);
setColor(0, 255, 0);
lcd.setCursor(0, 1);
lcd.print("Gas Aman ");
}
delay(500);
}
void setColor(int r, int g, int b) {
if (!isCommonCathode) {
r = 255 - r;
g = 255 - g;
b = 255 - b;
}
analogWrite(redPin, r);
analogWrite(greenPin, g);
analogWrite(bluePin, b);
}