-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinal_Arduino_code.ino
More file actions
175 lines (150 loc) · 4.35 KB
/
Final_Arduino_code.ino
File metadata and controls
175 lines (150 loc) · 4.35 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#define BLYNK_TEMPLATE_ID "TMPL37sZd6ZUF"
#define BLYNK_TEMPLATE_NAME "Smart Urban Streetlight"
#include <SoftwareSerial.h>
#include <BlynkSimpleStream.h>
// Define pins
#define LDR_PIN A0
#define SOUND_SENSOR A1
#define VIBRATION_SENSOR A2
#define IR1 2
#define IR2 3
#define IR3 4
#define IR4 5
#define LED1 6
#define LED2 7
#define LED3 8
#define LED4 9
#define RED_LED 10
#define BUZZER 11
// Blynk authentication
char auth[] = "YourAuthToken"; // Replace with your Blynk auth token
// Thresholds
const int LDR_THRESHOLD = 500; // Adjust based on your LDR calibration
const int SOUND_THRESHOLD = 60; // Adjust based on your sound sensor calibration
const int VIBRATION_THRESHOLD = 50; // Adjust based on your vibration sensor calibration
// Software Serial for communication with ESP8266
SoftwareSerial espSerial(9, 10); // RX, TX (connected to ESP8266 via level shifter)
// Variables
bool accidentDetected = false;
unsigned long lastAccidentTime = 0;
const unsigned long accidentTimeout = 30000; // 30 seconds
void setup() {
// Initialize pins
pinMode(LDR_PIN, INPUT);
pinMode(SOUND_SENSOR, INPUT);
pinMode(VIBRATION_SENSOR, INPUT);
pinMode(IR1, INPUT_PULLUP);
pinMode(IR2, INPUT_PULLUP);
pinMode(IR3, INPUT_PULLUP);
pinMode(IR4, INPUT_PULLUP);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(RED_LED, OUTPUT);
pinMode(BUZZER, OUTPUT);
// Initialize serial communication
Serial.begin(9600);
espSerial.begin(9600);
// Initialize Blynk
Blynk.begin(espSerial, auth); // Correct parameter order
// Turn off all LEDs initially
allLedsOff();
}
void loop() {
Blynk.run();
// Read sensor values
int ldrValue = analogRead(LDR_PIN);
int soundValue = analogRead(SOUND_SENSOR);
int vibrationValue = analogRead(VIBRATION_SENSOR);
// Day/Night detection
if (ldrValue > LDR_THRESHOLD) { // Daytime
allLedsOff();
}
else { // Nighttime
// Set dim lighting
analogWrite(LED1, 50);
analogWrite(LED2, 50);
analogWrite(LED3, 50);
analogWrite(LED4, 50);
// Check for motion detection (IR sensors are active LOW)
if (digitalRead(IR1) == LOW) analogWrite(LED1, 255);
if (digitalRead(IR2) == LOW) analogWrite(LED2, 255);
if (digitalRead(IR3) == LOW) analogWrite(LED3, 255);
if (digitalRead(IR4) == LOW) analogWrite(LED4, 255);
}
// Accident detection (sound + vibration)
if (!accidentDetected &&
soundValue > SOUND_THRESHOLD &&
vibrationValue > VIBRATION_THRESHOLD) {
triggerAccident();
}
// Reset accident detection after timeout
if (accidentDetected && (millis() - lastAccidentTime > accidentTimeout)) {
resetAccident();
}
// Check for SOS messages from ESP8266
if (espSerial.available()) {
String message = espSerial.readStringUntil('\n');
message.trim();
if (message == "SOS_TRIGGERED") {
triggerSOS();
}
else if (message == "RESET") {
resetSystem();
}
}
delay(100);
}
// Helper functions
void allLedsOff() {
analogWrite(LED1, 0);
analogWrite(LED2, 0);
analogWrite(LED3, 0);
analogWrite(LED4, 0);
digitalWrite(RED_LED, LOW);
digitalWrite(BUZZER, LOW);
}
void triggerAccident() {
accidentDetected = true;
lastAccidentTime = millis();
digitalWrite(RED_LED, HIGH);
digitalWrite(BUZZER, HIGH);
//arduinoSerial.println("⚠️ Accident Detected!");
Blynk.logEvent("accident_alert", "Emergency: ⚠️ Accident Detected!");
}
void resetAccident() {
accidentDetected = false;
digitalWrite(RED_LED, LOW);
digitalWrite(BUZZER, LOW);
}
void triggerSOS() {
digitalWrite(RED_LED, HIGH);
digitalWrite(BUZZER, HIGH);
//arduinoSerial.println("🚨 SOS Button Pressed!");
Blynk.logEvent("sos_alert", "Emergency: 🚨 SOS Button Pressed!");
delay(10000); // 10 second alarm
digitalWrite(BUZZER, LOW);
}
void resetSystem() {
digitalWrite(RED_LED, LOW);
digitalWrite(BUZZER, LOW);
}
// Blynk virtual pin handlers
BLYNK_WRITE(V0) { digitalWrite(LED1, param.asInt()); }
BLYNK_WRITE(V1) { digitalWrite(LED2, param.asInt()); }
BLYNK_WRITE(V2) { digitalWrite(LED3, param.asInt()); }
BLYNK_WRITE(V3) { digitalWrite(LED4, param.asInt()); }
BLYNK_WRITE(V4) {
if (param.asInt()) {
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
} else {
allLedsOff();
}
}
BLYNK_WRITE(V5) {
resetSystem();
}