Ethernet Shield (SD Slot) & LCD combined problems

Hello all.

So I am working on a project, with a number of functions.

I have encountered a problem where when I use the LCDDisplay() function and the LogData() function together, the data does not get recorded in the SD card.

I’ve tested them both seperatly in my main code and they both work, but when using them together they don’t.

I am not sure what the problem is but any help would be greatly appreciated.

I am using the Adruino Mega and the Adruino Ethernet shield. The LCD screen is a serLCD.

#include <SD.h>

// for measuring time.
unsigned long time;
unsigned long timer=0;
unsigned long seconds;

// On the Ethernet Shield, CS is pin 4. 
const int chipSelect = 4;

// Variable for reading the pushbutton status
int buttonState1 = 0; 
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int Button; 

// Variables for calculated values to be displayed on LCD
float celcius;
float ph;
float oxygen;
float yeast;

// Variables for raw sensor inputs
int tempRaw = 0;
int pHRaw = 0;
int Turbidity = 0;
int yeastRaw = 0;

// Variables for temperature sensor and calculations 
int i = 0;
int n;
int sample = 30;
float tempRawArray[30];
float averagetempRaw;
float sumtempRaw;
float referenceVoltage;
float volts;
float voltDivisor;
float millivolts;
float kelvin;
int x;

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600); 
  referenceVoltage = 5000;
  
  // initialize the pushbutton pins (24,26,28,30) as inputs:
  pinMode(24, INPUT); 
  pinMode(26, INPUT);
  pinMode(30, INPUT);
  pinMode(32, INPUT); 
  
  // Ethernet Shield Initialization
  int analogPin = 2; 
  
    Serial.print("Initializing SD card...");

  pinMode(53, OUTPUT);
  
  // see if the card is present and can be initialized:
  SD.begin(chipSelect);

}

void loop() 
{
//Timer for data logging every 10 seconds
    time = millis();

  if(time == (timer+10000)){

  timer = millis();
  seconds = time/1000;
  LogData();

    }

  
 buttonStateFunction();
 //LCDDisplay();
  phSense();
 TemperatureSense();

  



}



//////////////////////////////////////
// Temperature Sensor Functions
/////////////////////////////////////

void TemperatureSense() {
  
    tempRaw = analogRead(A0);    
  

  //Arduino analogReads in a value 0-1024
  voltDivisor = 1024/referenceVoltage;
  kelvin = millivolts/10;
  volts = millivolts / 1000;
  celcius = kelvin - 273.15; 

}

void TempControl (){
  int TempInput = analogRead(A0);
      if(TempInput > 560){
    digitalWrite(13, HIGH);
  }
  else
  {
    digitalWrite(13, LOW);
  }
}

//////////////////////////////////////
// p.H. Sensor Functions
/////////////////////////////////////

void phSense(){
 
  pHRaw = analogRead(A1);
  
  }

//////////////////////////////////////
// Conductance Functions
/////////////////////////////////////

void yeast1Sense(){
 
  Turbidity = analogRead(A2);
  
  }

//////////////////////////////////////
// Turbidity Sensor Functions
/////////////////////////////////////

void yeast2Sense(){
 
  yeastRaw = analogRead(A3);
  
  }

//////////////////////////////////////
// Buttons Functions
/////////////////////////////////////

void buttonStateFunction() { //Check which button is being pushed

  // Check if buttons have been pushed
  int buttonState1 = digitalRead(24);
  int buttonState2 = digitalRead(26);
  int buttonState3 = digitalRead(30);
  int buttonState4 = digitalRead(32);

  // Check which button has been pushed and assign a variable to it 
if (buttonState1 == HIGH) {
  Button = 1;
    }
else if(buttonState2 == HIGH) {
  Button = 2;
    }
else if(buttonState3 == HIGH) {
  Button = 3;
    }
else if(buttonState4 == HIGH) {
  Button = 4;
    }

}

//////////////////////////////////////
// Data Logging
/////////////////////////////////////

void LogData()
{
  // make a string for assembling the data to log:
  // Temperature
  String dataString1 = ""; 
  dataString1 += String(tempRaw);
  
  // p.H.
  String dataString2 = ""; 
  dataString2 += String(pHRaw);
  
  // Yeast 1
  String dataString3 = ""; 
  dataString3 += String(Turbidity);
  
  // Yeast 2
  String dataString4 = ""; 
  dataString4 += String(yeastRaw);
  
  // Timer
  String dataString5 = ""; 
  dataString5 += String(seconds);
  
  // 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("TempData.txt", FILE_WRITE);
    dataFile.print("Temperature = ");
    dataFile.println(dataString1);
    dataFile.close();
    
  File dataFile1 = SD.open("pHData.txt", FILE_WRITE);
    dataFile1.print("pH = ");
    dataFile1.println(dataString2);
    dataFile1.close();
    
  File dataFile2 = SD.open("TurbidityData.txt", FILE_WRITE);
    dataFile2.print("Turbidity = ");
    dataFile2.println(dataString3);
    dataFile2.close();
    
  File dataFile3 = SD.open("YeastData.txt", FILE_WRITE);
    dataFile2.print("Yeast Levels = ");
    dataFile2.println(seconds);
    dataFile2.close();

}

//////////////////////////////////////
// SerLCD Functions
/////////////////////////////////////

void LCDDisplay() { //Decide what to display on the LCD

switch (Button) {
  case 1:
 // clearLCD();
  selectLineOne();
    Serial.print("Temperature    ");
  selectLineTwo();  
    Serial.print(tempRaw);
    Serial.print(" ");
    Serial.print("Celcius        ");
   break;
  
  case 2:
  //clearLCD();
  selectLineOne();
    Serial.print("p.H.            ");
  selectLineTwo();  
    Serial.print(pHRaw);
    Serial.print("                ");
    break; 
    
  case 3:
  //clearLCD();
  selectLineOne();
    Serial.print("Turbidity       ");
  selectLineTwo();  
    Serial.print(Turbidity);
    Serial.print("               ");
  break;
  
  case 4:
  selectLineOne();
    Serial.print("Volume Fraction");
  selectLineTwo();  
    Serial.print("Of Yeast");
    Serial.print(" ");
    Serial.print(Turbidity);
    Serial.print("               ");
  break;

  default: 
  selectLineOne();
    Serial.print("Please select");
  selectLineTwo();  
    Serial.print("what to display");
    delay(4000);
    clearLCD();
  selectLineOne();
    Serial.print("Button 1 for");
  selectLineTwo();
    Serial.print("Temperature");
    delay(3000);
    clearLCD();
  selectLineOne();
    Serial.print("Button 2 for");
  selectLineTwo();
    Serial.print("p.H. levels");
    delay(3000);
    clearLCD();  
  selectLineOne();
    Serial.print("Button 3 for");
  selectLineTwo();
    Serial.print("Turbidity");
    delay(3000);
    clearLCD();
  selectLineOne();
    Serial.print("Button 4 for");
  selectLineTwo();
    Serial.print("Yeast Levels");  
  delay(3000);
  //clearLCD();  
  break;
  }  
}

void selectLineOne(){  //puts the cursor at line 0 char 0.
   Serial.print(0xFE, BYTE);   //command flag
   Serial.print(128, BYTE);    //position
}
void selectLineTwo(){  //puts the cursor at line 0 char 0.
   Serial.print(0xFE, BYTE);   //command flag
   Serial.print(192, BYTE);    //position
}
void goTo(int position) { //position = line 1: 0-15, line 2: 16-31, 31+ defaults back to 0
if (position<16){ Serial.print(0xFE, BYTE);   //command flag
              Serial.print((position+128), BYTE);    //position
}else if (position<32){Serial.print(0xFE, BYTE);   //command flag
              Serial.print((position+48+128), BYTE);    //position 
} else { goTo(0); }
}

void clearLCD(){
   Serial.print(0xFE, BYTE);   //command flag
   Serial.print(0x01, BYTE);   //clear command.
}