6

I'm trying to datalog to a EyeFi SD card by writing to a file with a jpg extension. Here's that portion of my code so far:

// see if the directory exists, create it if not.
if( !SD.exists("/DCIM/100NIKON") ) 
{
  if( SD.mkdir("/DCIM/100NIKON") ) 
  {
    Serial.print("File directory created: ");
  } 
  else {
    error("File directory not created");
  }
}
else
{
  Serial.print("File directory exists: ");
}

// Test file
char filename[] = "DSCN0000.JPG";

if (! SD.exists(filename))
{
  logfile = SD.open(filename, FILE_WRITE); // only open a new file if it doesn't exist
}

if (! logfile)
{
  error("Couldnt create file");
}

The Arduino creates the directory, but the file is still saved to the root. I would love some help or tips on this.

user2218339
  • 219
  • 3
  • 12

1 Answers1

5

You're creating a directory, and then not writing the file into it.

You need to change the filename to /DCIM/100NIKON/DSCN0000.JPG";

Note that SD.open() takes the full filepath to the file, not just the file*name*.

From the Arduino Docs:

The file names passed to the SD library functions can include paths separated by forward-slashes, /, e.g. "directory/filename.txt". Because the working directory is always the root of the SD card, a name refers to the same file whether or not it includes a leading slash (e.g. "/file.txt" is equivalent to "file.txt"). As of version 1.0, the library supports opening multiple files.

(emphasis mine)

Connor Wolf
  • 2,724
  • 13
  • 16