SD card read files with LCD

Discussion in 'Embedded Systems and Microcontrollers' started by bembe24, Feb 3, 2017.

  1. bembe24

    Thread Starter New Member

    Oct 1, 2016
    19
    0
    Im trying to read text in SD card and display on the LCD but it display 2 weird characters after the text. What is it and why does it show up?

    Here is my code:

    Code (Text):
    1.  
    2. #include <SD.h>
    3. #include <SPI.h>
    4.  
    5. #include <LiquidCrystal_I2C.h>
    6. LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7);
    7.  
    8. File myFile;
    9. const int CSpin = 10;
    10.  
    11. void setup() {
    12.   // put your setup code here, to run once:
    13.   Serial.begin(9600);
    14.   pinMode(CSpin, OUTPUT);
    15.   SD.begin(CSpin);
    16.  
    17.   lcd.begin(20,4);
    18.   lcd.setBacklightPin(3, POSITIVE);
    19.   lcd.setBacklight(HIGH);
    20. }
    21.  
    22. void loop() {
    23.   // put your main code here, to run repeatedly:
    24.   lcd.setCursor(0,0);
    25.   ReadLine();
    26.   myFile.close();
    27. }
    28.  
    29. void ReadLine(){
    30.   myFile = SD.open("Sample.txt", FILE_READ);
    31.   myFile.seek(0);
    32.   char cr;
    33.   for(unsigned int i = 0; i < (3 - 1);){
    34.     cr = myFile.read();
    35.     if(cr == '\n')
    36.       {
    37.         i++;
    38.       }
    39.   }
    40.  
    41.   while(true){
    42.     cr = myFile.read();
    43.     Serial.write(cr);
    44.     lcd.print(cr);
    45.     if(cr == '\n'){
    46.       break;
    47.     }
    48.   }
    49. }
    50.  
    I attached a picture of what it looks like
     
    Last edited by a moderator: Feb 3, 2017
  2. ericgibbs

    Moderator

    Jan 29, 2010
    8,385
    1,689
    hi,
    Check the hex value of the 'weird' chars, they maybe just carriage return and line feed codes, 0x0d, 0x0a
    E
     
  3. bembe24

    Thread Starter New Member

    Oct 1, 2016
    19
    0
    How? what code am I going to use to check their hex values?
     
  4. GopherT

    AAC Fanatic!

    Nov 23, 2012
    7,983
    6,786

    Your test to check whether your character is '\n' is currently AFTER the write command. You should check it BEFORE sending.
     
    ericgibbs likes this.
  5. bembe24

    Thread Starter New Member

    Oct 1, 2016
    19
    0
    im sorry sir im new to these can you please give me a sample code of that? im confused
     
  6. GopherT

    AAC Fanatic!

    Nov 23, 2012
    7,983
    6,786
    In your last segment of code you had...

    Code (Text):
    1.  
    2.   while(true){
    3.     cr = myFile.read();
    4.     Serial.write(cr);
    5.     lcd.print(cr);
    6.     if(cr == '\n')
    7.       break;
    8.  
    9.  
    Try...
    Code (Text):
    1.  
    2.   while(true){
    3.     if(cr == '\n')
    4.       break;
    5.     cr = myFile.read();
    6.     Serial.write(cr);
    7.     lcd.print(cr);
    8.  
    9.  
     
  7. Kjeldgaard

    Member

    Apr 7, 2016
    370
    129
    Something like:
    Code (Text):
    1.     if(cr == '\n')
    2.       break;
    3.     lcd.print(cr);
    And maybe a extra check for Carriage Return:
    Code (Text):
    1.     if((cr == '\r') || (cr == '\n'))
    2.       break;
    3.     lcd.print(cr);
    Edit: Inserted Code Tags
     
    Last edited: Feb 3, 2017
  8. bembe24

    Thread Starter New Member

    Oct 1, 2016
    19
    0
    I replaced '\n' with '\r' and It removed the other one, but it still displays one weird character right next to the text
     
  9. GopherT

    AAC Fanatic!

    Nov 23, 2012
    7,983
    6,786
    Now try it with the exact command @Kjeldgaard proposed. With the || command that means "or". One advances the line (\n) and one moves back to the left margin (return)\r. You want to check for either. And, you want to move that check like I shared above (the check is the IF statement).
     
  10. bembe24

    Thread Starter New Member

    Oct 1, 2016
    19
    0
    while(true){
    if((cr == '\r')||(cr == '\n'))
    break;
    cr = myFile.read();
    lcd.print(cr);
    }

    using the code It still displayed that one weird character
     
  11. Kjeldgaard

    Member

    Apr 7, 2016
    370
    129
    Now the instructions is in wrong order, try:
    Code (Text):
    1.     while(true){
    2.     cr = myFile.read();
    3.     if((cr == '\r')||(cr == '\n'))
    4.         break;
    5.     lcd.print(cr);
    6.     }
     
  12. bembe24

    Thread Starter New Member

    Oct 1, 2016
    19
    0

    IT WORKED!!! Thank you sooo much for the help! :D

    One last thing, why was it displaying that kind or character? and what does newline and carriage return actually do? Ive been working with this project in over 2 weeks now and im still confused of what their purpose is :(
     
  13. MrChips

    Moderator

    Oct 2, 2009
    19,051
    6,121
    Text files are written with line terminators, CR (decimal 13) and LF (decimal 10).

    This is a carry-over from the typewriter-teletype era where one had to press RETURN to move the print head to the left of the page and NEW LINE to move down one line.

    One could have used other characters to terminate the line of text. Some systems use CR alone or some other "control character".

    Control Characters are used for special effects and are usually not displayed on an LCD. They occupy the first 32 spots in the ASCII Table.
    Since they might not be relevant for an LCD, the manufacturers of LCD controller chips (Hitachi HD44780, for example) have taken the liberty to use these for user defined characters. What you are seeing on the LCD is some random pattern that appears in the LCD RAM when the power is turned on.

    ASCII Table:

    http://www.theasciicode.com.ar/ascii-control-characters/carriage-return-ascii-code-13.html
     
  14. Kjeldgaard

    Member

    Apr 7, 2016
    370
    129
    MrChips was a little faster, but I think there is a little extra in the following text:

    It's a slightly longer history, but ASCII codes can read a lot about the web, for example on Wikipedia

    However, the current problem are the lower 32 codes which are normally not visible on a screen or on paper.

    And it seems that lcd.print () does not look at the character value, but simply writes the received character up on the LCD. Here you need to get the data sheet for the display to see what the graphics are displayed by the low codes.
     
  15. GopherT

    AAC Fanatic!

    Nov 23, 2012
    7,983
    6,786
    These are "hidden characters" and typical to end a line in text (otherwise a computer doesn't know when the paragraph ends. Look up ASCII (This is the number equivalent of each text character saved on your computer).
     
  16. bembe24

    Thread Starter New Member

    Oct 1, 2016
    19
    0
    So what it did was read the char '\n' display on the LCD?
     
  17. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,986
    1,844
    I have a program on my PC called "Notepad" that too is dependent on the CR/LF characters to skip to the beginning of the next line. Most text editors have a similar scheme for end of line.

    It's not as archaic as you may guess.
     
  18. GopherT

    AAC Fanatic!

    Nov 23, 2012
    7,983
    6,786

    We don't know the origin of your text string that displayed the date but many sources of text strings automatically put a \n (new line), and \r (return) on the end of a command, like PRINT ('abc')
    will output: abc like this...

    >>PRINT ('abc')
    abc
    >>

    The >> shows that the command prompt has returned to the user for entering more text. See how the 'abc' is on a line by itself? the \n made that happen (the PRINT command automatically added that for you). Also, notice that the cursor doesn't have 3 empty spaces to the left, the return character \r made that happen too.).
     
  19. bembe24

    Thread Starter New Member

    Oct 1, 2016
    19
    0
    I encounterd another problem
    I can read the first line up to the 2nd to the last line. When I try ko read the last line the program stops running an LCD displays all white squares. what am I doing wrong here?

    I used the code from before with the suggestions you gave me:

    Code (Text):
    1.  
    2.   myFile.seek(0);
    3.   char cr;
    4.   for(unsigned int i = 0; i < 15;){
    5.     cr = myFile.read();
    6.     if(cr == '\n')
    7.       {
    8.         i++;
    9.       }
    10.   }
    11.   while(true){
    12.     cr = myFile.read();
    13.     if((cr == '\n')||(cr == '\r'))
    14.     break;
    15.     lcd.print(cr);
    16.   }
    17.  
    Moderators note: added code tags, for more info on BB-codes look here : http://forum.allaboutcircuits.com/help/bb-codes
     
    Last edited by a moderator: Feb 3, 2017
  20. GopherT

    AAC Fanatic!

    Nov 23, 2012
    7,983
    6,786
    When you post code, put it inside of "code tags"
    [ c o d e ]

    Your code here

    [ / c o d e ]

    Don't put spaces between letters in "code" in your actual post.
     
Loading...