Hello guys, my problem is that when I insert an SD card into the SD shield socket, the LCD display goes haywire. It 's display goes blank. But when there is no SD card in the socket, the LCD displays the data from the pH sensor perfectly. I am using an Arduino Mega 2560 and a 16x2 LCD display. The connections I use for are LCD Pin 14- Pin 7 on Mega, LCD Pin 13- Pin 6, LCD Pin 12- Pin 5, LCD Pin 11- Pin 3. My connections for the SD shield is 53-10, 51-11, 52-13, and 50-12. I will include my code for reference.
#include <LiquidCrystal.h> //include LCD library
#include <SD.h>
long phReadInterval = 500;
long previousMillisPH = 0;
int PHState = HIGH;
float PH_Val;
const int chipSelect = 53; //SD Card
LiquidCrystal lcd(8,9,3,5,6,7);
String inputstring = ""; //a string to hold incoming data from the PC
String sensorstring = ""; //a string to hold the data from the Atlas Scientific product
boolean input_stringcomplete = false; //have we received all the data from the PC
boolean sensor_stringcomplete = false;
//have we received all the data from the Atlas Scientific product
void setup()
{ //set up the hardware
Serial.begin(9600); //for SD Card
Serial.begin(38400); //set baud rate for the hardware serial port_0 to 38400
Serial3.begin(38400); //set baud rate for software serial port_3 to 38400
inputstring.reserve(5); //set aside some bytes for receiving data from the PC
sensorstring.reserve(30); //set aside some bytes for receiving data from Atlas Scientific product
lcd.begin (16,2);
Serial.print("Initializing SD Card...");
pinMode(53, OUTPUT);
if (!SD.begin(chipSelect))
{
Serial.println("Card failed, or not present"); //Don't do anything more
return;
}
Serial.println("card initialized.");
}
void serialEvent()
{ //if the hardware serial port_0 receives a char
char inchar = (char)Serial.read(); //get the char we just received
inputstring += inchar; //add it to the inputString
if(inchar == '\r')
{
input_stringcomplete = true;
} //if the incoming character is a <CR>, set the flag
}
void serialEvent3()
{ //if the hardware serial port_3 receives a char
char inchar = (char)Serial3.read(); //get the char we just received
sensorstring += inchar; //add it to the inputString
if(inchar == '\r')
{
sensor_stringcomplete = true;
} //if the incoming character is a <CR>, set the flag
}
void loop(){ //here we go....
if (input_stringcomplete)
{ //if a string from the PC has been recived in its entierty
Serial3.print(inputstring); //send that string to the Atlas Scientific product
inputstring = ""; //clear the string:
input_stringcomplete = false; //reset the flage used to tell if we have recived a completed string from the PC
}
if (sensor_stringcomplete)
{ //if a string from the Atlas Scientific product has been recived in its entierty
Serial.println(sensorstring); //send that string to to the PC's serial monitor
sensorstring = ""; //clear the string:
sensor_stringcomplete = false; //reset the flage used to tell if we have recived a completed string from the Atlas Scientific product
}
getPHvalue();
lcd.setCursor(0,1);
lcd.print("PHvalue: ");
char charBuf[10];
sensorstring.toCharArray(charBuf, 10);
lcd.print(charBuf);
File dataFile = SD.open("datalog.csv", FILE_WRITE);
if (dataFile)
{
dataFile.println(charBuf);
dataFile.close();
}
else
{
Serial.println("error opening datalog.csv");
}
}
void getPHvalue()
{
unsigned long currentMillisPH = millis();
if(currentMillisPH - previousMillisPH > phReadInterval)
{
previousMillisPH = currentMillisPH;
if (PHState == HIGH)
{
PHState = LOW;
}
else
{
PHState = HIGH;
Serial.print("r\r"); // 'R' reads one value, see stamp doc
while (Serial.available() > 0) // in the serial stream. In this case on hardware serial 1
{ // Serial1.parseFloat looks for the next valid float number
PH_Val = Serial.parseFloat();
if (Serial.read() == '\r') // look for the carriage return. That's the end of your sentence:
{
Serial.println(PH_Val); // can be used for debug, pc display etc.
return;
}
}
}
}
}