Hey guys,
I'm trying to integrate an LCD to a project with an existing micro sd card module that is already hooked up correctly. Now I'm trying to integrate the Sparkfun color LCD shield (the Nokia 6100 display). When I run the following piece of code (the setup section is the relevant part here), the LCD module seems to responds up to the moment when I'm start using the sd card. After that, the LCD doesn't responds to any display command.
I suspect that the problem is in the SPI communication rather than in the LCD itself, but I can't pinpoint the problem and don't know how to surmount it.
Please help me overcome this problem, I'll be so grateful!
This is the code in which I don't know what the problem is:
#include <SD.h>
#include <SPI.h>
#include <SparkFunColorLCDShield.h>
int CS_PIN = 10; // Chip Select pin for the SD
int LCD_SSpin=9; // Chip Select pin for the LCD
LCDShield lcd;
int buttonPins[3] = {3, 4, 5};
File file;
void setup()
{
Serial.begin(9600);
// LCD initialization:
for (int i = 0; i < 3; i++)
{
pinMode(buttonPins[i], INPUT);
digitalWrite(buttonPins[i], HIGH);
}
lcd.init(PHILIPS); // This initialization function turns on the communication with the LCD
lcd.contrast(-51);
lcd.clear(BLACK);
lcd.setStr("First", 30, 30, WHITE, BLACK); // This line executed correctly so "First" was displayed on the LCD
delay(1000);
lcd.clear(BLACK);
digitalWrite(LCD_SSpin, HIGH); // Turn off the communication with the LCD
initializeSD(); // This initialization function turns on the communication with the SD
createFile("test.txt");
writeToFile("This is sample text!");
closeFile();
openFile("prefs.txt");
String WORD;
unsigned int len=10;
char buf[10];
WORD=readLine();
closeFile();
WORD.toCharArray(buf,len);
Serial.println(buf);
// Up to now, everything works pretty well.
digitalWrite(CS_PIN, HIGH); // Turn off the communication with the SD
digitalWrite(LCD_SSpin, LOW); // Turn on the communication with the LCD
lcd.setStr("Second", 30, 30, WHITE, BLACK); // This line does not executed correctly at all, the "Second" wasn't displayed on the LCD :(
lcd.setStr(buf, 50, 50, WHITE, BLACK); // This line also does not executed correctly, probably because of the same esoteric reason.
delay(1000);
}
void loop()
{
}
void initializeSD()
{
Serial.println("Initializing SD card...");
pinMode(CS_PIN, OUTPUT);
if (SD.begin())
{
Serial.println("SD card is ready to use.");
} else
{
Serial.println("SD card initialization failed");
return;
}
}
int createFile(char filename[])
{
file = SD.open(filename, FILE_WRITE);
if (file)
{
Serial.println("File created successfully.");
return 1;
} else
{
Serial.println("Error while creating file.");
return 0;
}
}
int writeToFile(char text[])
{
if (file)
{
file.println(text);
Serial.println("Writing to file: ");
Serial.println(text);
return 1;
} else
{
Serial.println("Couldn't write to file");
return 0;
}
}
void closeFile()
{
if (file)
{
file.close();
Serial.println("File closed");
}
}
int openFile(char filename[])
{
file = SD.open(filename);
if (file)
{
Serial.println("File opened with success!");
return 1;
} else
{
Serial.println("Error opening file...");
return 0;
}
}
String readLine()
{
String received = "";
char ch;
while (file.available())
{
ch = file.read();
if (ch == '\n')
{
return String(received);
}
else
{
received += ch;
}
}
return "";
}
Any help will be highly appreciated ![]()
Thanks!