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.