bembe24
February 3, 2017, 11:08am
1
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?
#include <SD.h>
#include <SPI.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7);
File myFile;
const int CSpin = 10;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(CSpin, OUTPUT);
SD.begin(CSpin);
lcd.begin(20,4);
lcd.setBacklightPin(3, POSITIVE);
lcd.setBacklight(HIGH);
}
void loop() {
// put your main code here, to run repeatedly:
lcd.setCursor(0,0);
ReadLine();
myFile.close();
}
void ReadLine(){
myFile = SD.open("Sample.txt", FILE_READ);
myFile.seek(0);
char cr;
for(unsigned int i = 0; i < (3 - 1);){
cr = myFile.read();
if(cr == '\n')
{
i++;
}
}
while(true){
cr = myFile.read();
Serial.write(cr);
lcd.print(cr);
if(cr == '\n'){
break;
}
}
}
I attached a picture of what it looks like
I just think you have some non-printable characters there. Maybe a carriage return?
And why do you open the file in a function but close it in loop()?
bembe24
February 3, 2017, 11:36am
3
what can I do to get rid of those characters without using the delete ASCII characters?
It was just a test if the arduino can open and close a file in void loop
Option 1: use an editor (in which you make th file) that can show you all characters. For example Notepad++ can do that.
Option 2: Ignore all chars you can't print. So I would say only print chars between 0x20 and 0x7E (included)
Im sorry im new to these can you please give me a sample code of that?
#include <SD.h>
#include <SPI.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7);
File myFile;
const int CSpin = 10;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(CSpin, OUTPUT);
SD.begin(CSpin);
lcd.begin(20,4);
lcd.setBacklightPin(3, POSITIVE);
lcd.setBacklight(HIGH);
}
void loop() {
// put your main code here, to run repeatedly:
lcd.setCursor(0,0);
ReadLine();
myFile.close();
}
void ReadLine(){
myFile = SD.open("Sample.txt", FILE_READ);
myFile.seek(0);
char cr;
for(unsigned int i = 0; i < (3 - 1);){
cr = myFile.read();
if(cr == '\n')
{
i++;
}
}
while(true){
cr = myFile.read();
Serial.write(cr);
if(cr == '\n'){
break;
}
//here...
else if(cr >= 0x20 && cr <= 0x7E){
lcd.print(cr);
}
}
}