CCCPaste

Gas Safety rev_01

/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: Gas Safety
	- Source Code NOT compiled for: Arduino Uno
	- Source Code created on: 2024-10-11 13:21:28

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* This code will This code will to make lpg gas */
	/* detection using mq2 sensor and servo motor to */
	/* controlled the gas regulator and also include */
	/* flame sensor and water pumb using Extinguish the */
	/* fire and it controlled by relaymoduel and exhuste */
	/* fan and inclu */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include 	//https://github.com/miguel5612/MQSensorsLib
#include             // Include the Servo library

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t my_MQ2_DOUT_PIN_D2 = 2;      // Digital pin for MQ2 sensor
const uint8_t flameSensorPin = 3;          // Digital pin for flame sensor
const uint8_t relayPin = 4;                 // Digital pin for relay module (water pump)
const uint8_t exhaustFanPin = 5;            // Digital pin for exhaust fan
const uint8_t servoPin = 6;                 // Digital pin for servo motor

/***** DEFINITION OF ANALOG INPUT PINS *****/
const uint8_t my_MQ2_AOUT_PIN_A0 = A0;      // Analog pin for MQ2 sensor

/****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
#define Board                   ("Arduino UNO")
#define Voltage_Resolution      (5)
#define ADC_Bit_Resolution      (10) 
#define RatioMQ2CleanAir       (9.83) // RS / R0 = 9.83 ppm

// Create an instance of the MQUnifiedsensor class for the MQ2 sensor
MQUnifiedsensor MQ2(Board, Voltage_Resolution, ADC_Bit_Resolution, my_MQ2_AOUT_PIN_A0, "MQ-2");
Servo gasRegulator; // Create a Servo object for the gas regulator

void setup(void)
{
    // Initialize serial communication for debugging
    Serial.begin(9600); 

    // Set up the digital and analog pins
    pinMode(my_MQ2_DOUT_PIN_D2, INPUT_PULLUP);
    pinMode(flameSensorPin, INPUT); // Set flame sensor pin as input
    pinMode(relayPin, OUTPUT);       // Set relay pin as output for water pump
    pinMode(exhaustFanPin, OUTPUT);  // Set exhaust fan pin as output

    // Attach the servo motor to its pin
    gasRegulator.attach(servoPin);

    // Initialize the MQ2 sensor
    MQ2.setRegressionMethod(1); //_PPM =  a*ratio^b
    MQ2.setA(574.25); 
    MQ2.setB(-2.222); 
    MQ2.init(); 

    // Calibration process
    Serial.print("Calibrating please wait.");
    float calcR0 = 0;
    for(int i = 0; i < 10; i++) // Start from 0 for clarity
    {
        MQ2.update(); // Update data, the Arduino will read the voltage from the analog pin
        calcR0 += MQ2.calibrate(RatioMQ2CleanAir);
        Serial.print(".");
    }
    MQ2.setR0(calcR0 / 10);
    Serial.println(" done!");

    MQ2.serialDebug(true); // Enable serial debugging for the sensor
}

void loop(void)
{
    MQ2.update(); // Update data, the Arduino will read the voltage from the analog pin
    MQ2.readSensor(); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
    MQ2.serialDebug(); // Will print the table on the serial port

    // Check for LPG gas concentration
    if (MQ2.getPPM() > 200) { // Threshold for LPG gas detection
        Serial.println("LPG gas detected!");
        digitalWrite(relayPin, HIGH); // Activate water pump to extinguish fire
        digitalWrite(exhaustFanPin, HIGH); // Turn on exhaust fan
        gasRegulator.write(90); // Open gas regulator (adjust angle as needed)
    } else {
        digitalWrite(relayPin, LOW); // Deactivate water pump
        digitalWrite(exhaustFanPin, LOW); // Turn off exhaust fan
        gasRegulator.write(0); // Close gas regulator
    }

    // Check flame sensor
    if (digitalRead(flameSensorPin) == HIGH) {
        Serial.println("Flame detected! Activating safety measures.");
        digitalWrite(relayPin, HIGH); // Activate water pump
        digitalWrite(exhaustFanPin, HIGH); // Turn on exhaust fan
    }

    delay(500); // Sampling frequency
}

/* END CODE */