#define gasSensorPin 0 // Analog output pin of the gas sensor
#define trigPin 14 // Trigger pin for the ultrasonic sensor
#define echoPin 12 // Echo pin for the ultrasonic sensor
#define threshold 500 // Gas detection threshold (adjust based on your sensor)
#define distanceThreshold 200 // Distance threshold for person detection (in cm)
int gasSensorValue;
long duration;
int distance;
void setup() {
Serial.begin(9600); // Initialize serial communication for debugging
pinMode(gasSensorPin, INPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
gasSensorValue = analogRead(gasSensorPin);
Serial.print("Gas Sensor Value: ");
Serial.println(gasSensorValue);
// Check if gas level is above threshold
if (gasSensorValue > threshold) {
Serial.println("Gas Detected!");
triggerUltrasonic(); // Trigger ultrasonic sensor when gas is detected
// Check for people in close proximity after gas detection
if (isPersonPresent()) {
// Add actions here - sound alarm, turn on lights, etc.
Serial.println("Person Detected in Gas Leak Area!");
// Add your desired actions here, for example:
// activateAlarm();
// turnOnLights();
}
}
delay(500); // Adjust delay based on desired detection frequency
}
void triggerUltrasonic() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
}
int isPersonPresent() {
// Trigger ultrasonic sensor
triggerUltrasonic();
// Measure distance
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.println(distance);
return (distance <= distanceThreshold); // True if person is within threshold
}