Ignore my previous comment. I think I have this figured out. I read the documentation listed here. I tried the code listed here with the SD library and everything worked! Also using the Arduino fork of the SdFat library.
I then used the same serial and SD init code with my version and it worked!
I think by default the CS pin is D4.
However, this can be changed to whatever you like in the code. I believe the reference code waits for serial to initialize. Though I cannot figure out why my SD init code did not work earlier but works now.
My version of the working code is below:
#include <Arduino.h>
#include <SPI.h>
#include <SdFat.h>
#define SD_CS_PIN 2
#define LEDPIN 4
#define SERIALBAUD 9600
SdFat SD;
File dataFile;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(SERIALBAUD);
while (!Serial)
{
;
}
Serial.println("Hello Xiao!");
Serial.print("Initializing SD card...");
if (!SD.begin(SD_CS_PIN))
{
Serial.println(" failed!");
while (1)
{
;
}
}
else
{
Serial.println(" done!");
}
String fileName;
fileName = "GPSData_";
fileName += String("19");
fileName += String("01");
fileName += String("2024");
fileName += String("_");
fileName += String("10");
fileName += String("45");
fileName += String("00");
fileName += String(".kml");
Serial.print("File name is: ");
Serial.println(fileName);
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
// (O_READ | O_WRITE | O_CREAT) erases any previous content and then writes.
// FILE_WRITE is (O_RDWR | O_CREAT | O_AT_END)
dataFile = SD.open(fileName, FILE_WRITE);
// if the file opened okay, write to it:
if (dataFile)
{
Serial.print("Writing to file...");
dataFile.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
dataFile.println("<kml xmlns=\"http://www.opengis.net/kml/2.2\">");
dataFile.println("");
dataFile.println(" <Document>");
dataFile.println(" <name>Views with Time</name>");
dataFile.println(" <open>1</open>");
dataFile.println(" <description>");
dataFile.println(" In Google Earth 20, enable historical imagery and sunlight,");
dataFile.println(" then click on each placemark to fly to that point in time.");
dataFile.println(" </description>");
dataFile.println("");
dataFile.println(" <Placemark>");
dataFile.println(" <name>Sutro Baths in 1946</name>");
dataFile.println(" <gx:TimeStamp>");
dataFile.println(" <when>1946-07-29T05:00:00-08:00</when>");
dataFile.println(" </gx:TimeStamp>");
dataFile.println(" <longitude>-122.518172</longitude>");
dataFile.println(" <latitude>37.778036</latitude>");
dataFile.println(" <altitude>221.0</altitude>");
dataFile.println(" </Placemark>");
dataFile.println("");
dataFile.println(" </Document>");
dataFile.println("</kml>");
dataFile.close();
Serial.println(" done!");
}
else
{
// if the file didn't open, print an error:
Serial.println("Error opening file for writing!!");
}
// re-open the file for reading:
Serial.print("\n\nReading from file: ");
Serial.println(fileName);
Serial.println("");
dataFile = SD.open(fileName, FILE_READ);
if (dataFile)
{
// read from the file until there's nothing else in it:
while (dataFile.available())
{
Serial.write(dataFile.read());
}
// close the file:
dataFile.close();
Serial.println("\n File read!");
}
else
{
// if the file didn't open, print an error:
Serial.println("Error opening file for reading!!");
}
// Close the serial port
Serial.end();
}
void loop()
{
// nothing happens after setup
}