-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_system.ino
159 lines (139 loc) · 4.41 KB
/
main_system.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <Servo.h>
// Pin Definitions
#define LDRpin A2 // LDR Sensor Pin
#define MQ2pin A4 // MQ-2 Gas Sensor Pin
#define TEMPpin A0 // Temperature Sensor (Thermistor) Pin
const int TRIG_PIN = 3; // Ultrasonic Sensor TRIG pin
const int ECHO_PIN = 13; // Ultrasonic Sensor ECHO pin
const int SERVO_PIN = 11; // Servo Motor pin
const int BUZZER_PIN = 7; // Buzzer pin
const int LED_PIN = 10; // LED pin
// Thresholds
const int DISTANCE_THRESHOLD = 10; // Ultrasonic Distance Threshold in cm
const int TEMP_THRESHOLD = 50; // Temperature Threshold (raw value)
const int GAS_THRESHOLD = 200; // Gas Threshold (raw value)
const int LDR_THRESHOLD = 100; // LDR Threshold (raw value)
// Global Variables
Servo servo; // Servo Motor Object
float distance_cm; // Ultrasonic Sensor Value
float tempValue; // Temperature Sensor Value
float gasValue; // MQ-2 Gas Sensor Value
int LDRValue; // LDR Sensor Value
void setup() {
Serial.begin(9600); // Initialize Serial Monitor
// Pin Modes
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(LDRpin, INPUT);
pinMode(MQ2pin, INPUT);
pinMode(TEMPpin, INPUT);
servo.attach(SERVO_PIN);
servo.write(90); // Initial state: Servo closed
// Ensure initial states
digitalWrite(BUZZER_PIN, LOW);
digitalWrite(LED_PIN, LOW);
}
void updateUltrasonic() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
float duration_us = pulseIn(ECHO_PIN, HIGH);
distance_cm = 0.017 * duration_us;
if (distance_cm <= 0 || distance_cm > 400) {
distance_cm = 400; // Out of range
}
}
void updateSensorValues() {
tempValue = analogRead(TEMPpin);
gasValue = analogRead(MQ2pin);
}
bool isAlarmConditionActive() {
return (tempValue < TEMP_THRESHOLD || gasValue > GAS_THRESHOLD);
}
void playAlarm() {
while (true) {
digitalWrite(LED_PIN, HIGH); // Turn on the LED
Serial.println("Alarm is active!");
// Check sensor values before each pattern
updateSensorValues();
if (!isAlarmConditionActive()) {
Serial.println("Conditions returned to normal - stopping alarm");
stopAlarm();
break;
}
// Pattern 1
for (int i = 0; i < 80; i++) {
digitalWrite(BUZZER_PIN, HIGH);
delay(1);
digitalWrite(BUZZER_PIN, LOW);
delay(1);
// Check every few iterations to make the response more immediate
if (i % 20 == 0) {
updateSensorValues();
if (!isAlarmConditionActive()) {
Serial.println("Conditions returned to normal - stopping alarm");
stopAlarm();
return;
}
}
}
// Pattern 2
for (int i = 0; i < 100; i++) {
digitalWrite(BUZZER_PIN, HIGH);
delay(2);
digitalWrite(BUZZER_PIN, LOW);
delay(2);
// Check every few iterations to make the response more immediate
if (i % 25 == 0) {
updateSensorValues();
if (!isAlarmConditionActive()) {
Serial.println("Conditions returned to normal - stopping alarm");
stopAlarm();
return;
}
}
}
}
}
void stopAlarm() {
digitalWrite(BUZZER_PIN, LOW);
digitalWrite(LED_PIN, LOW);
Serial.println("Alarm stopped.");
}
void loop() {
// Update Sensor Values
updateUltrasonic();
LDRValue = analogRead(LDRpin);
updateSensorValues();
// Log Sensor Values to Serial Monitor
Serial.print("Distance: ");
Serial.print(distance_cm);
Serial.println(" cm");
Serial.print("LDR Value: ");
Serial.println(LDRValue);
Serial.print("Temperature Value (Raw): ");
Serial.println(tempValue);
Serial.print("Gas Value (Raw): ");
Serial.println(gasValue);
// Conditional Logic
if (isAlarmConditionActive()) {
Serial.println("Condition met for alarm: Temp < 50 or Gas > 200");
servo.write(90); // Close the servo
playAlarm(); // Activate alarm
}
else if (LDRValue > LDR_THRESHOLD && distance_cm <= DISTANCE_THRESHOLD) {
Serial.println("Condition met for servo open: LDR > 100 and Distance <= 10 cm");
servo.write(0); // Open the servo
stopAlarm(); // Deactivate alarm
}
else {
Serial.println("No conditions met for alarm, servo closed.");
servo.write(90); // Close the servo
stopAlarm(); // Deactivate alarm
}
delay(500); // Delay for stability
}