Arduino resets at the same spot every time

    //

/*CHECK SD PINS
 * TEST OTHER LIBRARY
 */
  // The SFE_LSM9DS1 library requires both Wire and SPI be
  // included BEFORE including the 9DS1 library.
  #include <Wire.h>
  #include <SPI.h>
  #include <Arduino_LSM9DS1.h>
  #include "SdFat.h"
  
  
  //////////////////////////
  // LSM9DS1 Library Init //
  //////////////////////////
  // Use the LSM9DS1 class to create an object. [imu] can be
  // named anything, we'll refer to that throught the sketch.
  SdFat SD;
  const uint8_t chipSelect = SS;
  #define SD_CS_PIN SS
  #define error(s) SD.errorHalt(PSTR(s))
   
  // GPS
  float latitude;
  float longitude;
  float kph;
  float heading;
  float altitude;

  //etc
  unsigned long timeSampleStart;
  
  #include "Adafruit_FONA.h"
  
  // We default to using software serial. If you want to use hardware serial
  // (because softserial isnt supported) comment out the following three lines 
  // and uncomment the HardwareSerial line
  #include <SoftwareSerial.h>
  // SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
  //SoftwareSerial *fonaSerial = &fonaSS;
  SoftwareSerial *fonaSerial = &SoftwareSerial(3, 2);

  
  // Hardware serial is also possible!
  // if this gives compilation errors, try switching "Serial1" for "Serial"
  //HardwareSerial *fonaSerial = &Serial1;
  
  Adafruit_FONA fona = Adafruit_FONA(4);


void setup()
{
  //fona
  while (! Serial);
  Serial.begin(115200);
  Serial.println(F("Adafruit FONA 808 & 3G GPS demo"));
  Serial.println(F("Initializing FONA... (May take a few seconds)"));

  fonaSerial->begin(4800);
  if (! fona.begin(*fonaSerial)) {
    Serial.println(F("Couldn't find FONA"));
    while(1);
  }
  Serial.println(F("FONA is OK"));

  Serial.println(F("Enabling GPS..."));
  fona.enableGPS(true);

  delay(1000);
  Serial.println(F("delay"));

  //sd
  Serial.println();
  Serial.print(F("Initializing SD card..."));
  if (!SD.begin(7)) {
    Serial.println(F("initialization failed!"));
    return;
  }
  if (!SD.begin(7,SPI_HALF_SPEED)) SD.initErrorHalt();
  Serial.println(F("initialization done."));
  delay(1000);
  Serial.println(F("delay"));
 

 //imu
 if (!IMU.begin()) {
    Serial.println("Failed to initialize IMU!");
    while (1);
  }

  
  delay(1000);
  Serial.println(F("delay"));
  File myFile;
  myFile = SD.open("f.txt"); // clear file after transmission eventually
  SD.remove("f.txt");
  myFile.close();
  
}

void setupGPS()
{
  while (!fona.getGPS(&latitude,&longitude,&kph,&heading,&altitude)) {
    Serial.println(F("Waiting for FONA GPS 3D fix..."));
    delay(2000);
  }
  //Serial.println(F("fuck"));
  //timeSampleStart = millis();
  delay(2000);
  return;
}



void loop()
{  
  float distance;
  float x;
  float y;
  float z;  
  unsigned long time2;
  File myFile;
  myFile = SD.open("f.txt");
  myFile.println("help");
  setupGPS(); 
          
  myFile = SD.open("f.txt",FILE_WRITE);
  if ( IMU.accelerationAvailable() )
  {
    IMU.readAcceleration(x,y,z);
    time2 = millis();
  
    // Calculate distance traveled
     distance = (kph/3.6) * (time2-timeSampleStart);
     if(distance > 10.0){ 
       
      myFile.println();
      myFile.print(latitude);
      myFile.print(' ');
      myFile.print(longitude);
      myFile.print(' ');
      myFile.println(kph);
      timeSampleStart = millis();
         
      }
      
    if((x < .3) && (x > -0.3) && (y < 0.3) && (y > -0.3)){ // Arbitraty bounds to verify vehicle is travelling at constant speed.
       myFile.print(time2);
       myFile.print(' ');
       myFile.println(z);
    }
    else
    {
      myFile.println('x');
    }
  }
  myFile.close();
}

I am using an Arduino Uno with an accelerometer, Fona GPS/GSM, and a microSD slot. The code runs correctly through the initialization. The GPS finds satellites before proceeding in the loop. After getting GPS coordinates, I attempt to read values from the IMU. At "IMU.readAcceleration(x,y,z)", the program resets and goes through initialization again, and we never get further into the loop.

All components work correctly on their own. Is the Arduino crashing when it resets? What about reading acceleration values after acquiring a 3D fix is causing this to crash? Could it be a power issue? I don't have the equipment to check, but I don't believe that is the case. Any tips?

Edit: Sitting at 73% dynamic memory. Can a memory issue cause resets like this?

Is the Arduino crashing when it resets?

Yes.

What about reading acceleration values after acquiring a 3D fix is causing this to crash?

I'd guess that some memory location is being overwritten, causing the processor to do something unexpected.

All components work correctly on their own.

But there are a lot of them... Start with one component working, with just the code for that component. Add another component and it's code. Stop when the issue recurs.

PaulS:
Yes.
I'd guess that some memory location is being overwritten, causing the processor to do something unexpected.
But there are a lot of them... Start with one component working, with just the code for that component. Add another component and it's code. Stop when the issue recurs.

Ok, I've done a bit of unit testing, but I have some ideas. I only worry about it being memory because I've already reduced it from 100% dynamic memory usage to the current 73% and I don't think it can be reduced much further.

Probably a memory problem, the SDcard library uses a fair amount of memory for a buffer, but I do get one warning from the compiler, is that correctly creating an instance of software serial?

/home/Arduino/sketch_apr21c/sketch_apr21c.ino:42:50: warning: taking address of temporary [-fpermissive]
 SoftwareSerial *fonaSerial = &SoftwareSerial(3, 2);
                                                  ^

david_2018:
Probably a memory problem, the SDcard library uses a fair amount of memory for a buffer, but I do get one warning from the compiler, is that correctly creating an instance of software serial?

/home/Arduino/sketch_apr21c/sketch_apr21c.ino:42:50: warning: taking address of temporary [-fpermissive]

SoftwareSerial *fonaSerial = &SoftwareSerial(3, 2);
                                                  ^

Hmm, that doesn't give me a warning.

No the sample code from the example is:

   SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
  SoftwareSerial *fonaSerial = &fonaSS;

but I concatenated it because it seemed unneeded.