I need to read a text file on the SD card and print that to a LCD screen but when it comes to ">" in the text file I want it to pause from reading and wait for a button press to start back where it left off. Or it could keep track where it left off at and continue when a button is pressed.
// re-open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
// This is where I would put the code to pause
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
This is my attempt.
// open the file for reading:
char receivedChars1[16]; // an array to store the received data from line 1
char receivedChars2[16];
char receivedChars3[16];
char receivedChars4[16];
myFile = SD.open("test.txt");
if (myFile) {
Serial.println(" ");
Serial.println("test.txt:");
// read from the file until there's nothing else in the line:
char endMarker = '\n';
char rc;
int x;
while (myFile.available()) {
//Serial.write(myFile.read());
rc = myFile.read();
Serial.write(rc);
receivedChars1[x] = rc;
x++;
if (rc == endMarker) {
Serial.print("End of line 1: ");
//myFile.close();
int y;
do{
Serial.print(receivedChars1[y]);
y++;
}while (y < 16);
break;
}
}
Serial.println(" ");
Serial.println("test.txt 2nd line: ");
x = 0;
while (myFile.available()) {
//Serial.write(myFile.read());
rc = myFile.read();
Serial.write(rc);
receivedChars2[x] = rc;
x++;
if (rc == endMarker) {
Serial.print("End of line 2: ");
//myFile.close();
int y;
do{
Serial.print(receivedChars2[y]);
y++;
}while (y < 16);
break;
}
}
Serial.println(" ");
Serial.println("test.txt 3rd line: ");
x = 0;
while (myFile.available()) {
//Serial.write(myFile.read());
rc = myFile.read();
Serial.write(rc);
receivedChars3[x] = rc;
x++;
if (rc == endMarker) {
Serial.print("End of line 3: ");
//myFile.close();
int y;
do{
Serial.print(receivedChars3[y]);
y++;
}while (y < 16);
break;
}
}
Serial.println(" ");
Serial.println("test.txt 4th line: ");
x = 0;
while (myFile.available()) {
//Serial.write(myFile.read());
rc = myFile.read();
Serial.write(rc);
receivedChars4[x] = rc;
x++;
if (rc == endMarker) {
Serial.print("End of line 4: ");
//myFile.close();
int y;
do{
Serial.print(receivedChars4[y]);
y++;
}while (y < 16);
break;
}
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
Now how would I change this so that one: it can read the text file so it knows how many lines to read and two: then puts every line into variables so all the data in the file has been read? Thanks
Thank you I will make the changes.
I thought a lot about this and I think the best way to know how many lines are in the text file is to make the first line that number. So if the first line has "20" there would be 20 lines more.
Thanks for your help
chrisnet:
I thought a lot about this and I think the best way to know how many lines are in the text file is to make the first line that number. So if the first line has "20" there would be 20 lines more.
I suspect that you're going to skate on thin ice as this basically requires dynamic memory allocation.
You're probably better of having a two-dimensional array holding just the information that is to be displayed. The approach could be something like
1)
read initial 4 lines into array
2)
display and wait
3)
after keypress
move lines 1 .. 3 one position up (0 .. 2); check out [i]memcpy[/i]
read one line and srore in position 3 of array; check out [i]strcpy[/i]
back to (2)
Note that this does not cater for end-of-fie; I'll leave that up to you. Also note that the numbers above are indices (0 is the first element of the array)
sterretje:
I suspect that you're going to skate on thin ice as this basically requires dynamic memory allocation.
This is the current issue! After combining all the new code for the sd card with my existing project, I am way over and now have to consider a different chip. I was at 70% dynamic memory allocation now at 120% and I still have more code to add. I was trying to use the ATmega328 any suggestions?