#include <Arduino.h>
#include <ESP32Servo.h>
const int potPin = A0; // Connect the potentiometer output to pin A0 on the Arduino Uno
const int buzzerPin = 8; // Connect the buzzer to pin 8 on the Arduino Uno
const int servoPin = 9; // Connect the servo to pin 9 on the Arduino Uno
const int gasSensorPin = 1; // Connect the gas sensor to analog pin 1 on the Arduino Uno
Servo gasDetector; // Create a servo object
void setup() {
Serial.begin(9600); // Initialize serial communication
pinMode(buzzerPin, OUTPUT); // Set buzzer pin as output
gasDetector.attach(servoPin); // Connect the servo object to the servo pin
}
void loop() {
int potValue = analogRead(potPin); // Read value from potentiometer
Serial.print("Potentiometer Value: ");
Serial.println(potValue); // Send potentiometer value via serial communication
int gasValue = analogRead(gasSensorPin); // Read value from gas sensor
Serial.print("Gas Sensor Value: ");
Serial.println(gasValue); // Send gas sensor value via serial communication
// If gas sensor value exceeds a certain threshold, move the servo to turn off the gas regulator
if (gasValue > 800) { // Change the threshold value according to your gas sensor characteristics
gasDetector.write(0); // Move servo to 0 degree position (e.g., to turn off the gas regulator)
delay(1000); // Wait for a moment to ensure servo movement
} else {
// If no gas leakage, move servo according to potentiometer value
int angle = map(potValue, 0, 1023, 0, 180); // Map potentiometer value to servo angle
gasDetector.write(angle); // Rotate servo according to potentiometer value
}
if (Serial.available() > 0) { // If there is data available on the serial port
String message = Serial.readStringUntil('\n'); // Read received data from Arduino Uno
Serial.print("Gas Detected: ");
Serial.println(message); // Print received data from Arduino Uno
}
Serial.println("Gas Detection!"); // Send "Gas Detection" message from Arduino Uno
// Generate sound on buzzer based on potentiometer value
tone(buzzerPin, potValue); // Generate sound with frequency based on potentiometer value
delay(1000); // Delay for 1 second before reading again
}