SD: Error writing to file!

Michael_Link:
I'm using a simple example sketch to test a new SD module (CATALEX SD module) with an Uno. The Arduino successfully initializes the card reader but it can't write to the file. It looks like it must be happening in the "Void Save Data" section but I can't seem to figure it out. I have attached my code. Thanks for your help!

void saveData(){
if(SD.exists("data.csv")){ // check the card is still there
// now append new data file
  sensorData = SD.open("data.csv", FILE_WRITE);
  if (sensorData){
    sensorData.println(dataString);
    sensorData.close(); // close the file
    }
  }
else{
  Serial.println("Error writing to file !");
  }
}

Do you want only one line of data in the file? Every time you open the file, the FILE_WRITE options sounds good, but it is not one of the valid openFile() options.

You might want to review the oFlags. {from: \Program Files\Arduino\libraries\SD\src\utility\SdFile.cpp}

* \param[in] oflag Values for \a oflag are constructed by a bitwise-inclusive
 * OR of flags from the following list
 *
 * O_READ - Open for reading.
 *
 * O_RDONLY - Same as O_READ.
 *
 * O_WRITE - Open for writing.
 *
 * O_WRONLY - Same as O_WRITE.
 *
 * O_RDWR - Open for reading and writing.
 *
 * O_APPEND - If set, the file offset shall be set to the end of the
 * file prior to each write.
 *
 * O_CREAT - If the file exists, this flag has no effect except as noted
 * under O_EXCL below. Otherwise, the file shall be created
 *
 * O_EXCL - If O_CREAT and O_EXCL are set, open() shall fail if the file exists.
 *
 * O_SYNC - Call sync() after each write.  This flag should not be used with
 * write(uint8_t), write_P(PGM_P), writeln_P(PGM_P), or the Arduino Print class.
 * These functions do character at a time writes so sync() will be called
 * after each byte.
 *
 * O_TRUNC - If the file exists and is a regular file, and the file is
 * successfully opened and is not read only, its length shall be truncated to 0.
 */

I would probably specify :

  sensorData = SD.open("data.csv", O_WRITE|O_APPEND);

Chuck.