Ardunino 101 Gyroscope Data Freezeing

So I have been working on a research project using an Ardunio 101 to do head rotation stuff within a Driving Simulator. I cant seam to figure out whats wrong with my code that causes it freeze up when saving the data to the SD card. I need to be able to save my data coming in at 10 Hz to the SD card every 60 seconds as a separate TXT file. But my issue right now is that It freezes and I lose the last data file and it freezes randomly. I need to be able to run each test for 15- 20 minutes. I know this is a code issues as I had it working then needed to make a change and then it no longer works. Thanks

#include <SD.h>
#include <SPI.h>
#include <CurieIMU.h>
#include <MadgwickAHRS.h>
File testFile;
Madgwick filter;

float accelScale, gyroScale;

int pinCS = 5;
const int button = 8;
int led = 9;
int f = 0;
int times = 0;
int dataread = 0;


void setup() {
  Serial.begin(9600);
 
  pinMode(pinCS, OUTPUT);
  pinMode(button, INPUT);
  pinMode(led,OUTPUT);

  digitalWrite(button, HIGH);
  
  // start the IMU and filter
  CurieIMU.begin();
  CurieIMU.setGyroRate(25);
  CurieIMU.setAccelerometerRate(25);
  filter.begin(25);

  // Set the accelerometer range to 2G
  CurieIMU.setAccelerometerRange(2);
  // Set the gyroscope range to 250 degrees/second
  CurieIMU.setGyroRange(250);
  
    if (SD.begin()) {
        Serial.println("SD card is ready to use.");
      } else      {
        Serial.println("SD card initialization failed");
        return;   }

  // initialize variables to pace updates to correct rate


}

float convertRawAcceleration(int aRaw) {
  // since we are using 2G range
  // -2g maps to a raw value of -32768
  // +2g maps to a raw value of 32767
  
  float a = (aRaw * 2.0) / 32768.0;
  return a;}


float convertRawGyro(int gRaw) {
  // since we are using 250 degrees/seconds range
  // -250 maps to a raw value of -32768
  // +250 maps to a raw value of 32767
  
  float g = (gRaw * 250.0) / 32768.0;
  return g;
         }

void loop() {
  int aix, aiy, aiz;
  int gix, giy, giz;
  float ax, ay, az;
  float gx, gy, gz;
  float roll, pitch, heading;
  
  



 if(digitalRead(button) == LOW && times == 0)  {                                      //Looks for when to start the timer  
          dataread = 1;                                                                     //Gets you into while loop
          times = 1;                                                                        //Gets the timer ready
          Serial.println("Turn Pitch Roll");
          delay(500);
          }

while(dataread == 1 && f <100) {                                                         //Looks to see if button was pressed and if f is still within file range
    

                      char testFile1[] = "LOOP0000.txt";
                        //  testFile1[4] = (f/1000)%10 + '0';
                        //    testFile1[5] = (f/100)%10 + '0';
                            testFile1[6] = (f/10)%10 + '0';
                            testFile1[7] = f%10 + '0';

                            testFile = SD.open(testFile1, FILE_WRITE);

                
         while(dataread ==1 && times >= 1 &&  times <= 600){   
  // check if it's time to read data and update the filter
                              if (f<=99){ digitalWrite(led,HIGH); }                       //Used to make light blink when files are still available
                              if (f>100){digitalWrite(led,LOW); dataread =0;}                          //Used to stop blinking light when out of files
                                                                               
    // read raw data from CurieIMU
    CurieIMU.readMotionSensor(aix, aiy, aiz, gix, giy, giz);



    // convert from raw data to gravity and degrees/second units
    ax = convertRawAcceleration(aix);
    ay = convertRawAcceleration(aiy);
    az = convertRawAcceleration(aiz);
    gx = convertRawGyro(gix);
    gy = convertRawGyro(giy);
    gz = convertRawGyro(giz);


    // update the filter, which computes orientation
    filter.updateIMU(gx, gy, gz, ax, ay, az);

    // print the heading, pitch and roll
    roll = filter.getRoll();
    pitch = filter.getPitch();
    heading = filter.getYaw();
    
//    Serial.print("Orientation: ");
    Serial.print(heading);
    Serial.print(" ");
    Serial.print(pitch);
    Serial.print(" ");
    Serial.println(roll);
    
      testFile.println("");                                         //prints the gyroscope data to the open file
      testFile.print(times);
      testFile.print("\t");
      testFile.print(heading);                      //roll (Turning head left/right) yaw
      testFile.print("\t");
      testFile.print(pitch);                     //heading (Ear to shoulder) roll
      testFile.print("\t");
      testFile.print(roll);                    //pitch (Knodding) pitch
      
//

          if(digitalRead(button) == LOW && times > 0){           //Looks for when the button is pressed again to see if it needs to stop. Also resets time and breaks loop
                                      dataread = 0;                                       //For breaking loop
//                                      Serial.println("Data collected breaking loop");
                                      digitalWrite(led,LOW);
                                      Serial.println(f);
                                      times = 0;
                                      delay(200);
                                      }
                  
                  delay(50);
                  digitalWrite(led,LOW);                                                  //For blinking lights
                  delay(50);
                  times = times + 1;                                                      //increases time
              
              }
              testFile.close();  
              f = f+1;
              
              Serial.println("Here I am");
              Serial.println(f);
    if(dataread > 0){times = 1;}
     
    if(dataread == 0){times = 0;}                                                         //Makes sure dataread is right value for being in or out of loop
    
    }
                                                                                    
// Serial.println("outside of loop");    
 delay(1);    
Serial.println("Outside");          //out of loop
 
}

Just have a counting loop writing to the SD card.

See what happens.

That is basically what it is doing. Where do you think it should be added?