Vehicle Tracking Project

I am fairly new to Arduino and its environment. This is my very first project as well. The definition goes like this: System should detect the car location through GPS and should be able to store data in SD card every few minutes as a log. Also whenever someone calls the GSM module, it should be able to extract the calling number compare it with a list of numbers store on SD card and on verification do some appropriate task. The message stating accident detection and movement detection are yet to be implemented.

I need 3 helps as of now:

  1. I am using an Arduino UNO. With the code I have written, I am facing low memory issue. Help me optimize the same code where I could use the same library and code to achieve my goal.

  2. For accident detection, I am suppose to use a gyroscope and accelerometer. Can someone suggest me at what angle and direction would I detect that? I mean the conditions for code?

  3. I am also trying to implement a mechanism (kind of basic theft detection) where if someone moves the car without switching of the switch (or button) when it enters. The changes in coordinates should be detected and an alert message for a theft like scenario should be sent.

#include <SoftwareSerial.h>
#include <TinyGPS++.h>
#include <SD.h>
#include <SPI.h>

TinyGPSPlus gps;
SoftwareSerial ss(6, 7); //used with GPS

//GSM goes in pin 0,1

File myFile;
String buffer[10];
boolean SDfound,flag=0;

byte i = 0,n,old_min,old_hour,in;
String sta,s,num=""; //number goes here
char la_t[10];
char lon_g[10];

void setup() {
  
  Serial.begin(9600);
  delay(100);
  ss.begin(9600);
  Serial.println("ATE0");
  
  if (SDfound == 0) {
    if (!SD.begin(4)) {
      Serial.println(F("The SD card cannot be found"));
      while(1);
    }
  }
  SDfound = 1;
  
  myFile = SD.open("Welcome.txt");
  if (!myFile) {
    Serial.println(F("The text file cannot be opened"));
    while(1);
  }

  while (myFile.available()) {
    buffer[i++] = myFile.readStringUntil('\n'); 
    delay(50);
  }
  buffer[i++]=num;
  n=0;
  myFile.close();

}

void loop(){
  
  if(Serial.available()>0)
    sta=Serial.readString();
  
  s=sta.substring(21,31);
  if(s.length()==10){
      Serial.println("ATH\r\n");
      for(n=0;n<i;n++){
        if(s.equals(buffer[n])){
          flag=1;
          break;
        }
      }
      if(flag==1){
        in=0;
        flag=0;
      }
      else
        in=1;
      Serial.println(in);
      send_sms(n,in);
      delay(2000);    
  }
  
  while(ss.available()>0)
    if(gps.encode(ss.read()))
      displayInfo();  

}  

void displayInfo(){ 
  
  collect();
  
  if (gps.time.isValid() && n==1){
    n=0;
    old_hour=gps.time.hour();
    old_min=gps.time.minute();
  }

  if(gps.time.isValid()){
    if(gps.time.minute()-old_min>=1 || gps.time.minute()-old_min<=-1){
      input();        
      old_min=gps.time.minute();
    }
    if(gps.time.hour()-old_hour>=1 || gps.time.hour()-old_hour<=-1){
      input();
      old_hour=gps.time.hour();
    }
  }
}

void input() {
    myFile = SD.open("Data_log.txt",FILE_WRITE);
    if (!myFile) {
      Serial.print(F("The text file cannot be opened"));
      while(1);
    }
    else {
      collect();
      myFile.print(F("Location: Latitude = "));
      myFile.print(la_t);
      myFile.print(F(", Longitude = "));
      myFile.print(lon_g);
      myFile.print("\n");
      myFile.print(F("Time: "));
      myFile.print(gps.time.hour());
      myFile.print(":");
      myFile.print(gps.time.minute());
      myFile.print(F(" Date: "));
      myFile.print(gps.date.day());
      myFile.print("/");
      myFile.print(gps.date.month());
      myFile.print("/");
      myFile.print(gps.date.year());
      Serial.println(F("Added"));
      delay(150);
    }
    myFile.close();
}

void collect() {
  if (gps.location.isValid() && gps.date.isValid() && gps.time.isValid()){
    dtostrf(gps.location.lat(),9,6,la_t);
    dtostrf(gps.location.lng(),9,6,lon_g);
  }
}

void send_sms(byte x,byte y){
  if(gps.location.isValid()){
  delay(200);
  //String msg="AT+CMGS=\"+91"+ buffer[x] +"\"\r";
  if(y==0){    
    //Serial.println("AT+CMGF=1");
    //Serial.println(msg);  
    delay(200);
    Serial.println("Vehicle Tracked. Location: " + String(la_t) + "," + String(lon_g));
  }
  if(y==1){
    //Serial.println("AT+CMGF=1");
    //Serial.println(msg);  
    delay(200);
    Serial.println("Number " + s + " tried to access Location: " + String(la_t) + "," + String(lon_g));
  }
  if(y==2){
    for(n=0;n<i;n++){
      //Serial.println("AT+CMGF=1");
      //String msg="AT+CMGS=\"+91"+ buffer[n] +"\"\r";
      //Serial.println(msg);  
      delay(200);
      Serial.println("Accident Detected. Location: " + String(la_t) + "," + String(lon_g));
    }
  }
  if(y==3){
    //Serial.println("AT+CMGF=1");
    //String msg="AT+CMGS=\"+91"+ num +"\"\r";
    //Serial.println(msg);  
    delay(200);
    Serial.println("Movement Detected. Location: " + String(la_t) + "," + String(lon_g));
  }
  delay(200); 
  Serial.write(26);
}
}

Thank You So Much

Accelerometers are best for accident detection. They are used for air bag deployment.

The trip point will depend on the accelerometer chosen, but you could look up the g value that is typically used for air bags.

jremington: Accelerometers are best for accident detection. They are used for air bag deployment.

The trip point will depend on the accelerometer chosen, but you could look up the g value that is typically used for air bags.

Can you help me with its coding? I have never worked with them actually. I am using a GY-521 MPU 6050 3 axis gyro sensor + 3 axis accelerometer module

There are lots of tutorials on line for that sensor. Work your way through one and if you have specific questions, post them on the forum.

Help me optimize the same code where I could use the same library and code to achieve my goal.

Why? Because you didn't like that answers you got in you other thread (or is it threads)? Get over it.

PaulS: Why? Because you didn't like that answers you got in you other thread (or is it threads)? Get over it.

Not at all brother....I had already done those optimization things which you suggested me........And talking about working on same code is because I had written that thing and I have faith in that and I don't want to use anyone else code atleast in my learning phase.........After implementing with this code, I would give other methods a try as well but I don't want to leave this thing in between and just play a switching game whole life.....It might happen that I use those other library based code and in few days someone else pops out with the thing that it would have these issues so you used this things......Actually I want to create atleast 1 working copy first and then make it efficient in any possible mean.........And talking about 2 threads? This is purely for adding code to the project and not solving issues in code.....The code works perfectly fine if it is provided with proper memory

Just one more issue…help me out of this…The sketch works perfectly fine as of now but the only bug is the message sending…The first time it detects the number it verifies it and do the need full but after the delay gets over it again send the message continuously with illegal access attempt (in=1) and number extracted from string in s be ERROR

Kavish5: 1. I am using an Arduino UNO. With the code I have written, I am facing low memory issue. Help me optimize the same code where I could use the same library and code to achieve my goal.

With PaulS and my own suggestions, you could free quite a few amount of SRAM. However, your code still look pretty much the same.

Please read this more carefully: http://forum.arduino.cc/index.php?topic=438284.msg3019973#msg3019973

arduino_314: With PaulS and my own suggestions, you could free quite a few amount of SRAM. However, your code still look pretty much the same.

Please read this more carefully: http://forum.arduino.cc/index.php?topic=438284.msg3019973#msg3019973

Sorry haven't updated my code in the code section here.....have a look at it now.....if you could find some more issues please notify me......also help me stop that continuous messaging

Kavish5:
if you could find some more issues please notify me…also help me stop that continuous messaging

You still have a lot to cleanup and more SRAM will be available after and code will become more readable…

Honestly I do not know what is the purpose of this code and around it:

  for(n=0;n<i;n++){
      if(s.equals(buffer[n])){
         flag=1;
         break;
      }
  }

Get rid completely of String usage!

Clean all code and you will then see what is wrong.
More simplest code, more quickly you and anyone else will find what is wrong.

arduino_314:
Honestly I do not know what is the purpose of this code and around it:

  for(n=0;n<i;n++){

if(s.equals(buffer[n])){
        flag=1;
        break;
      }
  }




Get rid completely of String usage! 

Clean all code and you will then see what is wrong. 
More simplest code, more quickly you and anyone else will find what is wrong.

That thing verifies if the number who is calling the GSM is authenticated to get the location or not

Can you tell me why I am not able to stop the messages(sms) once someone calls the GSM

In send_sms function the strings which I have are the actual message which I would like to send to the user. Can it be place under F() macro or how can i get rid of those strings? Since they would be send as message and since I use serial for gsm can it free up the space?