The code checks to see if it can detect the microSD, then checks if the file is there. If it is, then it reports, otherwise it'll try to create the file and report again if it's there.
My output hasn't been successful output:
Code: Select all
Card failed, or not present
data.csv doesn't exist.
Creating data.csv...
data.csv doesn't exist.
Code: Select all
#include <SPI.h>
#include <SD.h>
// HSPI
#define HSPI_SCLK 14
#define HSPI_MISO 4
#define HSPI_MOSI 13
#define HSPI_CS 2
SPIClass * hspi = NULL;
String dataString =""; // holds the data to be written to the SD card
File sensorData;
void setup() {
hspi = new SPIClass(HSPI);
hspi->begin(HSPI_SCLK, HSPI_MISO, HSPI_MOSI, HSPI_CS); //SCLK, MISO, MOSI, SS
pinMode(HSPI_CS, OUTPUT); //HSPI SS
Serial.begin(115200);
// see if the card is present and can be initialized:
if (!SD.begin(HSPI_CS)) {
Serial.println("Card failed, or not present");
}
if (SD.exists("data.csv")) {
Serial.println("data.csv exists.");
}
else {
Serial.println("data.csv doesn't exist.");
}
// open a new file and immediately close it:
Serial.println("Creating data.csv...");
sensorData = SD.open("data.csv", FILE_WRITE);
sensorData.close();
// Check to see if the file exists:
if (SD.exists("data.csv")) {
Serial.println("data.csv exists.");
}
else {
Serial.println("data.csv doesn't exist.");
}
while(1);
}
void loop() {
}