SD write problems

Hi all,

I'm having problems with writing data to my sd card. I'm using the arduino uno and the sd shield in arduino 1.0. I'm trying to log the data from an accelerometer (MMA7455). Everything worked good for a while but then I started to have problems with the SD card shield. It seemed to crash after it saved a few values in the file. I don't know what could be the problem. I thought, maybe, it could be a connection problem between my usb port and the arduino but I powered it using a power adapter and I had the same problem. Another thing that I think may be the problem is that the accelerometer is spitting out data too fast for it to be saved to the file. Do you guys have any idea of what could be my problem?

I have attached the file which it creates. the file contains multiple runs of the code. You can see where it crashes by the garbage it writes.

Thanks in advance.

Heres the code

#include <Wire.h>
#include <SD.h>


#define ACCELEROMETER 0x1D //Address for Accelerometer

//REGISTERS
#define MODE_CONTROL  0x16 //Mode control register
#define PULSE_DET     0x1B //Pulse detection threshold limit value
#define X_OUT         0x06 //8 bit register containing value for X
#define Y_OUT         0x07 //8 bit register containing value for Y
#define Z_OUT         0x08 //8 bit register containing value for Z
#define DETECTION     0x0A //Detection source register

//VALUES
#define Z_PULSE       0x40 //Pulse detected on Z-axis
#define SENSEVALUE    0x25 //Default sensitivity level

File myFile;

//required setup function
void setup() {
  Wire.begin();
  Serial.begin(9600);
  accWrite(MODE_CONTROL, SENSEVALUE);
  
  
                                       
  Serial.print("Initializing SD card...."); //getting SD card ready
 
  pinMode(10, OUTPUT);

  
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
   Serial.println("initialization done.");
  
   myFile = SD.open("accellog.csv", FILE_WRITE);
  

    // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to accel_log.csv ");
    String header = "xVal, yVal, zVal";
    myFile.println(header);
	// close the file:
    myFile.close();
    Serial.println("header");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening file");
  }
  
  /* re-open the file for reading:
  myFile = SD.open("accel_log.txt");
  if (myFile) {
    Serial.println("accel_log.txt:");
    
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
    	Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
  	// if the file didn't open, print an error:
    Serial.println("error opening accel_log.txt");
  }*/
}
  
  

//required loop function
void loop() {
 // if(button == HIGH) BEGIN BUTTON PRESS LOOP
  //{
    
    //Data string for storing to SD card
  // Use CSV format
  
  
    
  /*Serial.print("X: ");
  Serial.print(accRead(X_OUT));
  Serial.print("   Y: ");
  Serial.print(accRead(Y_OUT));
  Serial.print("   Z: ");
  Serial.println(accRead(Z_OUT));
  delay(100);
  */
  String dataString =  String(accRead(X_OUT)) + "," + String(accRead(Y_OUT)) + "," + String(accRead(Z_OUT));
  
 File myFile = SD.open("accellog.csv", FILE_WRITE);
  if (myFile)
  {
    myFile.println(dataString);
    myFile.close();
    Serial.println(dataString);
   
  }
  
  else
  {
    Serial.println("Could not open file");
    
  }
  delay(50);
}
//} END OF BUTTON PRESS LOOP
//function to write byte data into a register
void accWrite(byte address, byte data) {
  Wire.beginTransmission(ACCELEROMETER);
  Wire.write(address);
  Wire.write(data);
  Wire.endTransmission();
}

//function to read byte data from a register
unsigned int accRead(byte address){ 
  byte val = 0x00;
  Wire.beginTransmission(ACCELEROMETER);
  Wire.write(address);
  Wire.endTransmission();
  Wire.requestFrom(ACCELEROMETER, 1);
  if(Wire.available() > 0) {
    val = Wire.read();
  }
  Wire.endTransmission();
  return val;
}

// 	MMA 7455 pin
//	1 - VIN to Arduino 3.3V
//	5 - GND, ground to Arduino GND
//	6 - Chip Select - connect this to 5V with a 1K resistor, this selects I2C mode
//	7 - Data - connect this to the ATmega's SDA (data line) on analog input pin 4, with 4.7K pull up resistor
//	8 - Clock - connect this to ATmega's SCL (clock line) on analog input pin 5, with 4.7K pull up resistor

ACCELLOG.CSV (3.22 KB)

You are almost certainly running out of memory. The String class is particularly known for that. Here for example:

    String header = "xVal, yVal, zVal";
    myFile.println(header);

Why not just do this?

    myFile.println("xVal, yVal, zVal");

And instead of:

  String dataString =  String(accRead(X_OUT)) + "," + String(accRead(Y_OUT)) + "," + String(accRead(Z_OUT));
...
    myFile.println(dataString);

You could do:

myFile.print (accRead(X_OUT));
myFile.print (",");
myFile.print (accRead(Y_OUT));
myFile.print (",");
myFile.println (accRead(Z_OUT));

Thanks. That sure fixed my problem.

Thanks so much for the post. Got me sorted.