#include <LiquidCrystal.h>
// Define sensor pins
#define MQ2_PIN PA0
#define LDR_PIN PA1
#define BUZZER_PIN PB0
#define GAS_LED PB1
#define FIRE_LED PC13
#define GAS_THRESHOLD 900
#define LDR_THRESHOLD 512
// LCD pins: RS, EN, D4, D5, D6, D7 (you can connect accordingly)
LiquidCrystal lcd(PA2, PA3, PA4, PA5, PA6, PA7); // Adjust pins as per your Wokwi wiring
void setup() {
pinMode(GAS_LED, OUTPUT);
pinMode(FIRE_LED, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
Serial.begin(9600);
lcd.begin(16, 2);
lcd.print("System Initializing");
delay(2000);
lcd.clear();
}
void loop() {
int gasValue = analogRead(MQ2_PIN);
int ldrValue = analogRead(LDR_PIN);
Serial.print("Gas Sensor Value: ");
Serial.print(gasValue);
Serial.print(" | LDR Value: ");
Serial.println(ldrValue);
bool gasDetected = gasValue > GAS_THRESHOLD;
bool fireDetected = ldrValue < LDR_THRESHOLD;
digitalWrite(GAS_LED, gasDetected ? HIGH : LOW);
digitalWrite(FIRE_LED, fireDetected ? HIGH : LOW);
lcd.clear();
if (gasDetected && fireDetected) {
lcd.print("Gas & Fire Alert!");
tone(BUZZER_PIN, 1000);
} else if (gasDetected) {
lcd.print("Gas Detected!");
tone(BUZZER_PIN, 1000);
} else if (fireDetected) {
lcd.print("Fire Detected!");
tone(BUZZER_PIN, 1000);
} else {
lcd.print("All Safe");
noTone(BUZZER_PIN);
}
delay(500);
}