Hi, I'm creating a logger and have data from a sensor going to a file on an SD card. It works great except the file keeps growing which I don't want.
How do I remove the last lines from the file and keep it at 336 records? It should grow until it hit 336 records than the first record would be deleted and a new one added and so on.
I tried adding an ID to each line so I can find the last line but nothing I do works. I'm surprised there isn't much info on removing or finding data.
This is how the data looks on the SD card.
[grW1=153.50-1]
[grW1=152.40-2]
[grW1=152.95-3]
[grW1=153.50-4]
[grW1=152.95-5]
[grW1=152.95-6]
[grW1=152.95-7]
[grW1=152.95-8]
[grW1=152.95-9]
[grW1=152.40-10]
[grW1=152.40-11]
[grW1=153.50-12]
[grW1=152.95-13]
[grW1=152.40-14]
[grW1=152.40-15]
[grW1=152.95-16]
[grW1=152.40-17]
[grW1=152.40-18]
The grW1 is a variable name that each value goes into when I load the file, this works good.
The number after the - is the ID. When ID 337 gets added ID 1 should be removed. When ID 338 gets added ID 2 should be removed, etc...
This is the code for writing the data.
void writeGraph()
{
// Delete the old One
// SD.remove("settings.txt");
// Create new one
myFile = sd.open("graph.txt", FILE_WRITE);
// writing in the file works just like regular print()/println() function
myFile.print("[");
myFile.print("grW1=");
myFile.print(grW1,2);
myFile.print("-");
myFile.print(grWid);
myFile.println("]");
// close the file:
myFile.close();
}
This is the code for reading the data.
void readGraph()
{
char character;
String settingName;
String settingValue;
String grHistID;
myFile = sd.open("graph.txt");
if (myFile)
{
while (myFile.available())
{
character = myFile.read();
while((myFile.available()) && (character != '['))
{
character = myFile.read();
}
character = myFile.read();
while((myFile.available()) && (character != '='))
{
settingName = settingName + character;
character = myFile.read();
}
character = myFile.read();
while((myFile.available()) && (character != '-'))
{
settingValue = settingValue + character;
// settingName = settingName + character;
character = myFile.read();
}
character = myFile.read();
while((myFile.available()) && (character != ']'))
{
grHistID = grHistID + character;
character = myFile.read();
}
if(character == ']')
{
// Apply the value to the parameter
applyGraph(settingName,settingValue);
// Reset Strings
settingName = "";
settingValue = "";
}
}
// close the file:
myFile.close();
}
else
{
// if the file didn't open, print an error:
Serial.println("error opening settings.txt");
}
}
// converting string to Float
float toFloat(String settingValue)
{
char floatbuf[settingValue.length()+1];
settingValue.toCharArray(floatbuf, sizeof(floatbuf));
float f = atof(floatbuf);
return f;
}
void applyGraph(String settingName, String settingValue)
{
if(settingName == "grW1") {grW1=toFloat(settingValue);}
}
Any help would be appreciated, thanks.