I'm having difficulty with SD cards that I can't figure out. Must be me (of course) but please, someone point me in the right direction....
Actually, I got two problems :
The first problem is, my arduino hangs when there's no card plugged in
I thought it was my own code, so I tried the basic example code from the SD library :
#include <SD.h>
const int chipSelect = 4;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
}
void loop()
{
// make a string for assembling the data to log:
String dataString = "";
// read three sensors and append to the string:
for (int analogPin = 0; analogPin < 3; analogPin++) {
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 2) {
dataString += ",";
}
}
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
// more stuff
}
teh serial printstring NOR the "error opening datalog.txt" EVER comes out of the Serial monitor, if there's no card placed... Nor does any of the following "more stuff" code run (not even lighting a led)... the loop just ends there, hangs forver, right at SD.Open ....
So I thought : this must be a wiring error, but I cheked it and it looks fine, then I inserted a card...
And it works now ! , on one condition : I have to make an empty file "datalog.txt" on teh card first with my PC. ( it was there allready when I first tested, but when I emptied the card later i discovered it failed again, untill I put the file back there)
If the card is 100% clean : code hangs
BUT ! The arduino doesn't write to the file I made ! it makes a second file "DATALOG" without ".txt" to which it writes henceforth.....
And Second problem : the file then looks like this : ( lets assume I'm printing a string every second "1-2-3-4-5")
1-2-3-4-5
1-2-3-4-5
1-2-3-4-51-2-3-4-5
-2-3-4-51-2-3-4-
1-2-3-4-51-2-3-4-51-2-3-4-5
1-2-3-4-5
1-2-3-4-53-4-5 1-2-3-4-5
WTF can cause THAT ? running serial monitor shows me a nice correct list of the string.... so it's printing the strngs correctly, just not writing them correctly... the first characters go missing and the line breaks are missing sometimes
Any ideas on where the problem may lie? What should I look for?