Am learning to access an SD card and then displaying the contents to an LCD.
I have a Nano-compatible connected to the LCDisplay via I2C and SD card module via SPI.
Both modules work using them one at a time but now the difficulties start when i start combining them together.
The code actually works, but sometimes the LCDisplay doesn't lcd.clear() - i thought it was the code but discovered that it works after i closed and then opened the Serial Monitor (for debugging).
And now i encounter an error message which i have no idea about - is this hardware related ? (or bad setup??)
Here's the error message;
Error inside Serial.serialEvent()
java.io.IOException: Bad file descriptor in nativeavailable
at gnu.io.RXTXPort.nativeavailable(Native Method)
at gnu.io.RXTXPort$SerialInputStream.available(RXTXPort.java:1532)
at processing.app.Serial.serialEvent(Serial.java:258)
at gnu.io.RXTXPort.sendEvent(RXTXPort.java:732)
at gnu.io.RXTXPort.eventLoop(Native Method)
at gnu.io.RXTXPort$MonitorThread.run(RXTXPort.java:1575)
Error inside Serial.serialEvent()
java.io.IOException: Bad file descriptor in nativeavailable
at gnu.io.RXTXPort.nativeavailable(Native Method)
at gnu.io.RXTXPort$SerialInputStream.available(RXTXPort.java:1532)
at processing.app.Serial.serialEvent(Serial.java:258)
at gnu.io.RXTXPort.sendEvent(RXTXPort.java:732)
at gnu.io.RXTXPort.eventLoop(Native Method)
at gnu.io.RXTXPort$MonitorThread.run(RXTXPort.java:1575)
Error inside Serial.serialEvent()
i also notice a red LED is lit up on the Nano (Iteaduino Nano) which is visible in the pic below.

this is the code itself;
/*
** Example Arduino sketch for SainSmart I2C LCD Screen 16x2
** by Edward Comer
** uses F Malpartida's NewLiquidCrystal library.
*/
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x20 //
#define BACKLIGHT_PIN 7
// using MJKDZ board
#define En_pin 4
#define Rw_pin 5
#define Rs_pin 6
#define D4_pin 0
#define D5_pin 1
#define D6_pin 2
#define D7_pin 3
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
/*
SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 10
created 24 Nov 2010
modified 9 Apr 2012 by Tom Igoe
This example code is in the public domain.
*/
#include <SD.h>
// Note that even if it's not used as the CS pin, the hardware CS pin
// must be left as an output or the SD library functions will not work.
const int chipSelect = 10;
void setup()
{
lcd.begin (16,2);
//Switch on the backlight
lcd.setBacklightPin(BACKLIGHT_PIN,NEGATIVE);
lcd.setBacklight(HIGH);
lcd.home ();
lcd.print("LCD connected");
Serial.begin(9600);
Serial.print("Initializing SD card...");
// make sure default chip select pin is set to output
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
} // end setup()
String readChar;
int posChar;
void loop()
{
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("what2prn.txt"); // default as FILE_READ
lcd.clear();
// lcd.autoscroll();
if (dataFile) {
while(dataFile.available()) {
// it's THIS SIMPLE - display byte-by-byte, NO need String var
lcd.write(dataFile.read());
// lcd.autoscroll();
}
//
posChar = 0;
lcd.setCursor(posChar,1);
lcd.write("End of file reached");
Serial.println("End of file reached");
delay(500);
Serial.println("Clearing LCDisplay");
lcd.clear();
delay(500);
//
}
}