code stops suddenly

My project is for a modified RC plane that will fly and navigate by itself. It has an MPU6050 accelerometer/gyro and a adafruit breakout GPS module. I can get the gyro to control the plane and make it fly steady. I have the GPS reading and printing values to the serial monitor. It uses 45% of storage space and 42% of dynamic memory. I am still in the process of working on this project so the code is not done yet. The main problem I am having is that the program will run for 2 minutes and then stop suddenly for no reason. This is a major problem because I need the plane to run for more than two minutes.
My question is this: why is my code stopping and how can I fix it?

#include “Wire.h”
#include <MPU6050_light.h>

#include <Servo.h>

#include <SoftwareSerial.h> //gps
#include <TinyGPS.h>

int x;
int y;
int z;
int throttle;

Servo servoELV;
Servo servoAL;
Servo servoAR;
Servo servodcmotor;

MPU6050 mpu(Wire);
unsigned long timer = 0;
unsigned long age, date, time, chars = 0;

static const double target_LAT = 29.9511, target_LON = -90.0715;

TinyGPS gps;
SoftwareSerial ss(4, 3);
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); //gps

void setup(){
Serial.begin(9600);
Wire.begin();
mpu.begin();
Serial.println(F(“Calculating gyro offset, do not move MPU6050”));
delay(1000);
mpu.calcGyroOffsets(); //calculates offset so gyro values start at 0
Serial.println(“Done!\n”);

pinMode(13,OUTPUT);
pinMode(12,OUTPUT);
pinMode(11,OUTPUT);
pinMode(10,OUTPUT);

servoELV.attach(13); //ELIVATORS(UP AND DOWN)
servoAL.attach(12); //LEFT WING
servoAR.attach(11); //RIGHT WING
servodcmotor.attach(10); //MOTOR

ss.begin(9600); //gps

}

void loop() {

float flat, flon; //gps

mpu.update();

if((millis()-timer)>20) // print data every 10ms

int x = mpu.getAngleX();
int y = mpu.getAngleY();
int z = mpu.getAngleZ();
Serial.print("X : “);
Serial.print(mpu.getAngleX());
Serial.print(”\tY : “); //this prints out the gyro values
Serial.print(mpu.getAngleY());
Serial.print(”\tZ : ");
Serial.print(mpu.getAngleZ());

timer = millis();

Serial.print(" GPS “);
gps.f_get_position(&flat, &flon);
Serial.print(” lat");
print_float(flat, TinyGPS::GPS_INVALID_F_ANGLE, 10, 6); // These are the printed values for the GPS
Serial.print(" long");
print_float(flon, TinyGPS::GPS_INVALID_F_ANGLE, 11, 6); //LAT,LONG,ALT,DISTANCE,DEG TO TARGET
Serial.print(" alt");
print_float(gps.f_altitude(), TinyGPS::GPS_INVALID_F_ALTITUDE, 7, 2);
Serial.print(" distance to target:");
print_float(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0xFFFFFFFF : (unsigned long)TinyGPS::distance_between(flat, flon, target_LAT, target_LON) / 1000, 0xFFFFFFFF, 9 ,2);
Serial.print(" course to target:");
print_float(flat == TinyGPS::GPS_INVALID_F_ANGLE ? TinyGPS::GPS_INVALID_F_ANGLE : TinyGPS::course_to(flat, flon, target_LAT, target_LON), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2);
Serial.print(“deg to target”);
Serial.print((TinyGPS::course_to(flat, flon, target_LAT, target_LON))-(gps.f_course(), TinyGPS::GPS_INVALID_F_ANGLE));

Serial.println();
smartdelay(10);
// servodcmotor.writeMicroseconds(throttle);

if ( y > 5.0)
{
tiltdown();
//digitalWrite(13,HIGH);
}
else if (y < -5.0){
tiltup();
// digitalWrite(10,HIGH);
}
else
{servoELV.write(90);
// digitalWrite(10,LOW);digitalWrite(13,LOW);
}

if ( x > 5){
rollleft();
//digitalWrite(12,HIGH);
}
else if ( x < -5){
rollright(); //This should allow 10deg of error.
//digitalWrite(11,HIGH);
}
else if (( x < 5)&&( x > -5))
{servoAL.write(95);servoAR.write(95);
//digitalWrite(12,LOW);digitalWrite(11,LOW);
}

if ((TinyGPS::course_to(flat, flon, target_LAT, target_LON))-(gps.f_course(), TinyGPS::GPS_INVALID_F_ANGLE)>10){ //calculates the degrees to target and turns accordingly
turnleft();
}
else if ((TinyGPS::course_to(flat, flon, target_LAT, target_LON))-(gps.f_course(), TinyGPS::GPS_INVALID_F_ANGLE)<-10){
turnright();
}
else{}

}//closeloop

void turnright(){

}
void turnleft(){

}
void tiltdown(){

servoELV.write(70);
} //pushes elivators down

void tiltup(){

servoELV.write(110); // pulls elivators up
}

void rollleft(){
servoAL.write(50);
servoAR.write(50);
}

void rollright(){
servoAL.write(140);
servoAR.write(140);
}

//*******************************************************************************/ gps
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) //these functions were copied from the example from the tinyGPS library
{
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(’ ');
}
smartdelay(0);
}

static void print_str(const char *str, int len)
{
int slen = strlen(str);
for (int i=0; i<len; ++i)
Serial.print(i<slen ? str : ’ ');

  • smartdelay(0);*
    }
    //******************************************************************************/gps
    code_first_version.ino (5.23 KB)

Read the how to use this forum-please read sticky to see how to properly post code and some advice on how to ask an effective question. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

To make it easy for people to help you please modify your post and use the code button </>
codeButton.png

so your code 
looks like this

and is easy to copy to a text editor. See How to use the Forum

Your code is too long for me to study quickly without copying to my text editor.

Also please use the AutoFormat tool to indent your code for easier reading.

...R

 Serial.begin(9600);

Very slow baud rate, yet you are trying to log loads of data every 20ms.

Use 115200 baudrate, then your program is likely to actually run at the intended rate.

Also I see an unneeded while loop, always a suspect for a freeze-up in code:

  static void smartdelay(unsigned long ms)
{
  unsigned long start = millis();
  do
  {
    while (ss.available())   <<<<< change while to if
      gps.encode(ss.read());
  } while (millis() - start < ms);
}

I’d also be explicit about the test being for positive count:

    if (ss.available() > 0)
      gps.encode(ss.read());

Perhaps a future version of SoftwareSerial might use negative values as error codes in this function? Be defensive in programming.