Hi, I'm doing a program who get a line from the SD and shows it, this function is called getData()
String getDataSD(){
memset(inputString, 0, sizeof(inputString));
myFile = SD.open(getFileNameSD());
//cursorPosition = getIndexFileSD("INDX");
myFile.seek(cursorPosition);
if(myFile){
endOfLine = false;
while(myFile.available() && endOfLine == false){
inputChar = myFile.read();
if(inputChar == '*'){
while(inputChar != '\n'){
inputChar = myFile.read();
}
}else{
cursorPosition = myFile.position()-1;
//saveIndexFileSD(cursorPosition,"INDX");
if(inputChar == 10 || inputChar == ' ') inputChar = myFile.read();
while(inputChar != '\n'){
if(stringIndex < 25){
inputString[stringIndex] = inputChar;
stringIndex++;
inputChar = myFile.read();
}
}
inputString[stringIndex-1] = '\0';
stringIndex = 0;
endOfLine = true;
}
}
myFile.close();
}
return inputString;
}
I don't have problem with this function. To get the name of the file, I've created another function who looks for it in a folder, this function is called getFileNameSD()
char* getFileNameSD (){
char folder[] = "/ACC";
myFile = SD.open(folder);
myFile = myFile.openNextFile();
if(!myFile) myFile.rewindDirectory();
if(myFile){
char fullPath[sizeof(folder)+ sizeof(myFile.name())];
strcpy(fullPath,folder);
strcat(fullPath,"/");
strcat(fullPath,myFile.name());
myFile.close();
return fullPath;
}
}
I'm having problem with this last function, it is not releasing or overwriting some data, so every time it's called, it is stored new data in memory
I've included the memory Free library to see where is the problem. When I call the getDataSD and write the file manually I dont have problem
myFile = SD.open("/ACC/11001");
freeMemory()=6592
/-27/-26/-56/
freeMemory()=6592
/-27/-26/-56/
freeMemory()=6592
/-27/-26/-56/
freeMemory()=6592
/-27/-26/-56/
freeMemory()=6592
/-27/-26/-56/
freeMemory()=6592
/-27/-26/-56/
the memory always is the same.
but not when I call the getFileNameSD()
myFile = SD.open(getFileNameSD());
freeMemory()=6571
/-27/-26/-56/
freeMemory()=6540
/-27/-26/-56/
freeMemory()=6509
/-27/-26/-56/
freeMemory()=6478
/-27/-26/-56/
freeMemory()=6447
/-27/-26/-56/
freeMemory()=6416
/-27/-26/-56/
I know I'm doing something wrong with getFileName() but I don't know what is, can anybody help/explain me?
FULL CODE:
#include <SD.h>
#include <MemoryFree.h>
File myFile;
int stringIndex = 0;
int cursorPosition = 0;
char inputString [25];
char inputChar;
boolean endOfLine;
void setup()
{
Serial.begin(9600);
Serial.print("Initializing SD card.");
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
}
void loop(){
Serial.println(getDataSD());
Serial.print("freeMemory()=");
Serial.println(freeMemory());
}
String getDataSD(){
memset(inputString, 0, sizeof(inputString));
myFile = SD.open(getFileNameSD());
myFile.seek(cursorPosition);
if(myFile){
endOfLine = false;
while(myFile.available() && endOfLine == false){
inputChar = myFile.read();
if(inputChar == '*'){
while(inputChar != '\n'){
inputChar = myFile.read();
}
}else{
cursorPosition = myFile.position()-1;
if(inputChar == 10 || inputChar == ' ') inputChar = myFile.read();
while(inputChar != '\n'){
if(stringIndex < 25){
inputString[stringIndex] = inputChar;
stringIndex++;
inputChar = myFile.read();
}
}
inputString[stringIndex-1] = '\0';
stringIndex = 0;
endOfLine = true;
}
}
myFile.close();
}
return inputString;
}
char* getFileNameSD (){
char folder[] = "/ACC";
myFile = SD.open(folder);
myFile = myFile.openNextFile();
if(!myFile) myFile.rewindDirectory();
if(myFile){
char fullPath[sizeof(folder)+ sizeof(myFile.name())];
strcpy(fullPath,folder);
strcat(fullPath,"/");
strcat(fullPath,myFile.name());
myFile.close();
return fullPath;
}
}