Simpel Example to save data to your sd card. The file is semicollumn delimited ( ; )
#include <SPI.h>
#include <SD.h>
//#define CS_SD 4
#define seperator ";" //Seperator of the collumns
#define HEADERSTRING "Collumn1;Collumn2;Collumn3" //First line of the file showing headers
//Initializing the sd card reader
bool storeMemoryCardinit()
{
if (SD.begin(SS_2)) {
SerialUSB.println("Card ok");
return true;
}
else{
SerialUSB.println("Card failed, or not present");
return false;
}
}
//Store data on the sd card, first operator is the data to be saved, second operator is the name of the file to be writen to.
void storeSD(String dataString, String DataFileName)
{
File dataFile;
dataFile = SD.open(DataFileName, FILE_WRITE); //Try opening the file
if(!dataFile){ //If it cant find or create de file
SerialUSB.println("couldn't open SD-card...trying to reinitialize");
if(storeMemoryCardinit()) //trying to reinitialize the cd card
{
dataFile = SD.open(DataFileName, FILE_WRITE); //trying to create or open the file again
}
else
{
SerialUSB.println("Something is going wrong");
return;
}
}
if (dataFile.size() < 10) //If it is the first line writen
{
//dataFile.println(String(HEADERSTRING));
dataFile.println("\"sep="+(String)seperator+"\""); //Write Seperator for excel
dataFile.println(HEADERSTRING); //Write the Collumn names
SerialUSB.println("Writing the headers");
}
dataFile.println(dataString); //Writing the data
SerialUSB.println("Stored:" + String(dataString));
dataFile.close(); //Closing the file
}
void setup() {
// put your setup code here, to run once:
delay(1000);
SerialUSB.println("Starting Serial");
SerialUSB.begin(9600);
delay(10000);
SerialUSB.println("Initializing Card");
storeMemoryCardinit();
SerialUSB.println("Beginning loop");
}
void loop() {
storeSD("Data1;Data2;Data3", "Test7.csv");
delay(5000);
}