LCD interfearing with SD

I have a 16 x 2 LCD shield that I got specifically because it uses pind D4 - D8 for data, allowing me to use shields that use the higher pins (SPI)... or so I thought. By default or at least based on the code I found online, the LCD uses pin 13 for RW. This is optional. But in my code, if I use any LCD calls in the main loop, then nothing gets written to the SD card. I have removed the LCD from the Arduino Uno, and wired it manually NOT connecting anything to pin 13. The LCD uses Pins D4 - D9 and the SD uses D10 - D13. I changed the initialization routing LiquidCrystal to use the option without the RW pin and looking through the code for the Liquid Crystal routines if you do this, it should not mess with RW and again pin 13 is NOT connected, but still nothing written to the SD if I have any LCD commands in the main loop. Here is my code:

// testing how quickly can poll the 3208 ADC

#include<SPI.h>
#include<LiquidCrystal.h>
#include <SdFat.h>

SdFat sd;
SdFile myFile;
const int chipSelect = 10;
int lcdpin = 9;
int adcpin = 2;

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

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

// Set the cable select for the ADC.
pinMode(adcpin, OUTPUT);
digitalWrite(adcpin, HIGH);

// 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();

// open the file for write at end like the Native SD library
if (!myFile.open("speed.csv", O_RDWR | O_CREAT | O_AT_END)) {
sd.errorHalt("opening speed.txt for write failed");
}
myFile.println("ADC,Time");
myFile.close();

}

void loop() {
int count;
unsigned long timelog[100];
int datalog[100];

// lcd.clear();
// lcd.setCursor(0,0);
// lcd.print(" Ready");
delay(2000);

//for(int i = 0;1 < 3;i ++) {
for (int i = 0; i < 100; i ++) {
datalog = adcpinread(7);
_ timelog = micros();_
* delayMicroseconds(100);*
* }*
// lcd.clear();
// lcd.setCursor(0,0);
// lcd.print("counting done");
// delay (3000);
* if (!myFile.open("speed.csv", O_RDWR | O_CREAT | O_AT_END)) {
_
sd.errorHalt("opening test.txt for write failed");_
_
//lcd.setCursor(0,1);_
_
//lcd.print("Disk error:");_
_
}_
_
// else {_
_
// lcd.setCursor(0,1);_
_
// lcd.print("test opened");_
_
// } _
_
delay(3000);_
_
for (int i = 0;i < 100; i ++) {_
_ myFile.print(datalog);
myFile.print(",");
myFile.println(timelog);
Serial.print("ADC: ");
Serial.print(datalog);
Serial.print(" Time: ");
Serial.println(timelog);
}
//}
myFile.close();*_

* lcd.clear();*
* lcd.setCursor(0,0);*
* lcd.print(" Done");*
* delay(50000);*
* pinMode(3, OUTPUT);*
* digitalWrite(3, HIGH);*
}

int adcpinread(int channel)
{
* int adcvalue = 0;*

* byte transbyte = 6;*
* byte returnbyte;*

* digitalWrite(adcpin, LOW); // Turn on device "adcpin"*

* // Set the first byte to transfer,*
* // with with a start bit and a 1 for single ended*
* // (not differentail) and the MSB of the channel number*
* if (channel >3) {*
* transbyte = 7;*
* }*
* SPI.transfer(transbyte);*

* // Set the next transfer byte with the channel number*
* // minus the MSB and catch the first 4 bits of the*
* // return value*
* transbyte |= (channel << 6);*
* returnbyte = SPI.transfer(transbyte);*
* // add the first four bits of the valuse to adcvalue*
* // then shift them to the first four (of 12) bits*
* adcvalue = returnbyte & 15;*
* adcvalue = adcvalue << 8;*
* // recieve the last 8 bits and add them to adc value*
* returnbyte = SPI.transfer(0x0);*
* adcvalue += returnbyte;*

* //end communication with device adcpin*
* digitalWrite(adcpin, HIGH);*
* return adcvalue;*
}
[/quote]
Please help. I think I have worked around all my other problems (limited pins) by using an 8 input 12 bit ADC, but I REALLY want to have both the SD and the LCD without having to buy a different Aruduino.
Bob

Does the SD work when the LCD is disconnected. I ask this because you are not using the common pin calls for it.

Nick_Pyner:
Does the SD work when the LCD is disconnected. I ask this because you are not using the common pin calls for it.

Not sure what you are asking. The LCD uses different pins so that it will not interfere with other shields. But I think the SD is using normal SPI. But to try to answer your question. I don't think I have to even disconnect the LCD to make it work, just comment out all the function calls in the main loop. The LCD works in all configurations I have tried.

Also, even with the LCD code working, the SD DOES write in the setup function, where there are no calls to the LCD. I can also move pins on the LCD if I have to, as it is currently not stacked, but sitting next to and wired up. I did this to keep the LCD from doing something to pin 23.

Bob

While I'm sure you are right about SD on SPI ,and I assume you have SD chipselect sorted, it is normal procedure to use

pinMode(53, OUTPUT);

with a Mega and I can't see that, so you might try it. I would be inclined to use pin 4 for SD select, just to stay with convention

I think

SPI.begin();

is redundant, but probably harmless.

Nick_Pyner:
While I'm sure you are right about SD on SPI ,and I assume you have SD chipselect sorted, it is normal procedure to use

pinMode(53, OUTPUT);

with a Mega and I can't see that, so you might try it. I would be inclined to use pin 4 for SD select, just to stay with convention

I think

SPI.begin();

is redundant, but probably harmless.

Yes, CS is working as the SDFile.print does work in the startup function, but for some odd reason it doesn't work in the loop function.