#include <LiquidCrystal.h>
#include <SoftwareSerial.h>

// Define LCD pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

// Define SIM900A module pins
SoftwareSerial sim900(2, 3);

// Define MQ-2 sensor pin
const int gasSensor = A0;
const int fan = 4;       // Fan connected to pin 4
const int redLED = 5;    // Red LED connected to pin 5

void setup() {
    // Initialize pins
    pinMode(fan, OUTPUT);
    pinMode(redLED, OUTPUT);
    pinMode(gasSensor, INPUT);
    
    // Initialize LCD
    lcd.begin(16, 2);
    lcd.print("Gas Scan is ON");
    delay(2000);
    lcd.clear();
    
    // Initialize SIM900A
    sim900.begin(9600);

    // Initialize Serial Monitor for debugging
    Serial.begin(9600);
    Serial.println("Setup complete");
}

void loop() {
    int gasLevel = analogRead(gasSensor); // Read gas sensor value
    Serial.print("Gas Level: ");
    Serial.println(gasLevel); // Debugging output
    
    // Display gas level on LCD
    lcd.setCursor(0, 0);
    lcd.print("Gas Level: ");
    lcd.setCursor(11, 0);
    lcd.print(gasLevel);
    
    if (gasLevel > 400) { // Gas detected threshold
        digitalWrite(fan, HIGH);  // Turn on exhaust fan
        digitalWrite(redLED, HIGH); // Turn on RED LED
        
        lcd.setCursor(0, 1);
        lcd.print("Gas Detected!");
        Serial.println("Gas detected! Sending SMS...");
        
        sendSMS(); // Send SMS alert
    } else {
        digitalWrite(fan, LOW);  // Turn off exhaust fan
        digitalWrite(redLED, LOW); // Turn off RED LED
        
        lcd.setCursor(0, 1);
        lcd.print("Safe         ");
        Serial.println("Safe environment");
    }
    
    delay(500);
}

void sendSMS() {
    sim900.println("AT"); // Check module communication
    delay(1000);
    
    sim900.println("AT+CMGF=1"); // Set SMS mode to text
    delay(1000);
    
    sim900.println("AT+CMGS=\"+94716496226\""); // Replace with your phone number
    delay(1000);
    
    sim900.println("Gas Leak Detected! Exhaust Fan Activated."); // Message content
    sim900.write(26); // End SMS
    delay(5000);
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
gas1:AOUT
gas1:DOUT
gas1:GND
gas1:VCC
led1:A
led1:C
lcd1:VSS
lcd1:VDD
lcd1:V0
lcd1:RS
lcd1:RW
lcd1:E
lcd1:D0
lcd1:D1
lcd1:D2
lcd1:D3
lcd1:D4
lcd1:D5
lcd1:D6
lcd1:D7
lcd1:A
lcd1:K
r1:1
r1:2