A little help please regarding SD cards and LCD [solved]

hi guys,

I am trying to integrate an SD card with a LCD screen. I’m trying to modify a code that displays SD card status info to the LCD screen instead of a monitor. but I’m having a little bit of trouble. The first thing i want to display is “Initializing Card” on the LCD screen for 3 sec, That works fine. then when the check is done for the SD card I want that to display “Card Failed” if the there was a problem or “Card Ready” if everything is fine for 3 sec (this part does not show up on the LCD screen). the code works fine for serial output but not for he LCD screen. any help would be appreciate it. thank you in advance.
regards,

SNK

#include <SD.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd( 12,11, 5, 4, 3, 2 );


int CS_pin = 8; //correct pin
int pow_pin = 7; //correct pin (could be any unoccupied pin)

void setup()
{
Serial.begin(4800);
lcd.begin(16, 2);

Serial.println("Initializing Card...");

lcd.setCursor(0,0);
lcd.print("Initi Card...");
delay(3000);//ms

pinMode(CS_pin, OUTPUT);
pinMode(pow_pin,OUTPUT);
digitalWrite(pow_pin,HIGH);

//check if card is ready
lcd.setCursor(0,1);
if(!SD.begin(CS_pin))
{
  Serial.println("Card Failed");  
  lcd.print("Card Failed");  
  return;// terminates entire program 
}
  Serial.println("Card Ready");
  lcd.print("Card Ready");
  delay(3000);//ms  
}

void loop()
{
String dataString = "Test";
File dataFile = SD.open("Test.csv", FILE_WRITE);
if(dataFile)
{
  dataFile.println(dataString);
  dataFile.close();
  Serial.println(dataString); 
}

else //if file couldnt be created
{
  Serial.println("Couldn't access file");

}

delay(5000);//ms

}

You have your LCD connected to the SPI pins (if you are using a UNO or other 328 based Arduino). That will never work, since the SD card access is via SPI. Get the LCD off of pins 11 and 12. Don't use 10 or 13, either.

  return;// terminates entire program

No, it doesn't.

thank you. much appreciate it. cheers

@PaulS: You didn’t comment on this …

String dataString = "Test";
File dataFile = SD.open("Test.csv", FILE_WRITE);
if(dataFile)
{
  dataFile.println(dataString);
  dataFile.close();
  Serial.println(dataString); 
}

@snkhan: Best not to use String if you can help it. You could have just used:

const char * dataString = "Test";

[quote author=Nick Gammon link=topic=272383.msg1920165#msg1920165 date=1413269526] @PaulS: You didn't comment on this ...

String dataString = "Test";
File dataFile = SD.open("Test.csv", FILE_WRITE);
if(dataFile)
{
  dataFile.println(dataString);
  dataFile.close();
  Serial.println(dataString); 
}

@snkhan: Best not to use String if you can help it. You could have just used:

const char * dataString = "Test";

[/quote]

Even better:

dataFile.println(F("Test"));

Thank you so much for a quick and precise reply. I just fixed the pins and now its working fine. Here is the updated code:

/*
 LCD to Ard_Uno 
GND(1): GDN
5V(2):  5V
Vo(3):  pot
RS(4):  Pin 2
R/W(5): GND
EN(6):  Pin 3
D0(7):
D1(8):
D2(9):
D3(10):
D4(11): Pin 4
D5(12): Pin 5
D6(13): Pin 6
D7(14): Pin 7
LED+(15): 5V
LED-(16): GND
*/

/*
* SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 8
*/

#include <LiquidCrystal.h>
#include <SD.h>

//Initialize an LCD Object
LiquidCrystal lcd(2,3,4,5,6,7);

int CS_pin = 8; //correct pin
int pow_pin = 9; //correct pin (could be any unoccupied pin)

void setup()
{
Serial.begin(4800);
lcd.begin(16, 2);

Serial.println("Initializing Card...");

lcd.setCursor(0,0);
String dataString1 = "Initi Card...";
lcd.print(dataString1);
delay(3000);//ms

pinMode(CS_pin, OUTPUT);
pinMode(pow_pin,OUTPUT);
digitalWrite(pow_pin,HIGH);

//check if card is ready
lcd.setCursor(0,1);
if(!SD.begin(CS_pin))
{
  Serial.println("Card Failed");  
  lcd.print("Card Failed");  
  delay(2000);
  return;// terminates entire program ????? google
}
else
{
  Serial.println("Card Ready");
  lcd.print("Card Ready");
  delay(2000);//ms

}
  
}

void loop()
{
String dataString = "Test";
File dataFile = SD.open("Test.csv", FILE_WRITE);
lcd.clear();

if(dataFile)
{
  lcd.print("Writing to File");
  dataFile.println(dataString);
  dataFile.close();
  Serial.println(dataString);
  delay(1000);
  lcd.clear();
  lcd.print(dataString);
  delay(1000);//ms
}
else //if file couldnt be created
{
  Serial.println("Couldn't access file");
  lcd.print("Couldn't access file");
}
delay(2000);//ms

}

p.s
@Nick Gammon >> thanks for the more efficient code will implement that next.