Hi All,
I'm trying to do something seemingly simple but I get the following error every time I insert the card.
- initialization failed!
- Couldn't create file...
The line below gives me 1 when inserted 0 when it isn't.
reading = digitalRead(sdInPin);
What am I doing wrong?
Thanks in advance,
Charles
#include <SPI.h>
#include <SD.h>
// --------- GENERAL -----------
int sampleTime = 10;
long int previous = 0;
long int startTime = 0;
int sdInPin = 2;
boolean starting = true;
boolean reading = false;
File dataFile;
const int chipSelect = 8;
void setup() {
Serial.begin(115200);
}
void loop() {
reading = digitalRead(sdInPin);
if (reading) {
if (starting) {
Serial.print("Starting: ");
Serial.println(starting);
loadSd();
startTime = millis();
starting = false;
}
if (millis() - previous > sampleTime) {
String dataString = "";
// Add all the data to dataString
Serial.println(dataString);
dataFile.println(dataString);
dataFile.flush();
previous = millis();
}
} else if (starting == false) {
starting = true;
}
}
void loadSd() {
pinMode(SS, OUTPUT);
if (!SD.begin()) {
Serial.println("initialization failed!");
}
// create a new numbered file
char filename[] = "PHEET00.CSV";
for (uint8_t i = 0; i < 100; i++) {
filename[5] = i / 10 + '0';
filename[6] = i % 10 + '0';
if (! SD.exists(filename)) {
// only open a new file if it doesn't exist
dataFile = SD.open(filename, FILE_WRITE);
break; // leave the loop!
}
}
dataFile = SD.open(filename, FILE_WRITE);
if (!dataFile) {
Serial.println("Couldn't create file...");
}
}