Thread Tools
Sep 15, 2019, 11:03 AM
Registered User
vollrathd's Avatar
Thread OP
Help!

Arduino SD memory question


I'm working on using the Arduino Nano with a common SD card and have it working. One command for the SD card is creating a new directory, and automatically creating a txt file and writing to it:

What I'd like to do is have the Arduino Nano create a new directory automatically each time the Arduino Nano is powered up

Here is the current sketch:

//this command creates "Test" directory
SD.mkdir("/Test") ;
//this command creates the /test/sample.txt file
File dataFiles = SD.open("/test/sample.txt",FILE_WRITE);


Here is what I'd like to do each time the Arduino Nano is powered up

//this command creates "Test" directory
SD.mkdir("/Test1") ;
//this command creates the /test/sample.txt file
File dataFiles = SD.open("/test1/sample.txt",FILE_WRITE);



The next time the Arduino Nano with SD card is powered up we create this test2 directory
//this command creates "Test" directory
SD.mkdir("/Test2") ;
//this command creates the /test/sample.txt file
File dataFiles = SD.open("/test2/sample.txt",FILE_WRITE);



The next time the Arduino Nano with SD card is powered up we create this test3 directory

//this command creates "Test" directory
SD.mkdir("/Test3") ;
//this command creates the /test/sample.txt file
File dataFiles = SD.open("/test3/sample.txt",FILE_WRITE);


And would also like to be able to automatically create

File dataFiles = SD.open("/test3/sample1.txt",FILE_WRITE);

File dataFiles = SD.open("/test3/sample2.txt",FILE_WRITE);

File dataFiles = SD.open("/test3/sample3.txt",FILE_WRITE);


Is there a way to do this
Sign up now
to remove ads between posts
Sep 15, 2019, 11:14 AM
I don't want to "Switch Now"
pmackenzie's Avatar
I am guessing what you really want is to increment the number each time?
I would put a text file in the root with the last number used. Read this file when you boot and then you will know the last number used. Update the file and create the new directory.
Or alternately keep the number in eeprom, and read it from there on boot.
Sep 15, 2019, 12:35 PM
Registered User
Pmackenzie suggestion is a good one. Especially using EEPROM for storing the value, the fastest and most reliable way

The other is to scan for existing files attaching a number at the end, until you find one that doesn't exist, and create that one. That way you don't need to store anything. But this method can resul tin a slow boot if you have hundreds of files

The biggest problem with your format is that you will have a test3 directory, test33, test333 over time, and it might fill the SD card after a certain number of boots, and will not sort properly

So I suggest you decide what's the maximum amount of files/directories you need before starting from scratch again. Then use a directory format of test0000, test0001, etc. That way upon boot you simply start scanning for test0000 incrementing the number by one, until you find a non-existing file (usually you try to open a file, if you succeed, close it and move on, the file obviously exists already). Same concept works for files and directories

I also highly suggest creating an account on https://forum.arduino.cc/ and asking your Arduino coding questions there, where I'm sure you will find people who can provide you with code and libraries likely implementing what you need already
Sep 15, 2019, 09:28 PM
Registered User
vollrathd's Avatar
Thread OP
Quote:
Originally Posted by robca
Pmackenzie suggestion is a good one. Especially using EEPROM for storing the value, the fastest and most reliable way

The other is to scan for existing files attaching a number at the end, until you find one that doesn't exist, and create that one. That way you don't need to store anything. But this method can resul tin a slow boot if you have hundreds of files

The biggest problem with your format is that you will have a test3 directory, test33, test333 over time, and it might fill the SD card after a certain number of boots, and will not sort properly

So I suggest you decide what's the maximum amount of files/directories you need before starting from scratch again. Then use a directory format of test0000, test0001, etc. That way upon boot you simply start scanning for test0000 incrementing the number by one, until you find a non-existing file (usually you try to open a file, if you succeed, close it and move on, the file obviously exists already). Same concept works for files and directories

I also highly suggest creating an account on https://forum.arduino.cc/ and asking your Arduino coding questions there, where I'm sure you will find people who can provide you with code and libraries likely implementing what you need already
Thanks
I was planning on using a 0000, 0001, 0002 format for creating a new directory in the SD card. That number would be saved to EEprom for the next go around. I've created directories with numbers, it works fine.

My problem is exactly HOW to incorporate the retrieved number from EErom into the SD.mkdir format.

Example:

unsigned int newdir = 1234;
SD.mkdir("/newdir") ;

newdir = newdir+1;

(I tried SD.mkdir(/newdir);, doesn't work. )

Result, the SD card now shows a /1234 directory has been created.

I've searched all over google and YouTube, can't find anything on how to do this.

Guess if this can't be done, the only option would be to create a couple of hundred directories on the SD card, and search for an unused one each time for new data entry.
Sep 15, 2019, 10:41 PM
I don't want to "Switch Now"
pmackenzie's Avatar
Not an Arduino expert, but my thoughts:

mkdir is expecting a filename, not an integer.
Try doing a string assignment to the file name first.

SD.mkdir( "/newdir") doesn't work because anything inside the quotes will not be evaluated. You should just get a directory called newdir

SD.mkdir(string("/"+string(newdir))) might work.

Or you might have to do the string assignment first.

fname = string("/"+string(newdir));
SD.mkdir(fname);
Sep 15, 2019, 11:58 PM
Registered User
vollrathd's Avatar
Thread OP
Quote:
Originally Posted by pmackenzie
Not an Arduino expert, but my thoughts:

mkdir is expecting a filename, not an integer.
Try doing a string assignment to the file name first.

SD.mkdir( "/newdir") doesn't work because anything inside the quotes will not be evaluated. You should just get a directory called newdir

SD.mkdir(string("/"+string(newdir))) might work.

Or you might have to do the string assignment first.

fname = string("/"+string(newdir));
SD.mkdir(fname);
It's midnight, I'll print your comments and try tomorrow.

Thanks!
Sep 16, 2019, 11:40 AM
Registered User
Using strings in C is always messy and with unexpected surprises. Everything in C is a char array, and the String library masks that fact.

Look for how to use sprintf, which is one of the most flexible ways to format a string. Code below should work, I didn't compile or test it though. Use it as a starting point to look into how sprint () works

int newdir = 1234;
int newfile = 23;
char dirBuf[5];
sprintf (dirBuf, "%04i", newdir);
char fileBuf[13];
sprintf(fileBuf, "%04ifile.txt", newfile);

It should create a directory named 1234, and a file named 0023file.txt

As I said, you will get answers from actual C and C++ coders with years of experience on the Arduino forums
Sep 16, 2019, 12:09 PM
Registered User
vollrathd's Avatar
Thread OP
Quote:
Originally Posted by robca
Using strings in C is always messy and with unexpected surprises. Everything in C is a char array, and the String library masks that fact.

Look for how to use sprintf, which is one of the most flexible ways to format a string. Code below should work, I didn't compile or test it though. Use it as a starting point to look into how sprint () works

int newdir = 1234;
int newfile = 23;
char dirBuf[5];
sprintf (dirBuf, "%04i", newdir);
char fileBuf[13];
sprintf(fileBuf, "%04ifile.txt", newfile);

It should create a directory named 1234, and a file named 0023file.txt

As I said, you will get answers from actual C and C++ coders with years of experience on the Arduino forums
I tried your code, it did compile, but did not create the directory or file.

Is the code missing the SD.makdir() command? If so, how do I code that?
Sep 16, 2019, 12:25 PM
Registered User
Quote:
Originally Posted by vollrathd
I tried your code, it did compile, but did not create the directory or file.

Is the code missing the SD.makdir() command? If so, how do I code that?
That was the code to simply create the char array for the name itself. Depending on the SD library and platform you use, then you will use the strings created to create either the directory or file. I was just explaining how to create the naming part, the rest depends on how you want your code to work...

In order to use the SD library there are quite a few initialization steps and checks to also do. I was not trying to write a real program, just explain the best way to create/format a string using sprint(), which I thought was your main sticking point

It will then be a matter to use SD.makedir(dirBuf) and SD.open(fileBuf, FILE_WRITE). But, as I said, to use directories and names, you need to also do the proper checks, initialize variables, etc
Sep 16, 2019, 03:57 PM
Registered User
vollrathd's Avatar
Thread OP
Quote:
Originally Posted by robca
That was the code to simply create the char array for the name itself. Depending on the SD library and platform you use, then you will use the strings created to create either the directory or file. I was just explaining how to create the naming part, the rest depends on how you want your code to work...

In order to use the SD library there are quite a few initialization steps and checks to also do. I was not trying to write a real program, just explain the best way to create/format a string using sprint(), which I thought was your main sticking point

It will then be a matter to use SD.makedir(dirBuf) and SD.open(fileBuf, FILE_WRITE). But, as I said, to use directories and names, you need to also do the proper checks, initialize variables, etc
Thanks for your help, I'll give it a go from here.


Quick Reply
Message:

Thread Tools

Similar Threads
Category Thread Thread Starter Forum Replies Last Post
Help! Atx SD-10G Memory Module Hack??? wdwimbish Hand Launch 0 Oct 25, 2015 06:11 PM
Help! Atx SD-10G Memory Module Hack??? wdwimbish Thermal 0 Oct 25, 2015 06:10 PM
Help! Atx SD-10G Memory Module Hack??? wdwimbish Sailplane Talk 0 Oct 25, 2015 06:06 PM
Discussion Aiptek 1.3 SD Memory Question meegosh Aerial Photography 4 Sep 11, 2006 08:12 AM
Pencam SD memory card question Karl Bē Aerial Photography 6 Jun 19, 2003 06:16 AM