     delay(3000);
     lcd.clear();
   }
-  }
-
-
-
-
+  char filename[fileName.length()+1];
+  fileName.toCharArray(filename, sizeof(filename));
+  if (!SD.open(fileName, FILE_WRITE)) {
+    LCDwrite("Error opening", fileName);
+    while(1);
+  }
+  file.close();
+  LCDwrite("Writing to", fileName);
+  delay(3000);
+  lcd.clear();
+  }

   * multiply that by 0.1875, and divide that by 1000 to get voltage
   * in volts.
   */
  resultmV = cVoltage;
  resultV = cVoltage/1000;
}

void SDwrite() {
  resultFile = SD.open(fileName, FILE_WRITE);
  if(!resultFile){
    LCDwrite("Cannot open", fileName);
    delay(1000);
    }
  lcd.clear();
  resultFile.println(resultmV + ","); //writes to SD card
}

void writeResult(){ //Writes result to SD card and LCD Screen
  readADS(); //Reads data from ADC
  SDwrite(); //Write data to SD card
  LCDwrite((resultV + " V"), (resultmV + " mV ")); //Space keeps unnecessary "V" from appearing
}

void titleAndInitialize(){

SdFat SD; //Added for backwards compatability. This was suggested on the Arduino forum
//by user Scottrockcafe to user trilbytim (https://forum.arduino.cc/index.php?topic=231495.0).
//Create file for saving to SD Card
String fileName = "calibration.txt"; //Change as needed
#define SD_CS_PIN SS //Defines CS pin as SS for SD card
SdFile file;


//Set pins for MISO/MOSI/SS/PP
