I'm doing a program who get a line from the SD and shows it, this function is called getData()
FULL CODE:
#include <SD.h>
#include <MemoryFree.h>
File myFile;
int stringIndex = 0;
int cursorPosition = 0;
char inputString [25];
char inputChar;
boolean endOfLine;
char fullPath[11];
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));
getFileNameSD();
myFile = SD.open(fullPath);
if(myFile){
myFile.seek(cursorPosition);
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;
}
void getFileNameSD (){
char folder[] = "/ACC";
checkFolderSD(folder);
myFile = SD.open(folder);
myFile = myFile.openNextFile();
if(!myFile) myFile.rewindDirectory();
if(myFile){
strcpy(fullPath,folder);
strcat(fullPath,"/");
strcat(fullPath,myFile.name());
myFile.close();
}
}
I don't have problem with this function(getDataSD). To get the name of the file, I've created another function who looks for a file in a specific folder, this function is called getFileNameSD()
I'm having problem with this last function, it's 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");
Serial monitor
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());
Serial monitor
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/
I know I'm doing something wrong with getFileName() but I don't know what is, can anybody help/explain me?
fullPath
?