Hello, I have a strange problem when using an SD card.
My project is a data logger with a HMI, to which I am logging data to an SD card which is fine.
In the startup I am pulling calibration values and sensor names through from a separate file on the SD which works okay, I have enabled the user to change the calibration values and sensor names which deletes the calibration file on the SD and then creates a new one populating it with the new values.
The problem is.. most of the time the calibration file is repopulated correctly and the values pull through okay on startup, but 'sometimes' the file is just empty and doesn't pull through any values. I have tried for hours to determine a trend to why this occurs but it just seems completely random..
The function used to clear the existing file and populate with new data is as follows:
void SDClear() {
// Wipe calibration file
SD.remove(Defaults);
// Create new calibration file
myFile = SD.open(Defaults, FILE_WRITE);
myFile.close();
//Populate caliration file with new values
myFile = SD.open(Defaults, FILE_WRITE);
for (int i = 0; i < 4; i++) {
myFile.print(sensorNames[i]);
myFile.print(",");
}
for (int i = 0; i < 4; i++) {
myFile.print(sensorScaling[i]);
myFile.print( ",");
}
for (int i = 0; i < 4; i++) {
myFile.print(sensorOffset[i]);
myFile.print(",");
}
myFile.println("");
myFile.close(); // close the file
}
The setup section of code that initially pulls the information through is as follows:
myFile = SD.open(Defaults, FILE_READ);
char inByte = 0;
char oot;
for (int u = 0; u < 4; u++) { //Sensor name read from DEFAULTS.csv file
oot = "";
sensorNames[u] = "";
while (String(oot) != ",") {
inByte = (char)myFile.read();
oot = (char)inByte;
SdError++;
if (SdError > 100) {
Serial.print(F("t0.txt=\""));
Serial.print(F("SD 'DEFAULTS.csv' file failed. Remove SD card and reset manually."));
endLine();
}
else {
}
if (isAlphaNumeric(oot) == true) {
sensorNames[u] += oot;
}
}
}
for (int u = 0; u < 4; u++) {
oot = "";
sensorScaling[u] = "";
while (String(oot) != ",") {
inByte = (char)myFile.read();
oot = (char)inByte;
if (isAlphaNumeric(oot) == true) {
sensorScaling[u] += oot;
}
}
}
for (int u = 0; u < 4; u++) {
oot = "";
sensorOffset[u] = "";
while (String(oot) != ",") {
inByte = (char)myFile.read();
oot = (char)inByte;
Serial.print("inByte");
Serial.println(inByte);
if (isAlphaNumeric(oot) == true) {
sensorOffset[u] += oot;
}
}
}
myFile.close();
Has anyone had a similar problem?
I can send the full code however I don't imagine anyone would like to sieve through hundreds of lines of code
Cheers.