Sahil
#include <Servo.h>
// Pin definitions
const int mq2Pin 2; // Digital pin for MQ-2 sensor
const int buzzerPin = 3; // Digital pin for buzzer
const int relayPin 4; // Digital pin for relay module
const int servoPin 5; // Digital pin for servo motor
Servo myservo; // Create servo object
void setup() {
pinMode (mq2Pin, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(relayPin, OUTPUT);
myservo.attach(servoPin); // Attaching the servo motor
// Set initial states
deactivateAlarm();
deactivateRelay();
myservo.write(8); // Start with servo at e degrees
}
void loop() {
// Reading from MQ-2 sensor
int gasValue digitalRead(mq2Pin);
// If gas is detected
if (gasValue == LOW) {
activateAlarm();
activateRelay();
// Activate alarm
// Activate relay module
turnServoTo90();
// Turn on servo motor to 90 degrees
} else {
deactivateAlarm();
deactivateRelay();
resetServo();
}
}void activateAlarm() {
// Deactivate alarm
//Deactivate relay module
// Reset servo motor to a degrees
digitalWrite(buzzerPin, HIGH); // Turn on buzzer
}
void deactivateAlarm() [
digitalWrite(buzzerPin, LOW); // Turn off buzzer
void activateRelay() (
digitalWrite(relayPin, LOW); // Activate relay module
void deactivateRelay() {
digitalWrite(relayPin, HIGH); // Deactivate relay module
void turnServoTo90() (
myservo.write(90); // Rotate servo motor to 90 degrees
delay(500);
}
// Allow some time for the servo to move
void resetServo() {
myservo.write(0); // Rotate servo motor to e degrees
delay(500);
}
// Allow some time for the serve to reset
}
Comments
Post a Comment