ekobagus15
Published © MIT

Personal Accident Android

This project is used to determine the occurrence of vehicle accidents by determining the fastest route to the victim.

AdvancedProtip169
Personal Accident Android

Things used in this project

Hardware components

Arduino UNO & Genuino UNO
Arduino UNO & Genuino UNO
×1
Analog Accelerometer: ADXL335
Adafruit Analog Accelerometer: ADXL335
×1
Arduino GSM shield V2
Arduino GSM shield V2
×1
Li-Ion Battery 1000mAh
Li-Ion Battery 1000mAh
×1
TinyShield GPS
TinyCircuits TinyShield GPS
×1
SparkFun Breadboard Power Supply Stick 5V/3.3V
SparkFun Breadboard Power Supply Stick 5V/3.3V
×1

Software apps and online services

Android Studio
Android Studio
MIT App Inventor 2
MIT App Inventor 2
Arduino IDE
Arduino IDE

Story

Read more

Custom parts and enclosures

Skema

skema

Code

Code Modul Vihacle Android

C/C++
// Cabling for i2c,
//using Sparkfun breakout with an Arduino Uno / Duemilanove:
// Arduino <-> Breakout board
// Gnd      -  GND
// 3.3v     -  VCC
// 3.3v     -  CS
// Analog 4 -  SDA
// Analog 5 -  SLC

// Cabling for i2c using Sparkfun breakout with an Arduino Mega / Mega ADK:
// Arduino <-> Breakout board
// Gnd      -  GND
// 3.3v     -  VCC
// 3.3v     -  CS
// 20       -  SDA
// 21       -  SLC

#include <Wire.h>
#include <SoftwareSerial.h>

#include <TinyGPS.h>

#define DEVICE (0x53) // Device address as specified in data sheet 


TinyGPS gps;
SoftwareSerial ss(10, 11);

static void smartdelay(unsigned long ms);
static void print_float(float val, float invalid, int len, int prec);
static void print_int(unsigned long val, unsigned long invalid, int len);
static void print_date(TinyGPS &gps);
static void print_str(const char *str, int len);
char inchar;

byte _buff[6];

char POWER_CTL = 0x2D;  //Power Control Register
char DATA_FORMAT = 0x31;
char DATAX0 = 0x32; //X-Axis Data 0
char DATAX1 = 0x33; //X-Axis Data 1
char DATAY0 = 0x34; //Y-Axis Data 0
char DATAY1 = 0x35; //Y-Axis Data 1
char DATAZ0 = 0x36; //Z-Axis Data 0
char DATAZ1 = 0x37; //Z-Axis Data 1
int xmax = 0;
int ymax = 0;
int zmax = 0;
int xmax2 = 0;
int ymax2 = 0;
int zmax2 = 0;

const int xMin = 100; // (-1 G)
const int xMax = 596; // (+1 G)

const int yMin = 100; // (-1 G)
const int yMax = 597; // (+1 G)

const int zMin = 100; // (-1 G)
const int zMax = 612; // (+1 G)

int xVal = 0;
int yVal = 0;
int zVal = 0;


float flat, flon;
unsigned long age, date, time, chars = 0;
unsigned short sentences = 0, failed = 0;
static const double MALANG_LAT = 51.508131, MALANG_LON = -0.128002;



void setup()
{
  pinMode(4, OUTPUT);
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output. Make sure you set your Serial Monitor to the same!
  Serial.print("init");

  //Put the ADXL345 into +/- 4G range by writing the value 0x01 to the DATA_FORMAT register.
  writeTo(DATA_FORMAT, 0x01);
  //Put the ADXL345 into Measurement Mode by writing 0x08 to the POWER_CTL register.
  writeTo(POWER_CTL, 0x08);
  digitalWrite(4, 1);
  ss.begin(9600);
}


void loop()
{
  readAccel(); // read the x/y/z tilt
  
}

void readAccel() {

  xVal = analogRead(0);
  yVal = analogRead(1);
  zVal = analogRead(2);

  long xScaled = map(xVal, xMin, xMax, -1000, 1000);
  long yScaled = map(yVal, yMin, yMax, -1000, 1000);
  long zScaled = map(zVal, zMin, zMax, -1000, 1000);


  uint8_t howManyBytesToRead = 6;
  readFrom( DATAX0, howManyBytesToRead, _buff); //read the acceleration data from the ADXL345

  // each axis reading comes in 10 bit resolution, ie 2 bytes.  Least Significat Byte first!!
  // thus we are converting both bytes in to one int
  int x = (((int)_buff[1]) << 8) | _buff[0];
  int y = (((int)_buff[3]) << 8) | _buff[2];
  int z = (((int)_buff[5]) << 8) | _buff[4];
  
  if (x > xmax)
  {
    xmax = x;
  }
  if (y > ymax)
  {
    ymax = y;
  }
  if (z > zmax)
  {
    zmax = z;
  }

  if (xScaled > xmax2)
  {
    xmax2 = xScaled;
  }
  if (yScaled > ymax2)
  {
    ymax2 = yScaled;
  }
  if (zScaled > zmax2)
  {
    zmax2 = zScaled;
  }

  if ((xmax >= 150) && (ymax >= 150) && (zmax >= 150))
  {

    if ((xmax2 >= 150) && (ymax2 >= 150) && (zmax2 >= 150))
    {
    digitalWrite(4, 0);
    gps.f_get_position(&flat, &flon, &age);
    smartdelay(1000);
    gps.f_get_position(&flat, &flon, &age);
    smartdelay(1000);

    Serial.println("AT");
    delay(1000);
    Serial.println("AT+CMGF=1");
    delay(1000);
    Serial.println("AT+CMGS=\"mobile phone"\r");
    delay(1000);
    Serial.print(flat, 6);
    Serial.print(",");
    Serial.println(flon, 6);
    delay(1000);
    Serial.println((char)26);
    delay(1000);
     Serial.println("AT+CMGS=\"mobile phone"\r");
    delay(1000);
    Serial.print(flat, 6);
    Serial.print(",");
    Serial.println(flon, 6);
    delay(1000);
    Serial.println((char)26);
    delay(2000);
     Serial.println("AT+CMGS=\"mobile phone\"\r");
    delay(1000);
    Serial.print(flat, 6);
    Serial.print(",");
    Serial.println(flon, 6);
    delay(1000);
    Serial.println((char)26);
    delay(1000);
     Serial.println("AT+CMGS=\"mobile phone"\r");
    delay(1000);
    Serial.print(flat, 6);
    Serial.print(",");
    Serial.println(flon, 6);
    delay(1000);
    Serial.println((char)26);
    delay(3000);
     Serial.println("AT+CMGS=\"mobile phone"\r");
    delay(1000);
    Serial.print(flat, 6);
    Serial.print(",");
    Serial.println(flon, 6);
    delay(1000);
    Serial.println((char)26);
    delay(4000);
     Serial.println("AT+CMGS=\"mobile phone"\r");
    delay(1000);
    Serial.print(flat, 6);
    Serial.print(",");
    Serial.println(flon, 6);
    delay(1000);
    Serial.println((char)26);
    delay(5000);
     Serial.println("AT+CMGS=\"mobile phone"\r");
    delay(1000);
    Serial.print(flat, 6);
    Serial.print(",");
    Serial.println(flon, 6);
    delay(1000);
    Serial.println((char)26);
    delay(6000);
  

    boolean in = false;
    do
    {
      if (Serial.available() > 0)
      {
        inchar = Serial.read ();
        Serial.print ((char)inchar);
        if (inchar == 'e')
        {
          delay (10);
          inchar = Serial.read();
          Serial.print ((char)inchar);
          if (inchar == 'c')
          {
            delay (10);
            inchar = Serial.read();
            Serial.print ((char)inchar);
            if (inchar == '0')
            {
              in = true;
              digitalWrite(4, 0);
              delay (10);
              digitalWrite(4, 1);
              goto sini;
            }
          }
        }
      }
    } while (in == false);
sini:
    in = false;
    xmax = 0;
    ymax = 0;
    zmax = 0;
    digitalWrite(4, 1);
  }
  }
}

void writeTo(byte address, byte val) 
{
  Wire.beginTransmission(DEVICE); // start transmission to device
  Wire.write(address);             // send register address
  Wire.write(val);                 // send value to write
  Wire.endTransmission();         // end transmission
}

// Reads num bytes starting from address register on device in to _buff array
void readFrom(byte address, int num, byte _buff[]) 
{
  Wire.beginTransmission(DEVICE); // start transmission to device
  Wire.write(address);             // sends address to read from
  Wire.endTransmission();         // end transmission

  Wire.beginTransmission(DEVICE); // start transmission to device
  Wire.requestFrom(DEVICE, num);    // request 6 bytes from device

  int i = 0;
  while (Wire.available())        // device may send less than requested (abnormal)
  {
    _buff[i] = Wire.read();    // receive a byte
    i++;
  }
  Wire.endTransmission();         // end transmission
}
static void smartdelay(unsigned long ms)
{
  unsigned long start = millis();
  do
  {
    while (ss.available())
      gps.encode(ss.read());
  } while (millis() - start < ms);
}

static void print_float(float val, float invalid, int len, int prec)
{
  if (val == invalid)
  {
    while (len-- > 1)
      Serial.print('*');
    Serial.print(' ');
  }
  else
  {
    Serial.print(val, prec);
    int vi = abs((int)val);
    int flen = prec + (val < 0.0 ? 2 : 1); // . and -
    flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
    for (int i = flen; i < len; ++i)
      Serial.print(' ');
  }
}

Credits

ekobagus15

ekobagus15

1 project • 0 followers

Comments

Add projectSign up / Login