hey guys iam making an accident detector using arduino mega,mpu6050 ,neo 6m gps module and sim 800L gsm module.and i want you guys to review my code and tell if it will work and if there are any corrections please tell me and i would be happy about that.and kindly if u can provide any suggestions please feel free to provide them.thank you
here's the code
#include <Wire.h>
#include <TinyGPS++.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_FONA.h>
#include <SoftwareSerial.h>
// Define MPU6050 object
Adafruit_MPU6050 mpu;
// Define GPS object
TinyGPSPlus gps; // Initialize TinyGPS++ object
// Define GSM object
SoftwareSerial gsmSerial(16, 17); // RX, TX (not used on Mega)
Adafruit_FONA fona = Adafruit_FONA(6); // Use the appropriate pin number for your SIM800L module
// Global variables
bool accidentDetected = false;
float lastAccelMag = 0.0;
char phone_number[] = "Your_Phone_Number"; // Replace with your phone number
char message[160]; // Buffer for SMS
float thresholdValue = 10.0; // Adjust this threshold as needed
// Function prototypes
void initializeMPU();
void initializeGPS();
void initializeGSM();
void sendSMS(const char* phoneNumber, const char* message);
void setup() {
Serial.begin(9600); // This is the Serial monitor
// Initialize sensors and modules
initializeMPU();
initializeGPS();
initializeGSM();
}
void loop() {
// Read accelerometer data
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
float accelX = a.acceleration.x;
float accelY = a.acceleration.y;
float accelZ = a.acceleration.z;
float accelMag = sqrt(accelX * accelX +
accelY * accelY +
accelZ * accelZ);
// Check for accident
if (accelMag > thresholdValue && accelMag > lastAccelMag) {
accidentDetected = true;
}
lastAccelMag = accelMag;
// GPS data
while (Serial1.available()) { // Read from Serial1 (GPS)
gps.encode(Serial1.read());
}
if (gps.location.isValid() && accidentDetected) {
// Get latitude and longitude
float latitude = gps.location.lat();
float longitude = gps.location.lng();
// Create Google Maps link
String mapLink = "https://maps.google.com/?q=" + String(latitude, 6) + "," + String(longitude, 6);
// Compose SMS message
sprintf(message, "Accident detected! Location: %s", mapLink.c_str());
// Send SMS
sendSMS(phone_number, message);
accidentDetected = false;
}
}
void initializeMPU() {
if (!mpu.begin()) {
Serial.println("MPU6050 initialization failed");
while (1);
}
}
void initializeGPS() {
Serial1.begin(9600); // Set the GPS module's baud rate to match your module
}
void initializeGSM() {
gsmSerial.begin(9600);
delay(1000);
// Ensure SIM card is accessible
if (!fona.begin(gsmSerial)) {
Serial.println("Couldn't find SIM800L module");
while (1);
}
// Unlock SIM card (replace with your SIM card PIN)
if (!fona.unlockSIM("YOUR_SIM_PIN")) {
Serial.println("SIM card unlock failed");
while (1);
}
}
void sendSMS(const char* phoneNumber, const char* message) {
if (fona.sendSMS(phoneNumber, message)) {
Serial.println("SMS sent successfully");
} else {
Serial.println("Failed to send SMS");
}
}