Motor accident project

so im trying to build a project about motor accident device but its hard to code the parts are mpu6050 neo6m sim900a and an arduino mega2560 we try some codes but it doesn't work the main thing that we needed to do is if the mpu6050 tilt to 20 degrees or 160 it will send a signal to the neo6m to pr

#include <Wire.h>
#include <MPU6050.h>
#include <SoftwareSerial.h>
#include <TinyGPS++.h>

#define MPU6050_ADDRESS 0x68
#define NEO6M_RX_PIN 2
#define NEO6M_TX_PIN 3
#define SIM900A_TX_PIN 4
#define SIM900A_RX_PIN 5

MPU6050 mpu;
SoftwareSerial neo6mSerial(NEO6M_RX_PIN, NEO6M_TX_PIN);
SoftwareSerial sim900aSerial(SIM900A_RX_PIN, SIM900A_TX_PIN);
TinyGPSPlus gps;

void setup() {
  Wire.begin();
  Serial.begin(9600);
  neo6mSerial.begin(9600);
  sim900aSerial.begin(9600);

  mpu.initialize();
}

void loop() {
  int16_t ax, ay, az;
  mpu.getAcceleration(&ax, &ay, &az);

  float tiltAngleX = atan2(ay, az) * 180 / M_PI;
  float tiltAngleY = atan2(ax, az) * 180 / M_PI;

  if (tiltAngleX >= 20 && tiltAngleX <= 160) {
    if (gps.location.isValid()) {
      sendLocationSMS(gps.location.lat(), gps.location.lng());
    } else {
      neo6mSerial.println("$GPGGA");
      delay(1000);
    }
  }

  while (neo6mSerial.available() > 0) {
    if (gps.encode(neo6mSerial.read())) {
      if (gps.location.isValid()) {
        sendLocationSMS(gps.location.lat(), gps.location.lng());
      }
    }
  }
}

void sendLocationSMS(float latitude, float longitude) {
  sim900aSerial.println("AT+CMGF=1");
  delay(100);
  sim900aSerial.print("AT+CMGS=\"recipient_phone_number\"\r");
  delay(100);
  sim900aSerial.print("Latitude: ");
  sim900aSerial.print(latitude, 6);
  sim900aSerial.print(", Longitude: ");
  sim900aSerial.print(longitude, 6);
  sim900aSerial.write(26);
  delay(100);
}

int to the sim900a and send to the receipient

start by writing a code which prints that angle

please read How to get the best out of this forum and post accordingly (including code with code tags and necessary documentation for your ask like your exact circuit and power supply, links to components etc).

Why use SoftwareSerial if you are using a Mega with 4 UARTs (3 available for serial communication with other devices, one for communication with the PC). Multiple SoftwareSerial instances is usually a recipe for problems.

I suggest the following change for starters. Change

SoftwareSerial neo6mSerial(NEO6M_RX_PIN, NEO6M_TX_PIN);
SoftwareSerial sim900aSerial(SIM900A_RX_PIN, SIM900A_TX_PIN);

to

#define neo6mSerial Serial1
#define sim900aSerial Serial2

And use the corresponding pins on the Mega.

iwill try thankyou

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.