mamech
Full Member level 3
hello everyone
I have a weird problem. I am trying to interface an Arduino Uno with an SD card. The code is made to produce a new file every timer interval. The logic seemed simple to me, but the results are not logical at all!
this is the code:
This code writes only one file with incrementing numbers in it, and when the part after delay function begins , the serial monitor gives several times :
error opening file
error opening file
error opening file
can any one tells me what is the problem in this code? I made numerous modifications nut no logical response.
I have a weird problem. I am trying to interface an Arduino Uno with an SD card. The code is made to produce a new file every timer interval. The logic seemed simple to me, but the results are not logical at all!
this is the code:
Code:
#include <SPI.h> //SPI communication protocol library
#include <SD.h> //SD card interface library
double File_name_counter=1;
String Fixed_File_Name = "ex";
String Variable_File_Name = "";
String Extension = ".txt";
char temp[9] = "ex.txt";
int i=0;
int soft_counter =0;
void setup()
{
// Open serial communications:
Serial.begin(9600);
Serial.print("Initializing SD card...");
pinMode(10, OUTPUT); // Comment from Arduino page: This can be the hardware SS pin - pin 10 (on most Arduino boards) or
// pin 53 (on the Mega) - or another pin specified in the call to SD.begin(). Note that
// even if you don't use the hardware SS pin, it must be left as an output or the SD library won't work.
if (!SD.begin(4)) // begin SD proces, and make the SS/CS pin to be on pin 4, and check if it returns true or false
{
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
delay(3000);
}
void loop()
{
File myFile;
myFile = SD.open(temp, FILE_WRITE);
if (myFile) {
Serial.println("Writing to the file");
i++;
myFile.println(i);
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening file");
}
delay(1000); //delay
soft_counter++;
if(soft_counter>30)
{
Variable_File_Name= Fixed_File_Name+File_name_counter+Extension; // construct new file name
Variable_File_Name.toCharArray(temp,9) ; // convert file name from String to CharArray
File_name_counter++;
soft_counter=0;
}
}
This code writes only one file with incrementing numbers in it, and when the part after delay function begins , the serial monitor gives several times :
error opening file
error opening file
error opening file
can any one tells me what is the problem in this code? I made numerous modifications nut no logical response.