Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.9 kB
12
Indexable
Never
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>

Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);
SoftwareSerial gpsSerial(10, 11);  // RX, TX for GPS module

bool crashDetected = false;

void setup() {
  Serial.begin(9600);
  gpsSerial.begin(9600);  // Initialize GPS serial communication

  if (!accel.begin()) {
    Serial.println("Could not find a valid ADXL345 sensor. Check wiring!");
    while (1);
  }
}

void loop() {
  sensors_event_t event;
  accel.getEvent(&event);
  
  // Set your threshold for crash detection
  float threshold = 10.0;

  // Check for a significant change in acceleration
  if (fabs(event.acceleration.x) > threshold || fabs(event.acceleration.y) > threshold || fabs(event.acceleration.z) > threshold) {
    crashDetected = true;
    GPSRecordLocation();
    AlertEmergencyServices();
  }

  // Continuously monitor the accelerometer data
  Serial.print("X: "); Serial.print(event.acceleration.x); Serial.print(" m/s^2\t");
  Serial.print("Y: "); Serial.print(event.acceleration.y); Serial.print(" m/s^2\t");
  Serial.print("Z: "); Serial.print(event.acceleration.z); Serial.print(" m/s^2\t");
  Serial.println();

  delay(1000); // Adjust the delay time as needed
}

void GPSRecordLocation() {
  // Use the TinyGPS++ library to parse GPS data
  TinyGPSPlus gps;

  while (gpsSerial.available() > 0) {
    if (gps.encode(gpsSerial.read())) {
      Serial.print("Location: ");
      Serial.print(gps.location.lat(), 6);
      Serial.print(", ");
      Serial.println(gps.location.lng(), 6);
      // You can store the GPS coordinates or send them as needed
    }
  }
}

void AlertEmergencyServices() {
  if (crashDetected) {
    // Use your Bluetooth or GSM module to send alerts
    // For Bluetooth, you can use Serial Bluetooth communication
    // For GSM, you may need a separate library and hardware
  }
}