SD card writing problems

Hello guys, I'm using Arduino mega 2560 for some calculation and communications, but my data can't save successfully every time.
Take this as an example :

#include <TinyGPS++.h>
#include <stdlib.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <JY901.h>
#include <SPI.h>
#include <SD.h>

File myfile;

float steering_angle1,steering_angle2,steering_angle,camber_angle1,camber_angle2,camber_angle;

byte buffer1[7],buffer2[7];

unsigned int t0,t1,location1,location2;

float ax1,ay1,az1,vx1,vy1,vz1,vx11,vy11,vz11,v,v_final,t,FRPM,RRPM,velocitykmhr;

int x=0,y=0,i=0,j=0;

float DSFE,DSRE,DSRFE,DSRRE,BSFE,BSRE,BSRFE,BSRRE; //Driving Slip Front Rate Error

unsigned int Lrfw,Lrrw;
unsigned short fuzzyf1,fuzzyf2,fuzzyr1,fuzzyr2,fuzzy_outputf=0,fuzzy_outputr=0;
unsigned short fuzzy_tablef[5][5]={{1,1,1,2,3},{1,1,2,3,3},{1,2,3,3,4},{2,3,3,4,4},{3,3,4,4,5}};
unsigned short fuzzy_tabler[5][5]={{1,1,1,2,3},{1,1,2,3,3},{1,2,3,3,4},{2,3,3,4,4},{3,3,4,4,5}};

float FS,RS,FRS,RRS,FS1,FS2,FS3,FS4,RS1,RS2,RS3,RS4,FRS1,FRS2,RRS1,RRS2;

TinyGPSPlus gps;

void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);//JY901
  Serial2.begin(9600);//Front
  Serial3.begin(9600);//Rear
  JY901.StartIIC();
  pinMode(53,OUTPUT); 
  
  if (SD.begin()) {
    Serial.println("initialization done!");
    }
  else{
  Serial.println("initialization failed.");
  return;
  }
}

void loop() {
t0=millis();

/////接收JY901(camber angle & steering angle)/////
  JY901.GetAcc();
  JY901.GetGyro();
  JY901.GetAngle();
  JY901.GetLonLat();
  JY901.GetGPSV();  
  JY901.CopeSerialData(Serial1.read());
  ax1=JY901.stcAcc.a[0];
  ax1=(ax1/32768*16)*9.81;
  ay1=JY901.stcAcc.a[1];
  ay1=(ay1/32768*16)*9.81;   
  az1=JY901.stcAcc.a[2];
  az1=(az1/32768*16)*9.81;      

  steering_angle1=JY901.stcAngle.Angle[2];///z axis = yaw = steering angle
  steering_angle1=steering_angle1/32768*180;
  
  camber_angle=JY901.stcAngle.Angle[1];/// y axis = roll = camber angle
  camber_angle=camber_angle/32768*180;
  if(camber_angle<0)
  camber_angle=-camber_angle;

  velocitykmhr=((float)JY901.stcGPSV.lGPSVelocity)/1000;///
  location1=((JY901.stcLonLat.lLon) /1e7);
  location2=((JY901.stcLonLat.lLat) /1e7);

  steering_angle2=JY901.stcAngle.Angle[2];
  steering_angle2=steering_angle2/32768*180;

  steering_angle=steering_angle2-steering_angle1;
  if(steering_angle<0)
  steering_angle=-steering_angle;
    

if(Serial3.available()>0 or Serial2.available()>0 ){
 
Serial2.readBytes(buffer2,7);
Serial3.readBytes(buffer1,7);


if(buffer1[0]==0xEE and buffer2[0]==0xEE){

RRS1=buffer1[1]<<8;
RRS2=buffer1[2]&0xFF;
RRS=RRS1+RRS2;
         
RS1=buffer1[3]<<8;
RS2=buffer1[4]&0xFF;
RS=RS1+RS2;

FRS1=buffer2[1]<<8;
FRS2=buffer2[2]&0xFF;
FRS=FRS1+FRS2;

FS1=buffer2[3]<<8;
FS2=buffer2[4]&0xFF;
FS=FS1+FS2;  


/////速度計算/////
t1=millis();
t=(t1-t0)*0.000000278;
vx1=velocitykmhr+ax1*t;
vx11=vx1*vx1;
vy1=velocitykmhr+ay1*t;
vy11=vy1*vy1;
vz1=velocitykmhr+az1*t;
vz11=vz1*vz1;
v=(sqrt((vx11+vy11+vz11)/3));///加速規的速度
if((abs(v-velocitykmhr)/100)>0.01){
v_final=velocitykmhr;       /////若加速規的速度>GPS速度 則以GPS為主
}
if((abs(v-velocitykmhr)/100)<0.01){
v_final=(v+velocitykmhr)/2; /////若加速規的速度<GPS速度 則以兩者平均為主
}

/////ADC to RPM/////

FRPM=(FS/32767)*300;
RRPM=(RS/32767)*300;

/////Slip Ratio計算/////

Lrfw=1.67*3.6*0.4808*FRPM*3.14/60;  //(km/hr)
Lrrw=1.67*3.6*0.4862*RRPM*3.14/60;
DSFE=(Lrfw-v_final*(1.67+0.4862*sin(camber_angle)*sin(steering_angle)))/Lrfw-0.13;
DSRE=(Lrrw-v_final*(1.67+0.5027*sin(camber_angle)*sin(steering_angle)))/Lrrw-0.13;

BSFE=(Lrfw-v_final*(1.67+0.4862*sin(camber_angle)*sin(steering_angle)))/(v_final*(1.67+0.4862*sin(camber_angle)*sin(steering_angle)))-0.13;
BSRE=(Lrrw-v_final*(1.67+0.4862*sin(camber_angle)*sin(steering_angle)))/(v_final*(1.67+0.4862*sin(camber_angle)*sin(steering_angle)))-0.13;

DSRFE=(DSFE-0.13)*100/0.13;DSRRE=(DSRE-0.13)*100/0.13;
BSRFE=(BSFE-0.13)*100/0.13;BSRRE=(BSFE-0.13)*100/0.13;

}
else{
if(x<7;x++)
{
buffer1[x] = 0;
}
if(y<7;y++)
{
buffer2[y] = 0;
}
}
  /////comunicate control test/////Front 
  if(i==0 and j==0){
fuzzy_outputf=i;
fuzzy_outputr=j;
delay(5000);
i=i+1;
j=j+1;
}
else if(i==1 and j==1){
fuzzy_outputf=i;
fuzzy_outputr=j;
delay(15000);
i=i+1;
j=j+1;
}
else if(i==2 and j==2){
fuzzy_outputf=i;
fuzzy_outputr=j;
delay(5000);
i=i+1;
j=j+1;
}
else if(i==3 and j==3){
fuzzy_outputf=i;
fuzzy_outputr=j;
delay(5000);
i=i+1;
j=j+1;
}
else if(i==4 and j==4){
fuzzy_outputf=i;
fuzzy_outputr=j;
delay(5000);
i=0;
j=0;
}
Serial.print("fuzzy:");
Serial.print(fuzzy_outputf);
Serial.print(",");
Serial.println(fuzzy_outputr);

Serial2.write(fuzzy_outputf);
Serial3.write(fuzzy_outputr); 

  myfile = SD.open("ct.txt", FILE_WRITE);

  myfile.print(location1);
  myfile.print(",");
  myfile.print(location2);
  myfile.print(",");
  
  myfile.print(RS);
  myfile.print(",");
  myfile.print(RRS);
  myfile.print(",");
  myfile.print(RRPM);
  myfile.print(",");
  myfile.print(FRS);
  myfile.print(",");
  myfile.print(FS);
  myfile.print(",");
  myfile.print(FRPM);
  myfile.print(",");

  myfile.print(v);
  myfile.print(",");
  myfile.print(velocitykmhr);
  myfile.print(",");
  myfile.print(v_final);
  myfile.print(",");

  
  myfile.print(steering_angle);
  myfile.print(",");
  myfile.print(camber_angle);

  myfile.print(fuzzy_outputf);
  myfile.print(",");
  myfile.println(fuzzy_outputr);
  
}
  myfile.close();
}

It suppose to save my data like :
location1,location2,RS,RRS,RRPM,FRS,FS,FRPM,v,velocitykmhr,v_final,steering_angle,camber_angle,fuzzy_outputf,fuzzy_outputr
.
.
.
.
as long as I'm activating it.
But my SD card's file has only


and I'm activating it like almost 1 minute which I suppose there will be more data.

Is it because I'm using too much dynamic memories?
Or are there any other reason?

Thanks for your reply!!

Your program is working just fine for the way you have the program written. Every time you open the file, write a record and close the file you get ONE record. And your print shows you are getting ONE record.
Paul

Paul,
#define FILE_WRITE (O_READ | O_WRITE | O_CREAT | O_APPEND)

So it goes like open write close, open write close ...
But why did it stop doing that?
Is it can't open? Or is it can't write?

Thanks for your reply!

I'm sorry I'm not sure what you mean.
Are you implying I should delete O_APPEND?

Thanks for your reply!

You are not paying attention. EVERY TIME YOU CLOSE AND OPEN THE FILE, YOU START OVER AGAIN!
Paul

So it's like, coding, open file, write, close file, coding, open file, write, close file.....right?
But if the code can record for the fist time, why can't it just record it over and over?

Thanks for your reply Paul!!

no. FILE_WRITE is append

Sorry, I'm confused.
Do you mean my code of file writing is correct?
Cause you said FILE_WRITE is append, which means my data will be like
coding, open file, writing data, close file.
coding, open file, writing data, close file.
coding, open file, writing data, close file.
.
.
.
and not overwriting, right?

Thanks for your reply!!

Are you seeing more lines of data in the Serial output than is being saved in the file?

What is connected to Serial2 and Serial3? Reading from both of those serial inputs when EITHER is available will slow things down, because readBytes waits for 1000mS looking for input data before it times out.

You might also want to move the myfile.close to immediately after the last myfile.println. In your sketch, all the code to open myfile and write to it is inside an if statement, but myfile.close is after that if statement, meaning you are repeatedly attempting to close a file that may or may not be open.

There are two MCU connected to Serial2 and 3, and it will both activate at the same time.
I think the position of myfile.close() is maybe the problem, I'll try it later.

Thanks a lot, David!!