Auto naming files in GPS/SD shield....

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
dpaulc
 
Posts: 34
Joined: Thu May 09, 2013 11:40 am

Auto naming files in GPS/SD shield....

Post by dpaulc »

The Ultimate GPS shield sketch automatically creates and names files up to 99. I'd like to go to 999.

Here's the code:

Code: Select all

  char filename[15];
  strcpy(filename, "GPSLG000.CSV");
  for (uint8_t i = 0; i < 1000; i++) {
    filename[5] = '0' + i/100;
    filename[6] = '0' + i/10;
    filename[7] = '0' + i%10;
   
    // create if does not exist, do not open existing, write, sync after write
    if (! SD.exists(filename)) {
      break;
    }
  }
Here's the naming after 99:

GPSLG099.CSV
GPSLG1:0.CSV
GPSLG1:1.CSV
GPSLG1:2.CSV

A colon is inserted instead of a number. What do I need to do to correct this problem?

User avatar
dpaulc
 
Posts: 34
Joined: Thu May 09, 2013 11:40 am

Re: Auto naming files in GPS/SD shield....

Post by dpaulc »

I think I've got it:

Code: Select all

char filename[15];
  strcpy(filename, "GPSLG000.CSV");
  for (uint8_t i = 0; i < 1000; i++) {
    filename[5] = '0' + i/100;
    if (i < 99) filename[6] = '0' + i/10;
    if (i >=100) filename[6] = '0' + (i%100)/10;
    filename[7] = '0' + i%10;
   
Please let me know if there is a better way. Thanks.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Auto naming files in GPS/SD shield....

Post by adafruit_support_bill »

You don't need the 'if' statements. Just this will do:

Code: Select all

filename[6] = '0' + (i%100)/10;

User avatar
dpaulc
 
Posts: 34
Joined: Thu May 09, 2013 11:40 am

Re: Auto naming files in GPS/SD shield....

Post by dpaulc »

Without the 'if' statement I get file numbering ...007,008,009,100...

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Auto naming files in GPS/SD shield....

Post by adafruit_support_bill »

Really? 10%100 = 10 and 10/10 = 1.

User avatar
dpaulc
 
Posts: 34
Joined: Thu May 09, 2013 11:40 am

Re: Auto naming files in GPS/SD shield....

Post by dpaulc »

My bad. You are correct. Had a hard to spot typo.

Thanks for the help.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”