arduino + ethernet shield (sd card reader) + I2C LCD display

hello,

i am using a micro sd chip. On this chip is a txt file and within this file is written the number 1.
when i put this chip in the ethernet shield and run my code, it writes this number 1 to the serial monitor well, but when i say: print it on the lcd .. it prints -1.
what is wrong? i want it to show 1, not -1

code:

const int chipSelect = 4;

#include <SdFat.h>
#include <LiquidCrystal_I2C.h>
#include <Ethernet.h>
#include <SPI.h>
#include <Wire.h>
SdFat sd;
SdFile myFile;
LiquidCrystal_I2C lcd(0x27,16,2);

void setup() {
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0,0);

  // Initialize SdFat or print a detailed error message and halt
  // Use half speed like the native library.
  // change to SPI_FULL_SPEED for more performance.
  if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();

  // re-open the file for reading:
  if (!myFile.open("test.txt", O_READ)) {
    sd.errorHalt("opening test.txt for read failed");
  }
  Serial.println("test.txt:");

  // read from the file until there's nothing else in it:
  int data;
  while ((data = myFile.read()) > 0) Serial.write(data); lcd.print(data);
  // close the file:
  myFile.close();
}

void loop() {
  // nothing happens after setup
}

thanks.

caspervk:
hello,

i am using a micro sd chip. On this chip is a txt file and within this file is written the number 1.
when i put this chip in the ethernet shield and run my code, it writes this number 1 to the serial monitor well, but when i say: print it on the lcd .. it prints -1.
what is wrong? i want it to show 1, not -1

code:

const int chipSelect = 4;

#include <SdFat.h>
#include <LiquidCrystal_I2C.h>
#include <Ethernet.h>
#include <SPI.h>
#include <Wire.h>
SdFat sd;
SdFile myFile;
LiquidCrystal_I2C lcd(0x27,16,2);

void setup() {
 Serial.begin(9600);
 lcd.init();
 lcd.backlight();
 lcd.setCursor(0,0);

// Initialize SdFat or print a detailed error message and halt
 // Use half speed like the native library.
 // change to SPI_FULL_SPEED for more performance.
 if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();

// re-open the file for reading:
 if (!myFile.open("test.txt", O_READ)) {
   sd.errorHalt("opening test.txt for read failed");
 }
 Serial.println("test.txt:");

// read from the file until there's nothing else in it:
 int data;
 while ((data = myFile.read()) > 0) Serial.write(data); lcd.print(data);
 // close the file:
 myFile.close();
}

void loop() {
 // nothing happens after setup
}




thanks.

hard to follow but try changing your while statement as follows

while ((data = myFile.read()) > 0) 
{
     Serial.write(data); 
     lcd.print(data);
}

Serial.write() sends binary data to the serial port. What is on the other end of the serial port? Is that expecting binary data? The LCD certainly is not.

Try this

while ((data = myFile.read()) > 0)
{
Serial.write(data);
lcd.print(char(data));
}

Good luck

What about this?

while ((data = myFile.read()) > 0) 
{
     Serial.write(data); 
     Serial.print(data);
}

Basically trying to write and print both to serial and see if you get 1-1. I kind of doubt. What RCHobby did was actually putting the second instruction (originally lcd.print(data)) inside the while loop. You will still see some trouble such as seeing 149 but at least that is expected :wink: