SD card problem- card initialized but error opening datalog.txt

Hi there!

I'm working on a project where I have to read data from some sensors, converting it to digital using a shield, and store them on an SD card.
I'm using an arduino uno r3 and as IDE version 1.5.6.
Here is the code that I'm using (most of it is probably useless, but I posted it all. I use the SD card in the funcion at the very end):

#include <SPI.h>
#include <SD.h>

#define RESOLUTION 16

#define SCALE_FACTOR 0.000152587890625

//pins for shield
#define BUSY 3
#define RESET 4
#define START_CONVERSION 5 
#define SHcs 10
#define MISO 12
#define LED 13
//#define SDcs 6

#define TOTAL_RAW_BYTES RESOLUTION

//pins for stepper
const int stepPin = 9;
const int dirPin = 8;
//pins for SD
const int SDcs = 7;

int bytesToRead = TOTAL_RAW_BYTES;
byte raw[TOTAL_RAW_BYTES];
signed long parsed[8];
//long parsed[8];
char incomingChar;
int angle = 0;
int i;
int c;
int stepRes = 1420; //number of steps for a degree
int verticalRes = 5; //degrees for calibration vertical
int verticalRange = 45;
int posHor = 0;
float posVer = verticalRange;
String content = "";
char character;

 
void setup() {
   
  pinMode(BUSY, INPUT);
  pinMode(RESET, OUTPUT);
  pinMode(LED, OUTPUT);
  pinMode(START_CONVERSION, OUTPUT);
  pinMode(MISO, INPUT);
  pinMode(SHcs,OUTPUT);
  pinMode(stepPin,OUTPUT);
  pinMode(dirPin,OUTPUT);
  pinMode(SDcs,OUTPUT);
  
  //Bridge.begin();
  Serial.begin(115200);
  //FileSystem.begin();
  SPI.begin();
  
  digitalWrite(START_CONVERSION, HIGH);  
  digitalWrite(SHcs, HIGH);
  digitalWrite(RESET, HIGH);
  delay(1);
  digitalWrite(RESET, LOW);
  
  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  //SD.begin(SDcs);
  // see if the card is present and can be initialized:
  if (!SD.begin(SDcs)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
  // attachInterrupt(0, func, CHANGE);
}
 
void loop() {
  while(!Serial);
  Serial.println("ready."); 
  Serial.print("Position: vertical: ");
  Serial.print(posVer);
  Serial.print(",  orizontal: ");
  Serial.print(posHor);
  Serial.print("\n");
  Serial.print("Vertical resolution");
  Serial.print(verticalRes);
  Serial.print("\n");
  Serial.print("Vertical range");
  Serial.print(verticalRange);
  Serial.print("\n");
  Serial.println("Try 10 meas: t ); 
  while(Serial.available() == 0){}
  // read the incoming byte:
  incomingChar = Serial.read(); 
  Serial.println(incomingChar); 
  
 if (incomingChar == 't') {
    Serial.println("Taking 10 measurments: ");
    for (int x=0; x<10; x++){
      reading();
      delay(500);
    } 
  } 
}
  
void parseRawBytes() {
  int i;
 
// 16-bit code not tested yet
      parsed[0] = (raw[0] << 8) + (raw[1] >> 0);
      parsed[1] = (raw[2] << 8) + (raw[3] >> 0);
      parsed[2] = (raw[4] << 8) + (raw[5] >> 0);
      parsed[3] = (raw[6] << 8) + (raw[7] >> 0);
      parsed[4] = (raw[8] << 8) + (raw[9] >> 0);
      parsed[5] = (raw[10] << 8) + (raw[11] >> 0);
      parsed[6] = (raw[12] << 8) + (raw[13] >> 0);
      parsed[7] = (raw[14] << 8) + (raw[15] >> 0);

 
  for(i=0; i<8; i++) {
    parsed[i] = fixSignBit(parsed[i]);
  }
}
 
long fixSignBit(long reading) {
 
// 16-bit code not tested yet
      if(reading & 0x8000) { // if reading is < 0 (stored as two's complement)
        return reading | 0xFFFF0000; // set bits 31-16
      } else {
        return reading;
      }
}

int readInt() {
  while(Serial.available() == 0){}
  String readString; 
  int n;
  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the string readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }
  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string 
    n = readString.toInt();  //convert readString into a number
  }    

  return n;
}

String readSt() {
  while(Serial.available() == 0){}
  while(Serial.available()) {
      character = Serial.read();
      content.concat(character);
  }
  return content;
}

void stepping(long S){
    Serial.println("Stepping..."); 
    // Prepare to take some steps
    for (int i=0; i<(S); i++){
        // to be reorganized for stap proper angle
        digitalWrite(stepPin, HIGH);
        delayMicroseconds(100);
        //delay(1);
        digitalWrite(stepPin, LOW);
        delayMicroseconds(100);
        //delay();
        }

}

void reading(){
  digitalWrite(START_CONVERSION, LOW);
  delayMicroseconds(10);
  digitalWrite(START_CONVERSION, HIGH);
  digitalWrite(LED, HIGH);
  while (digitalRead(BUSY) == HIGH) {
    // wait for conversion to complete
  }
  digitalWrite(LED, LOW);
  digitalWrite(SHcs, LOW);
  while (bytesToRead > 0) {
    raw[TOTAL_RAW_BYTES - bytesToRead] = SPI.transfer(0x00);
    bytesToRead--;
  }
  digitalWrite(SHcs, HIGH);
  bytesToRead = TOTAL_RAW_BYTES;
  parseRawBytes();
  String dataString;
  dataString += millis();
  dataString += ",";
  dataString += posHor;
  dataString += ",";
  dataString += (int)posVer;
  dataString += ",";
  for(i=0; i<8; i++) {
    //dataString += parsed[i], 5;
    dataString += (float)parsed[i] * SCALE_FACTOR * 1000, 5;
    dataString += ",";
  }
  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:
  }  
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  } 
  
  Serial.println(dataString);
  digitalWrite(LED, HIGH);
  delay(30);
}

The card is initialized in the setup, so I guess the wiring it's correct, but when I try to write on it in the function I get an error. If I then look on the card the file "datalog.txt" has been created, but it is empty.

I also tried to run the datalogging example I found on the Arduino website (http://arduino.cc/en/Tutorial/Datalogger), and that works, so I guess the problem must be in the code, I just can't really figure out what I am doing wrong.

Here is what I can see if I run the card info sketch always from the website (http://arduino.cc/en/Tutorial/CardInfo):

nitializing SD card...Wiring is correct and a card is present.

Card type: SDHC
Volume type is FAT32
Volume size (bytes): 3657433088
Volume size (Kbytes): 3571712
Volume size (Mbytes): 3488
Files found on the card (name, date and size in bytes):

Can anybody help me whit this??

I am really struggling.

Andrea.

  digitalWrite(LED, HIGH);

Your LED is connected to pin 13. An a 328-based Arduino, that is one of the SPI pins. You can NOT use this pin for other things, since the SD shield uses SPI to communicate with the Arduino.

Hi Pauls,

Thanks for the instantaneous reply, I tryed youir suggestion and it didn't solve my problem, but I'm sure that was another issue to my code.
The main issue was excessive RAM usage by the strings, I solved it usinf F() macros as suggested in this post:
SD card does not open file - Storage - Arduino Forum.

Thanks again for your help!