Skip to main content
added 1 character in body
Source Link
GEPD
  • 131
  • 6

Well, finally someone found where was the problem. In the getFileName() function, I open two instances of the file but I close just one.

Well finally someone found where was the problem. In the getFileName() function, I open two instances of the file but I close just one.

Well, finally someone found where was the problem. In the getFileName() function, I open two instances of the file but I close just one.

Source Link
GEPD
  • 131
  • 6

Well finally someone found where was the problem. In the getFileName() function, I open two instances of the file but I close just one.

here is the answer:

#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());
}

void 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();    
  }
}

void getFileNameSD (){

  char folder[] = "/ACC";
  File myDir = SD.open(folder);
  
  memset(fullPath, 0, sizeof(fullPath));

  if (myDir){
    myFile = myDir.openNextFile();
    
    if(myFile){
      strcpy(fullPath,folder);
      strcat(fullPath,"/");
      strcat(fullPath,myFile.name());
      myFile.close();
      myDir.close();
    }else{
      // Handle file not found
      myDir.close();
      fullPath[0] = '0';
    }
  }else{
    // Handle directory not opened
    fullPath[0] = '0';
  }
}