Go Down

Topic: Read from SD card to LCD screen (Read 519 times) previous topic - next topic

chrisnet

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.

Code: [Select]
// 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");
  }

chrisnet

#1
Jul 18, 2019, 03:38 am Last Edit: Jul 18, 2019, 04:27 am by chrisnet
This is my attempt.

Code: [Select]
// 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

Delta_G

Code: [Select]
int y;
        do{
          Serial.print(receivedChars1[y]);
          y++;
        }while (y < 16);


There's a bug.  y needs an initial value.  Since it is not global nor static, it will come into being with whatever garbage value was left over in that memory location.  Best to have an =0 at the definition. 

Why not just:

Code: [Select]
Serial.print(receivedChars1);

Which would print the same thing and do away with the do-while altogether. 



Don't you get tired of typing that same stuff over and over?  Why not make that a function?

When you catch yourself putting numbers in variable names:

Code: [Select]
char receivedChars1[16];   // an array to store the received data from line 1
 char receivedChars2[16];
 char receivedChars3[16];
 char receivedChars4[16];


What you really need is an array.  In this case it looks like you need an array of arrays, a 2D array.  Then you could write all that serial reading code once and put it in a function and pass in a parameter to tell it which one of these you're working on. 


As for the answer to your question about putting every line into a different variable, well you need to know how many lines first.  Or at least what will be the maximum number of lines. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

chrisnet

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

sterretje

#4
Jul 18, 2019, 10:17 am Last Edit: Jul 18, 2019, 10:18 am by sterretje
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
Code: [Select]

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)
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

chrisnet

#5
Jul 18, 2019, 04:23 pm Last Edit: Jul 18, 2019, 04:24 pm by chrisnet
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?

Delta_G

It's hard to make suggestions when we can't see the whole code and know absolutely nothing about what the project is or what it is supposed to do. 

|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

Go Up