SD card writing

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?

Hi,
Let us know some more details such as;
Arduino IDE version,
Type of Arduino,
Also the condition of your SD card, did you try with it freshly formatted to say FAT16 ?

Things to try if you haven't already;
Prove that that Arduino will communicate well with just a simple serial sketch first with no usual output, checking your serial parameters are correct,
Then try the same program above with the SD card inserted.

Rockwallaby . . .

IDE Last one before 1.0 (not home now so can't check exactly which 0.9 version it is , will update this post)
Mega 2560
yep clean format (twice !, fat16)

the test you describe is exactly what I did, above.

printing "datastring" to the serial monitor works fine

Sd.printing leads to teh above story

Ok, for Mega you need to change the pin 10 to pin 53.
I did the same thing today on my Freetronics Ether-Mega, works great.

Also, if you are using the Arduino ver prior to ver 1.0, say 023 you may wish to upgrade to 1.0.1.
I asked that question as I was wondering if you had copied code from the Arduino site and pasted it into your older version IDE.

Change your pinMode to be 53 instead of 10, and hopefully you'll have it working nice.
Good luck

rockwallaby . .

it is 53, as I said this example of code comes from the site here ... of course i adapted the pins to mega...

If it weren't, it would NEVER work would it, like i said, it works fine for 4 iterations of the loop, then goes haywire

Och christ never mind I'll find it myself, or just cancel that part....

Sorry, my eyes must be blurry, too much computer work today.
But can you tell me where you have set the pinMode to be 53 other that 10 or where you have mentioned it?
When I look in you code section I am sure I see the following

   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);

I guess with that last comment of yours, you are welcome, I shall leave you to it then.

rockwallaby . .