-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinalProjectCode.ino
More file actions
163 lines (133 loc) · 3.86 KB
/
FinalProjectCode.ino
File metadata and controls
163 lines (133 loc) · 3.86 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
#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
#include "TinyGPS++.h"
#define RXPin 10
#define TXPin 13
#define GPSBaud 9600
#define ConsoleBaud 9600
//ss - GPS, serial - GSM
// Define the pins connected to the SIM800L module
#define RX_PIN 2
#define TX_PIN 3
// Create a software serial object to communicate with the SIM800L
SoftwareSerial serial(RX_PIN, TX_PIN);
// Function to send an AT command and wait for a response
String sendAT(String cmd) {
serial.println(cmd);
delay(1000);
String response = "";
while (serial.available()) {
response += char(serial.read());
}
return response;
}
// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);
// The TinyGPS+ object
TinyGPSPlus gps;
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
float sensorValue;
const int sensorPin = A0;
const int buzzerpin = A5;
int motor1pin1 = 13;
int motor1pin2 = 10;
int motor2pin1 = 9;
int motor2pin2 = 8;
String message;
void setup() {
lcd.begin(16, 2);
Serial.begin(ConsoleBaud);
ss.begin(GPSBaud);
serial.begin(GPSBaud);
Serial.print("MQ3 warming up!");
pinMode(sensorPin, INPUT);
// put your setup code here, to run once:
pinMode(motor1pin1, OUTPUT);
pinMode(motor1pin2, OUTPUT);
pinMode(motor2pin1, OUTPUT);
pinMode(motor2pin2, OUTPUT);
pinMode(buzzerpin, OUTPUT);
digitalWrite(buzzerpin, LOW);
}
void loop() {
while (ss.available() > 0){
gps.encode(ss.read());
}
// Let's display the new location and altitude
// whenever either of them have been updated.
//if (gps.location.isUpdated() || gps.altitude.isUpdated())
//{
Serial.print("Location: ");
Serial.print(gps.location.lat(), 6);
Serial.print(",");
Serial.print(gps.location.lng(), 6);
Serial.print(" Altitude: ");
Serial.println(gps.altitude.meters());
//}
//sensorValue = 280;
// put your main code here, to run repeatedly:
sensorValue = analogRead(sensorPin);
Serial.print("Sensor Value: ");
Serial.println(sensorValue);
Serial.print("\n");
if(sensorValue<250){
digitalWrite(buzzerpin, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.clear();
lcd.print("Sober");
lcd.setCursor(0, 1);
lcd.print(sensorValue);
Serial.print("You good,\n ");
digitalWrite(motor1pin1, HIGH);
digitalWrite(motor1pin2, LOW);
digitalWrite(motor2pin1, HIGH);
digitalWrite(motor2pin2, LOW);
delay(1000);
digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, HIGH);
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, HIGH);
delay(1000);
} else if(sensorValue>250){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Drunk!!!");
lcd.setCursor(0, 1);
lcd.print(sensorValue);
Serial.print("\nYou drunk!, ");
message = "ALERT! DRUNK DRIVER";
Serial.print(sensorValue);
digitalWrite(buzzerpin, HIGH);
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, LOW);
delay(1000);
String response = sendAT("AT");
if (response == "OK") {
Serial.println("SIM800L is ready");
} else {
Serial.println("Error: SIM800L not ready");
return;
}
// Define the phone number and message
String phoneNumber = "+919021292137"; // Replace with recipient's number
// Send SMS command
String command = "AT+CMGS=\"" + phoneNumber + "\"\r\n";
sendAT(command);
delay(1000); // Wait for module to process
// Send the message content
serial.print(message);
delay(1000); // Wait for message to be sent
// Check SMS sending status
response = sendAT("AT+CMSS=1");
if (response == "+CMSS: 1") {
Serial.println("SMS sent successfully");
} else {
Serial.println("Error: SMS sending failed");
}
// Delay between messages (optional)
Serial.print("\n");
delay(10000);
}
}