Pages: [1]   Go Down
Author Topic: Short question about SD  (Read 1260 times)
0 Members and 1 Guest are viewing this topic.
Baltic states
Offline Offline
Jr. Member
**
Karma: 0
Posts: 97
new day, new try
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello everyone,
How should I write this code that on every new loop the name of the file get a new name. For example every 5sec. on the SD card will be written file with different name. First one test1.txt after 5 sec. test2.txt after 5 sec test3.txt and so on. This code of course don`t work but it shows exactly what I want. Many thanks! 
Code:

         for (int i=0; i <= 255; i++)
         myFile = SD.open("test[i].txt", FILE_WRITE);
         if (myFile) {
         myFile.println("arduino");
         myFile.close();
         }
         delay(5000);
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8082
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This code from a data logger example will try names from "LOGGER00.CSV" until "LOGGER99.CSV" until it finds one that doesn't already exist:
Code:
  // create a new file
  char filename[] = "LOGGER00.CSV";
  for (uint8_t i = 0; i < 100; i++) {
    filename[6] = i/10 + '0';
    filename[7] = i%10 + '0';
    if (! SD.exists(filename)) {
      // only open a new file if it doesn't exist
      logfile = SD.open(filename, FILE_WRITE);
      break;  // leave the loop!
    }
  }

  if (! logfile) {
   // Failed to find an unused filename
  }
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Few options you could explore - the last two closer to bullet proof.

1 - append the loop index as mentioned above.
2 - get system time.  This works until the system is reset or the counter wraps.
3  - combo of the above (less likely of a name collision )
4 - store the next file name (or current file name) or counter in a file or flash, update it each time through the loop and use it to create a new file name
5 - if you have a rtc, create a file name based on the RTC milliseconds (or combination of time fields)

Logged

Baltic states
Offline Offline
Jr. Member
**
Karma: 0
Posts: 97
new day, new try
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks! I think I get it smiley If you ask yourself for what I need it? It is for my new  robot project, something like adaptive mapping.
Logged

Baltic states
Offline Offline
Jr. Member
**
Karma: 0
Posts: 97
new day, new try
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What I need to change in this code that the names will change till 999 not till 99?smiley
Code:
// create a new file
  char filename[] = "LOGGER00.CSV";
  for (uint8_t i = 0; i < 100; i++) {
    filename[6] = i/10 + '0';
    filename[7] = i%10 + '0';
    if (! SD.exists(filename)) {
      // only open a new file if it doesn't exist
      logfile = SD.open(filename, FILE_WRITE);
      break;  // leave the loop!
    }
  }

  if (! logfile) {
   // Failed to find an unused filename
  }
Thank you!
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46113
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What I need to change in this code that the names will change till 999 not till 99?
Isn't it obvious?

Code:
  char filename[] = "LOGGER00.CSV";
The code is changing the 0 in this string to other values. You need to put a 3rd 0 in the name, without making it any longer.

Code:
    filename[6] = i/10 + '0';
Replaces the first 0 with the tens value.

Code:
    filename[7] = i%10 + '0';
Replaces the second 0 with the ones value.

You need to add another statement to replace a position with the hundreds value, change how the tens value is extracted ((i/10)%10) - 864 divided by 10 is 86; 86 modulo 10 is 6), and change the 10 to 100 in the ones statement.
Logged

Baltic states
Offline Offline
Jr. Member
**
Karma: 0
Posts: 97
new day, new try
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So it should be something like this?:

Code:
char filename[] = "LOGGE000.CSV";
filename[5] = ((i/10)%10) + '0';
filename[6] = i/10 + '0';
filename[7] = i%10 + '0';
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46113
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If I = 817, what will that file name look like?

You have the tens value first, then the hundred value (incorrectly calculated), then the ones value.
Logged

Baltic states
Offline Offline
Jr. Member
**
Karma: 0
Posts: 97
new day, new try
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

And one more question.. smiley how to write this line correctly? (char filename = val;)

Code:
   
    int val = 123;
    char filename = val;
    if (! SD.exists(filename)) {
    logfile = SD.open(filename, FILE_WRITE);
     }
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46113
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    int val = 123;
    char filename = val;
    if (! SD.exists(filename)) {
Do you have a file on the SD named "{"? That is what you set the file name to.
Logged

Baltic states
Offline Offline
Jr. Member
**
Karma: 0
Posts: 97
new day, new try
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So how should I convert 1 2 3 to 49 50 51?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46113
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
So how should I convert 1 2 3 to 49 50 51?
Is this a trick question? Add 48 to each digit (or '0'). If you want the compiler to do the work for you:

Code:
int fileNumber = 814;
char fileName[12];
sprintf(fileName, "LOGGE%03d.CSV", fileNumber);
will result in fileName containing "LOGGE814.CSV".

If fileNumber were 22, instead, fileName would contain "LOGGE922.CSV".
Logged

Baltic states
Offline Offline
Jr. Member
**
Karma: 0
Posts: 97
new day, new try
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you very much PaulS! I would never come to this code by myself.. With all sprintf and other funny things. smiley
Thanks you my code is working and I`am one step closer to finish the code!
Logged

Pages: [1]   Go Up
Jump to: