Rocket Stabilizer does not keep records in SD and seems slow

Hey guys,

thanks for taking your time to read about my problem.

I am involved in a project about a rocket Stabilizer and data log, that we need to characterize. To do so, I wanted to measure the acceleration. So what I am working with right now is a Arduino NANO, a SD-Card-Reader , a 6D IMU (MPU6050) and 4 Micro Servos (SG90). So far I managed to read the RAW-values of the MPU6050 and i want store them to the SD-card.I managed to map RAW-values in to 0-180 degrees for the servos and works well without save the data in the SD-card .

When i put all together i see the problem. It looks like my arduino get slowdown and my serial monitor seems to be lazy. Other problem is that my data file is created but is empty .

My Code :

#include <Wire.h>
#include <MPU6050.h>
#include <SdFat.h>
#include <Servo.h>
//SD card read/write
//MOSI - pin 11
//MISO - pin 12
//CLK  - pin 13
//CS   - pin 4
const int chipSelect = 4;
Servo sg901; //Servo x
Servo sg902; //Servo reverse x
Servo sg903; //Servo y
Servo sg904; //Servo reverse y

//atach servos in there pines
int servo_pin1 = 5;
int servo_pin2 = 6;
int servo_pin3 = 7;
int servo_pin4 = 8;
MPU6050 sensor ;

//active and separate data from the MPU6050
int16_t ax, ay, az;
int16_t gx, gy, gz;
SdFat sd;
SdFile myFile;

int count = 0;
int idx = 0;
void setup ( ) {

  sg901.attach ( servo_pin1 );
  sg902.attach ( servo_pin2 );
  sg903.attach ( servo_pin3 );
  sg904.attach ( servo_pin4 );

  // join I2C bus (I2Cdev library doesn't do this automatically)
  Fastwire::setup(400, true);
  // initialize serial communication
  // (115200 chosen because it works as well at 8MHz as it does at 16MHz)
  //Test the sensor, if it's not atach, will not move the servos
  Wire.begin ( );
  Serial.begin  (115200);
  // initialize device
  Serial.println  ( "Initializing the sensor" );
  sensor.initialize ( );
  // Initialize SdFat or print a detailed error message and halt
  // Use half speed like the native library.
  if (!sd.begin(chipSelect, SPI_HALF_SPEED))
  Serial.println (sensor.testConnection ( ) ? "Successfully Connected" : "Connection failed");
  delay (1000);

  Serial.println ( "Taking Values from the sensor" );
  delay (1000);
  // dynamic create a file
  if (count == 0)
    // dynamic filename
    char filename[11];
    sprintf(filename, "acc_%d.txt", idx);
    Serial.print("filename :");

    // open the file for write at end like the Native SD library
    if (!, O_RDWR | O_CREAT | O_AT_END))
      sd.errorHalt("opening att_0.txt for write failed");
    Serial.println ("File opened OK");
    myFile.close ();
  sg901.write (90);
  sg902.write (90);
  sg903.write (90);
  sg904.write (90);
  delay (5000);

void loop ( ) {

  sensor.getMotion6 (&ax, &ay, &az, &gx, &gy, &gz);
  // write data to file"acc_%d.txt", FILE_WRITE);
  myFile.print(count); myFile.print(", "); 
  myFile.print(ax); myFile.print(", ");
  myFile.print(ay); myFile.print(", ");
  delay (10);
  Serial.print(", ax: "); Serial.print(ax);
  Serial.print(", ay: "); Serial.print(ay); 
  Serial.print(", az: "); Serial.println(az); 
  count ++  ;
  // close file when every 1,000 records
  if (count >1000)
    count = -1;
    idx++  ;

  ax = map (ax, 8200, -8200, 180, 0) ; //Map the acelerometer x to convert in degrees
  ay = map (ay, 8200, -8200, 180, 0) ;//Map the acelerometer y to convert in degrees

  sg901.write (ax);
  ax = map (ax, 0, 180, 180, 0) ; //Remap the acelerometer x to convert in degrees in reverse servo
  sg902.write (ax);

  sg903.write (ay);
  ay = map (ay, 0, 180, 180, 0) ;//Remap the acelerometer y to convert in degrees in reverse servo
  sg904.write (ay);

  delay (10); //adjust the delay for rapid response


Do you have the same problem? Have you already solved it?
Please give me some advice.
Thanks for your time.

delay (10); //adjust the delay for rapid response

You ask why the program is slow and then I find this line which basically tells your program to do nothing for 10ms on every iteration of loop. If my program was going slow then my first bet would be to take out all the time wasting stuff like this.

How would you feel if you got a job digging a ditch and the boss said that every time you shovel one shovel full of dirt you have to sit down and rest for 10 minutes. You MUST do this for every shovel full you shovel. Then how would you feel if the same boss turned up at the end of the day saying that you weren't digging fast enough?