#include <LiquidCrystal.h>
#include <Servo.h>
// Initialize LCD and Servo
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
Servo exhaustFan;
// Pin Definitions
const int buzzerPin = 15;
const int servoPin = 18;
int gasLevel;
// Gas thresholds for different gas types (in ppm)
int coThreshold = 50;
int methaneThreshold = 5000;
int lpgThreshold = 10000;
void setup() {
pinMode(buzzerPin, OUTPUT);
exhaustFan.attach(servoPin);
// Initialize LCD
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("Gas Detection Init");
delay(500); // Reduced init delay
// Manually set gas type and gas level (simulating user input)
String gasType = "Methane";
gasLevel = 800;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Gas Type: ");
lcd.print(gasType);
lcd.setCursor(0, 1);
lcd.print("Gas Level: ");
lcd.print(gasLevel);
// Check if the gas level exceeds the threshold for the selected gas type
if (gasType == "CO" && gasLevel > coThreshold) {
handleGasAlarm("CO");
} else if (gasType == "Methane" && gasLevel > methaneThreshold) {
handleGasAlarm("Methane");
} else if (gasType == "LPG" && gasLevel > lpgThreshold) {
handleGasAlarm("LPG");
} else {
lcd.setCursor(0, 1);
lcd.print("No Harmful Gas");
}
}
void loop() {
// Nothing to do in loop, one-time check in setup
}
// Function to handle gas alarm based on gas type
void handleGasAlarm(String gasType) {
lcd.setCursor(0, 1);
lcd.print("Harmful " + gasType + "!");
// Activate buzzer
tone(buzzerPin, 1000);
// Rotate the exhaust fan (servo) quickly
for (int angle = 0; angle <= 180; angle += 10) {
exhaustFan.write(angle);
delay(50);
}
for (int angle = 180; angle >= 0; angle -= 10) {
exhaustFan.write(angle);
delay(50);
}
}