I'm recording data values to separate lines of a .txt file. I want to be able to read back these values to and LCD display, choosing which line number to read. How would I split them back into their individual lines, each record is of different lengths, from about 9 to 12 characters. I've heard of using things like a break variable but I dont know how to implement this in the reading code, its easy enough to add to the write code.
Any help would be great!
Down at the lower right of Serial Monitor there's a box that lets you choose to have End Of Line character(s) added.
Most common is newline, ASCII value == 10.
My data looks like this, in case it helps
- 52.36cm/s
- 987.33cm/s
- 9.87cm/s
I know the units dont need to be saved onto here but i would like them there for when i go through the text files on my pc.
I'm recording data values to separate lines of a .txt file.
How? Your code doesn't show that.
This is the write code (excluding a break variable).
void saveData()
{
speedFile = SD.open("speeds.txt", FILE_WRITE);
if (speedFile)
{
Serial.print("Writing to speeds.txt...");
speedFile.print(writeNo);
speedFile.print(". ");
speedFile.print(calcSpeed);
speedFile.println("cm/s");
speedFile.close();
Serial.println("done.");
writeNo++;
}
else
{
Serial.println("Save error.");
lcd.setCursor(0, 0);
lcd.print("Save error.");
}
}
Its more how to read this data back I cant work out. I can easily add the break variable into the file writing code if thats how is best to do it, due to the varying string lengths, I dont know how to read the data back and separate it into either separate strings or a string array.
This is the write code (excluding a break variable).
speedFile.print(writeNo);
speedFile.print(". ");
speedFile.print(calcSpeed);
speedFile.println("cm/s");
No, it isn't. You have a break character.
To my knowledge there is no break variable in the code which aids with its reading, ie a "," donates end of record or something like that. How would I read this data back line by line? as the string length varies. My difficulties lie in the reading of rather than recording of the data. I may be referring to it with the wrong name.
println adds a newline character to the end of what's printed.
You could do the same with.
Serial.print( "blahblahblah\n" );
At first I thought you just weren't getting EOL from Serial Monitor.
There is no such thing as a record in a file. The file is a stream of characters. What we typically mean by a record is "all of the characters in the stream up to some specific character". Typically, that character is the carriage return or line feed. Since you are writing carriage returns and line feeds, you can read the data from the file until you see a carriage return or a line feed. Either one indicates that you read an entire "record". If it isn't the right one, based one whatever criteria you are using, repeat the process until it is the right one.
How would I go about implementing that in code?
You already have by using Serial.println().
Actually Paul, you can write a file as records. Each line in a text file can be taken as a record but what I have known as records are simply fixed-length sections of data that make random access easy. The same books that outlined that for me (over 30 years ago, have things changed so much?) would then go on the the Advanced Topic of variable-length records.
How would I go about implementing that in code?
Create an array, probably global.
Create an index variable, initialized to 0, probably global.
Read a character from the file. If is is not the end-of-record marker (whatever that is, probably '\n'), (and is not the linefeed, '\r')store the character at the index position, increment the index, and store a NULL at the index position.
If it is the end of record marker, use the data in the array, if it is useful, increment the record number, reset the index value to 0, and keep reading, until you find the right record or the end of the file.
No, I won't write the code for you.
Ok thanks, I'll give that a go. Have to do a lot of research I think, not used arrays before. Would I read everything in the file to one string ie allData = sd.read(); then split it from that string? or would i split it as it reads with the given character? I'm thinking of using a " , " at the end of each line.
Would I read everything in the file to one string ie allData = sd.read();
That depends on which Arduino you have and ho0w big the file is. Most likely not, though.
I'm using the mega 2560 and the file i would imagine would remain quite small.
Would I be able to use a " , " to denote the end of one entry?
Are you really a student? If so, then you should be learning how to write C and/or C++ code, not looking for shortcuts everywhere.
Learn to read data from a file, and store it in an array.
Learn to parse that array properly.
Do NOT look to use crutches where you do not need to.
No one ever regretted learning something - the only complaint is when they HAD to learn something that put off until it was too late to do a good job.
PaulS:
No one ever regretted learning something
Uh... actually I regretted learning how hot dogs are made... I used to like them.
uobstudent:
I'm using the mega 2560 and the file i would imagine would remain quite small.Would I be able to use a " , " to denote the end of one entry?
Could you type a short example of what you want in a text editor like Wordpad?