Go Down

Topic: Short question about SD (Read 1 time) previous topic - next topic

Mastino

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: [Select]

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

johnwasser

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: [Select]

  // 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
  }
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

erg144

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)


Mastino

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

Mastino

What I need to change in this code that the names will change till 999 not till 99?:)
Code: [Select]
// 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!

PaulS

Quote
What I need to change in this code that the names will change till 999 not till 99?

Isn't it obvious?

Code: [Select]
  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: [Select]
    filename[6] = i/10 + '0';
Replaces the first 0 with the tens value.

Code: [Select]
    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.

Mastino

So it should be something like this?:

Code: [Select]

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

PaulS

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.

Mastino

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

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

PaulS

Code: [Select]
    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.

Mastino

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

PaulS

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: [Select]
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".

Mastino

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

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy