I am writing some code that is writing to several files on an SD card. The early version of the code works perfectly, reading from a file, however I have been "improving" the design and need to use some additional files by reading/writing and creating/deleting folders.
My problem is that the additional code checks that the file exists (which it agrees that it does), but at the "while(File.available())" bit it doesn't execute the code within.
Are there any quirks that anyone knows of, like timing issues, multiple file handling etc?
char CharIn;
String StringIn = "";
char* Folder = "TIMEUSED/";
char CurrentDate[] = {"DAYCHECK.TXT"};
int UserCounter;
String UserTimeFile = Folder;
for (int n = 2; n < 10; n++)
{
char CharRead = CurrentRFIDTag.charAt(n);
UserTimeFile += CharRead;
}
UserTimeFile += ".TXT";
char Filename[(UserTimeFile.length()+1)];
UserTimeFile.toCharArray(Filename, (UserTimeFile.length()+1));
Serial.print("Filename = ");
for(int m = 0; m < (UserTimeFile.length()+1); m++)
{
Serial.print(String(Filename[m]));
}
Serial.println("");
Serial.println(String(CurrentDate)+" is being opened");
if (SD.exists(CurrentDate))
{
Serial.println("The File Exists");
}
DateStampFile = SD.open(CurrentDate, FILE_WRITE); //Try to open the chosen file
if (DateStampFile) //If the file exists then
{
Serial.println("READING DAY INFO");
StringIn = "";
while(DateStampFile.available())//StringIn.length() < 4)//ConfigFile.available()) //Read from the file until there's nothing else in it:
{
Serial.println(F("While Available"));
CharIn = DateStampFile.read(); //Write the next charater from the file to 'CharIn'
StringIn += CharIn; //then add it to 'StringIn'
};
Serial.print(String(CharIn));
Serial.println(String(StringIn)+" is the value from "+String(CurrentDate));
Serial.println(F("2"));
DayNumber = StringIn; //Call the UserData Routine to ascertain the User's Information
DayNumberInt = DayNumber.toInt();
DayNumberIntCheck = ((dayOfWeekRT100)+monthRT);
Serial.println("Day is "+String(dayOfWeekRT)+" and Month is "+String(monthRT)); //Clear 'StringIn'
Serial.println("The allocated Day number for today is: "+String(DayNumberIntCheck));
StringIn = "";
DateStampFile.close();
// delay(10);
Serial.println("Day Number Int = "+String(DayNumberInt)+" : Day Number Int Check = "+String(DayNumberIntCheck));
if (DayNumberInt != DayNumberIntCheck)
{
Serial.println(F("Change of day detected"));
SD.rmdir(Folder);
delay(10);
DayNumberInt = DayNumberIntCheck;
SD.mkdir(Folder);
// delay(10);
UserCounterFile = SD.open(Filename, FILE_WRITE);
// delay(10);
UserCounterFile.println("0");
// delay(10);
UserCounterFile.close();
// delay(10);
SD.remove(CurrentDate);
// delay(10);
DateStampFile = SD.open(CurrentDate, FILE_WRITE);
// delay(10);
DateStampFile.println(DayNumberInt);
UserCounter++;
}
else
{
Serial.println(F("3"));
Serial.println(String(Filename)+" is being opened");
//This is the bit where the number in the file needs to be incremented
if (SD.exists(Filename))
{
UserCounterFile = SD.open(Filename, FILE_WRITE);
Serial.println(F("The File Exists"));
delay(10);
}
else
{
UserCounterFile = SD.open(Filename, FILE_WRITE);
Serial.println(F("Nope, not there.........>"));
UserCounterFile.close();
delay(10);
UserCounterFile = SD.open(Filename, FILE_WRITE);
delay(10);
}
StringIn = "";
Serial.println(F("4"));
while (UserCounterFile.available())//StringIn.length() < 3) //Read from the file until there's nothing else in it:
{
CharIn = UserCounterFile.read();
StringIn += CharIn;
};
Serial.println(F("******************************"));
Serial.println(CharIn);
Serial.println("String Length = "+String(StringIn.length()));
Serial.println("StringIn = "+String(StringIn));
UserCounter = StringIn.toInt(); //Call the UserData Routine to ascertain the User's Information
UserCounter++;
RunTimeInt = UserCounter; //Clear 'StringIn'
Serial.println("The allocated minutes for today is: "+String(UserCounter));
Serial.println("The allocated minutes for today is: "+String(RunTimeInt));
Serial.println("closed 5 (1)");
UserCounterFile.close();
delay(10);
Serial.println("closed 5 (2)");
SD.remove(Filename);
delay(10);
Serial.println("open");
UserCounterFile = SD.open(Filename, FILE_WRITE);
delay(10);
UserCounterFile.println(String(UserCounter));
Serial.println("User Counter has been updated to :"+String(UserCounter));
delay(10);
UserCounterFile.close();
}
}
else //Else
{
Serial.println("It is getting down to here, check it out");
RunTimeInt = 0;
UserCounterFile.println(String(RunTimeInt));
Serial.println("closed (last)");
UserCounterFile.close();
}
return; //Return
}
Obviously not all the code, but there is a lot of it.
Obviously not all the code, but there is a lot of it.
Obviously (to me, if not to you), you are not going to get help with snippets of code.
Which Arduino are you running this code on? I'd guess that you are running out of memory, with all the literal strings uselessly occupying SRAM and pissing away memory on Strings.
I'm running on a mega 2560. The majority of the String code has only been put in there for me to see what is happening in the code (Whilst it isn't working) and is taken out once it is up and running. I didn't post the whole lot as it is over 1700 lines long, but I will try stripping out printing Strings to cut down on memory usage.
Current size is:
Global variables use 5,302 bytes (64%) of dynamic memory, leaving 2,890 bytes for local variables. Maximum is 8,192 bytes.
Is this likely to be a memory issue?
I just am not understanding how it acknowledges that the file is there, yet isn't able to read it.
I just am not understanding how it acknowledges that the file is there, yet isn't able to read it.
I don't know which file you are talking about. Stuffing data into a String, one character at a time, and then converting the String to a string is just a huge waste of resources. Writing one character at a time into the string is JUST AS EASY.
Using Strings so that you can have one Serial.print() statement instead of two is a huge waste of resources. How many times do you need to type that code?
Serial.println("The allocated minutes for today is: "+String(UserCounter));
Is not really that much easier to type than
Serial.print("The allocated minutes for today is: "); Serial.println(UserCounter);