BT+SD+Arduino, Arduino fails to send data to SD card

Hello everyone,
I am working with a system with BT and SD card.
The code for BT part and SD part works perfectly when operated seperately. After combining them as below:

void loop()
{
//reads data from BT module until 3 successive readings are obtained (for each loop)
while(Serial3.available()&&(l!=3)){
l=l+1;
a[l]=Serial3.read();
Serial.println(a[l]);
}

if(l==3){

//code for some calculations on the data obtained from BT and gives the result to Sat2_in

//send Sat_in to SD card
sd_store(Sat2_in); // sd print
Serial.println(Sat2_in);
l=0;
}

}

void sd_store(float e){ //float f, float g
n=1; //n added for jumping into while loop for 1st time

while(n==1){
myFile = SD.open("test.txt", FILE_WRITE); // open the file. note that only one file can be open at //a time. //FILE_WRITE: open the file for reading and writing, starting at the end of the file.
if (myFile) {
Serial.print("Writing to test.txt...");
txt1=String(e);
myFile.print(txt1);
myFile.print("\t");
myFile.close();
Serial.println("text done.");
n=-1;
}
if(n==1){
Serial.println("error opening test.txt"); // if the file didn't open, print an error
}
}
}

This code should enable ,in each sec, Arduino of reading data from BT, then doing some calculations on the collected data and finally sending the result of calculation to SD card.

The problem occurs with putting the code of calculation part and SD part in a "if" condition. After the program starts, readings from BT can obtained, calculations could be done, SD card file could be opened, however, result of calculation can not be transmitted to SD card and the program stops. If the "if(l==3)" is removed, the program runs perfectly.

I was thinking if that is because the serial communication for BT disturbs the SPI communication of SD card.

I really need your wise suggestions on this. Thank you in advance.

while(n==1){
      myFile = SD.open("test.txt", FILE_WRITE); // open the file. note that only one file can be open at //a time.   //FILE_WRITE: open the file for reading and writing, starting at the end of the file.
      if (myFile) {
       Serial.print("Writing to test.txt...");
       txt1=String(e);
       myFile.print(txt1);
       myFile.print("\t");
       myFile.close();   
       Serial.println("text done.");
       n=-1;
     }

How many times is this while loop going to iterate? Why would you bastardize a while loop like this?

This is NOTHING to be gained in making the String class convert the float to a string and then wrapping it in an instance that the print() method needs to unwrap. The print() method is perfectly capable of creating a string from a float and writing the string to the file.

I was thinking if that is because the serial communication for BT disturbs the SPI communication of SD card.

I'd suggest thinking along some other lines.

I'd also suggest posting ALL of your code.

Thank you for your reply. Here is the full code. I have removed the part that is unnecessary.

#include <TFT.h> // Arduino LCD library
#include <SPI.h>
#include <SD.h>

//////////BT//////////////////////////////////////
int a[3]={0,0,0};
int l=0;
//////////SD//////////////////////////////////////
int n,m;
String txt1,txt2,txt3;
#define sd_cs 7
#define lcd_cs 10
#define dc 9
#define rst 8

File myFile;
//////////PID////////////////////////////////
const double Kp= 7;
const double Ki=0.3;
const double Kc=0.4;

double err, err_Kp, err_Ki, Sat_in, Sat_out, Sat2_in, Sat3_in;

const double LP_num=0.1;
const double LP_den=0.1;

const double PC_numone=1;
const double PC_numtwo=2;;
const double PC_den=3;

double set_point=50;

//Initialisation..................
double Kc_out=0;
double Integrator_out[2]={0,0};
double Integrator_in[2]={0,0};
double LP_out[2]={0,0};
double LP_in[2]={0,0};
double PC_out[2]={0,0};
double PC_in[2]={0,0};

void setup()
{
Serial.begin(9600);
Serial3.begin(9600);
bt_begin();
sd_begin();
}

void loop()
{
while(Serial3.available()&& (l!=3)){
l=l+1;
a[l]=Serial3.read();
Serial.println(a[l]);
}

if(l==3){

err = set_point-a[3];
err_Kp=errKp;
err_Ki=err
Ki;

//Integrator: y(n)=y(n-1)+x(n-1), [1]=[n-1], [2]=[n]
Integrator_in[2]=Kc_out + err_Ki;
Integrator_out[2]=Integrator_out[1] + Integrator_in[1];
Integrator_out[1]=Integrator_out[2];
Integrator_in[1]=Integrator_in [2];

//addition
Sat_in = err_Kp + Integrator_out[1];
Sat3_in=Sat_in; //used for this 'command'

//SAT
if(Sat_in>5){
Sat_in=5;}
if(Sat_in<0){
Sat_in=0;}

//Discrete Filter: y(n)-0.98y(n-1)=0.01(x(n)+x(n-1)),LP_num=0.01, LP_den=0.98, [1]=[n-1],[2]=[n]
LP_in[2]=Sat_in;
LP_out[2]=LP_denLP_out[1] + LP_num(LP_in[1]+LP_in[2]);
LP_out[1]=LP_out[2];
LP_in[1]=LP_in [2];

//Phase Compensator: y(n)=2.5x(n)-2.32x(n-1)+0.8198y(n-1),[1]=[n-1],[2]=[n], PC_num1=2.5, PC_num2=2.32, PC_den=0.8198
PC_in[2]=LP_out[1];
PC_out[2]=PC_numone
PC_in[2]-PC_numtwoPC_in[1] + PC_denPC_out[1];
PC_out[1]=PC_out[2];
PC_in[1]=PC_in [2];

//After PC
Sat2_in=PC_out[1];
//Feedback loop to Kc
Kc_out=(Sat2_in-Sat3_in)*Kc;

//SAT
if(Sat2_in>5){
Sat2_in=5;}
if(Sat2_in<0){
Sat2_in=0;}

Serial.println(Sat2_in);
sd_store(Sat2_in);
l=0;
}
}
}

void bt_begin(){

//BTsettings
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void sd_begin(){
Serial.println("Initializing SD card...");
if (!SD.begin(sd_cs)) {
Serial.println("SD connection failed!");
return;
}
Serial.println("SD connection done.");
//FILE_WRITE: open the file for reading and writing, starting at the end of the file.
}
else{
Serial.println("error opening test.txt"); // if the file didn't open, print an error
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void sd_store(double e){ //double f, double g
myFile = SD.open("test.txt", FILE_WRITE); // open the file. note that only one file can be open at a //time. //FILE_WRITE: open the file for reading and writing, starting at the end of the file.
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.print(e);
myFile.print("\t");
myFile.close();
Serial.println("text done.");
}
else{
Serial.println("error opening test.txt"); // if the file didn't open, print an error
}
}

  err = set_point-a[3];

You don't have an a[3]. Indices start at 0, so you have a[0], a[1], and a[2].

You are writing to a[1], a[2], and the non-existent a[3].

Though l starts with 0 outside the loop. It starts with a[1] inside the while loop. Please note that l is added with 1, before " a[l]=Serial3.read();" Therefore, this is not the problem causing program to stop.

Though l starts with 0 outside the loop. It starts with a[1] inside the while loop. Please note that l is added with 1, before " a[l]=Serial3.read();"

Yes, I know that. And that is WRONG!

You are, as a result, writing to a[1], a[2], and the non-existent a[3].

Not to mention that l is a poor choice for a variable name, as it looks too much like a | or a 1, depending on the font being used.

This still does not fix the problem. The program still stops at the step of printing to SD. Do we have another clue about it?

The program still stops at the step of printing to SD.

Have you fixed the known problems yet?

Yes. Luckily, I have. Thank you very much for help!