Been tinkering with the SdFat library and long file names. It works, but it writes the data twice to the card. Why is this and how do I resolve this? This is the code as of now. Using a Nano with PlatformIO on VSCode.
EDIT: The SD card is FAT32 formatted.
#include <Arduino.h>
#include <SPI.h>
#include <SdFat.h>
#define SD_CS_PIN 10
SdFat SD;
File dataFile;
void setup()
{
// Open serial comms
Serial.begin(9600);
Serial.println("\nInitializing SD card...");
if (!SD.begin(SD_CS_PIN))
{
Serial.println(" failed!");
return;
}
else
{
Serial.println(" done!");
}
String fileName;
fileName = "GPSData_";
fileName += String("01");
fileName += String("01");
fileName += String("2024");
fileName += String("_");
fileName += String("20");
fileName += String("45");
fileName += String("33");
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.
dataFile = SD.open(fileName, FILE_WRITE);
// if the file opened okay, write to it:
if (dataFile)
{
Serial.println("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, 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();
}
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);
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();
}
else
{
// if the file didn't open, print an error:
Serial.println("Error opening file for reading!!");
}
}
void loop()
{
// nothing happens after setup
}