Advertisement
electrotwelve

SdFat Xiao

Jan 18th, 2024
588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Arduino.h>
  2. #include <SPI.h>
  3. #include <SdFat.h>
  4.  
  5. #define SD_CS_PIN 3
  6. #define LEDPIN 4
  7.  
  8. SdFat SD;
  9. File dataFile;
  10.  
  11. void setup()
  12. {
  13.   pinMode(LEDPIN, OUTPUT);
  14.   // Open serial communications and wait for port to open:
  15.   Serial.begin(9600);
  16.   digitalWrite(LEDPIN, HIGH);
  17.  
  18.   Serial.print("\nInitializing SD card...");
  19.  
  20.   if (!SD.begin(SD_CS_PIN))
  21.   {
  22.     Serial.println(" failed!");
  23.     return;
  24.   }
  25.   else
  26.   {
  27.     Serial.println(" done!");
  28.   }
  29.  
  30.   String fileName;
  31.  
  32.   fileName = "GPSData_";
  33.   fileName += String("02");
  34.   fileName += String("01");
  35.   fileName += String("2024");
  36.   fileName += String("_");
  37.   fileName += String("15");
  38.   fileName += String("10");
  39.   fileName += String("00");
  40.   fileName += String(".kml");
  41.  
  42.   Serial.print("File name is: ");
  43.   Serial.println(fileName);
  44.  
  45.   // open the file. note that only one file can be open at a time,
  46.   // so you have to close this one before opening another.
  47.   // (O_READ | O_WRITE | O_CREAT) erases any previous content and then writes.
  48.   // FILE_WRITE is (O_RDWR | O_CREAT | O_AT_END)
  49.   dataFile = SD.open(fileName, FILE_WRITE);
  50.  
  51.   // if the file opened okay, write to it:
  52.   if (dataFile)
  53.   {
  54.     Serial.print("Writing to file...");
  55.  
  56.     dataFile.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  57.     dataFile.println("<kml xmlns=\"http://www.opengis.net/kml/2.2\">");
  58.     dataFile.println("");
  59.     dataFile.println("  <Document>");
  60.     dataFile.println("    <name>Views with Time</name>");
  61.     dataFile.println("    <open>1</open>");
  62.     dataFile.println("    <description>");
  63.     dataFile.println("      In Google Earth 20, enable historical imagery and sunlight,");
  64.     dataFile.println("      then click on each placemark to fly to that point in time.");
  65.     dataFile.println("    </description>");
  66.     dataFile.println("");
  67.     dataFile.println("    <Placemark>");
  68.     dataFile.println("      <name>Sutro Baths in 1946</name>");
  69.     dataFile.println("      <gx:TimeStamp>");
  70.     dataFile.println("        <when>1946-07-29T05:00:00-08:00</when>");
  71.     dataFile.println("      </gx:TimeStamp>");
  72.     dataFile.println("      <longitude>-122.518172</longitude>");
  73.     dataFile.println("      <latitude>37.778036</latitude>");
  74.     dataFile.println("      <altitude>221.0</altitude>");
  75.     dataFile.println("    </Placemark>");
  76.     dataFile.println("");
  77.     dataFile.println("  </Document>");
  78.     dataFile.println("</kml>");
  79.  
  80.     dataFile.close();
  81.     Serial.println(" done!");
  82.   }
  83.   else
  84.   {
  85.     // if the file didn't open, print an error:
  86.     Serial.println("Error opening file for writing!!");
  87.   }
  88.  
  89.   // re-open the file for reading:
  90.   Serial.print("\n\nReading from file: ");
  91.   Serial.println(fileName);
  92.   Serial.println("");
  93.  
  94.   dataFile = SD.open(fileName, FILE_READ);
  95.   if (dataFile)
  96.   {
  97.     // read from the file until there's nothing else in it:
  98.     while (dataFile.available())
  99.     {
  100.       Serial.write(dataFile.read());
  101.     }
  102.     // close the file:
  103.     dataFile.close();
  104.     Serial.println("\n File read!");
  105.   }
  106.   else
  107.   {
  108.     // if the file didn't open, print an error:
  109.     Serial.println("Error opening file for reading!!");
  110.   }
  111.   Serial.end();
  112. }
  113.  
  114. void loop()
  115. {
  116.   // nothing happens after setup
  117. }
  118.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement