Hello all
I have an UNO set up, with a TXT file I am reading that contains various text information. Probably about 15 lines.
I don't really understand the in's and out's of strings etc (and I seem to remember strings were bad for Arduinos?). Might be wrong.
My question is, how can I read each line as text that I can reproduce on an LCD screen later on? Bearing in mind these text lines will vary in length (probably up to around 30 character max, but usually a lot shorter).
I have been up all night tinkering... not really got anywhere to be honest. You can laugh at my code.
This is where I left it... it searches for a #, and then uses that as the indication that a line number follows.
Then, it reads the following pair of digits.
Based on that number, it would then retrieve the text if I asked it to.
This is where is all goes a bit wayward. I would have to add another symbol at the end of the sentence to indicate the end of the text. I originally thought of using the next # symbol at the start of the next line. Didn't seem to work that well.
I also am getting madly mixed up with returned values. To identify a # symbol, I am having to search for a returned value of 35. However, when it returns the number values, I have to deduct 48 to get the correct number.
Its a mess! It did start all tidy. Constant tweaking has ruined it.
This was searching for line 3
Ideally, it would read 15 lines and store them in 15 EEPROM addresses, but the UNO is gonna run out of memory before that ever happens.
#include <SPI.h>
#include <SD.h>
File myFile;
int hash1;
int data1;
int data2;
int data3;
byte i;
//----------------------------------------------------------------------------------------------------
void setup()
{
Serial.begin(9600); // Open serial communications and wait for port to open:
while (!Serial) {
}
Serial.print(F("Checking for SD card... "));
if (!SD.begin(4)) {
Serial.println(F("No SD card present, using memory stored/default data"));
return;
}
Serial.println(F("Found SD card"));
delay(80);
// Open the file for reading:
myFile = SD.open("SiteData.txt");
if (myFile) {
Serial.println(F("SiteData.txt:"));
Serial.println(F(" "));
// read from the file until there's nothing else in it:
while (myFile.available()) {
//Serial.write(myFile.read());
hash1=myFile.read(); //Serial.print(char(hash1)); //Search for a hash
if (hash1==35) {data1=(myFile.read());Serial.print(char(data1));data2=(myFile.read());Serial.print(char(data2));} //If a hash is found, get the line number of the data
if (((data1-48)*10+(data2-48))==3) {for (i=0;i<=60;i++) {data3=(myFile.read());Serial.print(char(data3)); if (data3==35) {break;}}}
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println(F("error opening SiteData.txt"));
}
void loop() {}