// Define pin numbers
const int gasSensorPin = A0; // Analog pin for gas sensor
const int gasDetectionLedPin = 7; // Digital pin for gas detection LED
const int servoControlPin = 9; // Digital pin for servo motor control
const int servoMovementLedPin = 8; // Digital pin for servo movement LED
// Define threshold for gas concentration (adjust as needed)
const int gasThreshold = 500;
#include <Servo.h>
Servo myServo; // Create a servo object to control the servo motor
void setup() {
// Initialize serial communication for debugging
Serial.begin(9600);
// Attach servo to the control pin
myServo.attach(servoControlPin);
// Set LED pins as output
pinMode(gasDetectionLedPin, OUTPUT);
pinMode(servoMovementLedPin, OUTPUT);
}
void loop() {
// Read analog value from gas sensor
int gasConcentration = analogRead(gasSensorPin);
// Print gas concentration for debugging
Serial.print("Gas Concentration: ");
Serial.println(gasConcentration);
// Check if gas concentration exceeds the threshold
if (gasConcentration > gasThreshold) {
// Gas detected
digitalWrite(gasDetectionLedPin, HIGH); // Turn on gas detection LED
// Move servo motor to simulate valve opening
myServo.write(180); // Adjust the angle based on your servo's specifications
// Turn on servo movement LED
digitalWrite(servoMovementLedPin, HIGH);
} else {
// No gas detected
digitalWrite(gasDetectionLedPin, LOW); // Turn off gas detection LED
// Move servo motor to simulate valve closing
myServo.write(0); // Adjust the angle based on your servo's specifications
// Turn off servo movement LED
digitalWrite(servoMovementLedPin, LOW);
}
// Optional: Implement a delay before closing the valve after gas is no longer detected
delay(5000); // Adjust the delay time as needed
}