could someone please point me in a starting direction,
i have a text file i want to read from an SD card using the SD library, and want to find certain words in the text file and store the numbers on the lines that follow the words.
the main problem i have is if im looking for 2 words in the file, say LINE and ARC, but i may not know which will show up first, ie the file may look like
LINE
10.0
120.0
LINE
10.0
120.0
ARC
10.0
120.0
LINE
10.0
90.0
ive tried using the stream findUntil function but it wont let me use an "or" eg.
myFile.findUntil( "AcDbCircle" || "AcDbLine", "EOF" ) ;
it's somewhat similar to reading input from a serial line see Serial Input Basics and using the standard C libraries to perform c-string comparison/analysis (stdlib.h and string.h)
until end of file:
---> read characters until the end of line or end of file in a buffer
---> compare the buffer with what you are looking for
---> decide next action based on what you found
---> proceed with next line
(would suggest using SDFat library instead of standard SD)
Reading byte by byte until the \n and storing that in a buffer is not rocket science… will take you a few lines of code
Here is an example I just typed in, don’t have hardware at hand to test if that works but you’ll get the idea
/*
* assuming file name is param.txt
* assuming SD card connected on standard hardware SS line of the SPI bus
* assuming file format is a repetition of [<COMMAND><new line><x><new line or space><y><new line>]
* where <COMMAND> is either ARC or LINE and <x> and <y> are floating point numbers
*/
#include <SD.h>
#include <SPI.h>
const byte buffer_size = 20; // need to be big enough
char lineBuffer[buffer_size + 1]; // keep space for a trailing null char
int bufferIndex;
char character;
File parameterFile;
void processLineBuffer()
{
float x, y;
lineBuffer[bufferIndex] = '\0'; // mark the end of the c-string
if (!strcmp(lineBuffer, "LINE")) {
Serial.println("Found LINE");
// here you could decide to read the next 2 values from the file with parameterFile.parseFloat() assuming your file is correctly structured
x = parameterFile.parseFloat();
y = parameterFile.parseFloat();
// here you do what you want to do with x and y and draw a line
Serial.print("drawing line to x="); Serial.print(x, 3); Serial.print(" y="); Serial.println(y, 3);
}
else if (!strcmp(lineBuffer, "ARC")) {
Serial.println("Found ARC");
// here you could decide to read the next 2 values from the file with parameterFile.parseFloat() assuming your file is correctly structured
x = parameterFile.parseFloat(); // this really assumes your file is well formatted
y = parameterFile.parseFloat(); // this really assumes your file is well formatted
// here you do what you want to do with x and y and draw an arc
Serial.print("drawing arc to x="); Serial.print(x, 3); Serial.print(" y="); Serial.println(y, 3);
}
else {
Serial.print("command not recognized, ignoring [");
Serial.print(lineBuffer);
Serial.print("]");
}
// =====================================================================
bufferIndex = 0; // get ready for the next line
}
void setup()
{
Serial.begin(115200);
if (!SD.begin()) { // or give the cspin (optional): the pin connected to the chip select line of the SD card; defaults to the hardware SS line of the SPI bus
Serial.println("ERROR The SD card cannot be found");
while (1); // die here
}
parameterFile = SD.open("param.txt", FILE_READ); // whatever the name of your file
if (!parameterFile) {
Serial.println("ERROR The parameter file cannot be found");
while (1); // die here
}
bufferIndex = 0; // just for the sake of it, already initialized to 0 as global variable
while (parameterFile.available() > 0) {
character = parameterFile.read();
if (bufferIndex >= buffer_size) bufferIndex = buffer_size - 1; // that will overwrite the end of the buffer and you won't be able to read the full line, buffer too small
if ((character != '\r') && (character != '\n')) lineBuffer[bufferIndex++] = character; // if this is not a end of line marker, add it to the buffer
if (character == '\n') processLineBuffer(); // end of line detected
}
if (bufferIndex > 0) processLineBuffer(); // this means the last line of the file was not terminated by a '\n'
parameterFile.close();
}
void loop() {}