Problem creating dir/subdir/

Hi everyone, right now I'm stuck in a function in the project of a datalogger, that basically creates directories (years) and inside each of these it creates subdirectories (months).

The problem is that when I use sprintf() it seems to not detect correctly the %s/%s because it only shows the subdirectory (month) and the directory appears blank. I've tried with %04d/%02d which has no sense, but just to give it a try and see what showed up, and it showed the directory (year) and subdir (month), but obviously not with the correct information, just random numbers.

I don't really understand why it only shows one of the parameters in this case the subdirectory, I suppose the problem might be in the operator /'.

Thanks for any help!

Code:

#include<SPI.h>
#include<SD.h>

const int chipSelect = 4; // Arduino Mega

const int DIR_ANYO = 4;
const int SUBDIR_MES = 13;

const char *mes[SUBDIR_MES] = {"", "01", "02", "03", "04", "05", "06",
"07", "08", "09", "10", "11", "12" }; //Array con los meses
const char *anyo[DIR_ANYO] = {"", "2016", "2017", "2018"};

char dir_subdir [17];
int a = 0, m = 0;

void setup() {
Serial.begin(9600);
pinMode(pinTemp, INPUT);
pinMode(chipSelect, OUTPUT);

Serial.print("Inicializando SD...");

// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Fallo o tarjeta no presente");
// don't do anything more:
return;
}

Serial.println("Tarjeta SD OK");

}

void loop() {

if( m < SUBDIR_MES )
{
sprintf(dir_subdir, "%s/%s", anyo[a], mes[m]);

if(!SD.exists(dir_subdir))

{

if(SD.mkdir(dir_subdir))

{

Serial.println(" ");
Serial.print("Directorio/subdirectorio creado: ");
Serial.print(dir_subdir);

}

if(m == SUBDIR_MES)
{
m = 0;
a++;
Serial.println("directorio completo, creando el siguiente...");
}
else
{
Serial.println("El subdir ya existe, creando el siguiente...");
m++;
}
}
}
else {
Serial.println("Todos los directorios/subdir creados!");
}

}

what is pinTemp?

your problem is that at the beginning of the loop you do

 if( m < SUBDIR_MES  )

and then in the execution while m has not been incremented you do

   if(m == SUBDIR_MES)

that will never be true because the moment m == SUBDIR_MES the main if at the top of the loop will kick you out to your message stating "Todos los directorios/subdir creados!" while in reality you will only have created
/01 to /12 because a never get a chance to be incremented.

sprintf works pretty well, if fed with the correct parameters.

#include<SPI.h>
#include<SD.h>

const int chipSelect = 4; // Arduino Mega

const int DIR_ANYO = 4;
const int SUBDIR_MES = 13;

const char *mes[SUBDIR_MES] = {"", "01", "02", "03", "04", "05", "06",
                               "07", "08", "09", "10", "11", "12"
                              };  //Array con los meses
const char *anyo[DIR_ANYO] = {"", "2016", "2017", "2018"};

char dir_subdir[8]; // "xxxx/xx\0"
int a = 1, m = 0;  // start with valid year
bool allDone;

void setup() {
  Serial.begin(115200);
  //pinMode(pinTemp, INPUT);
  pinMode(chipSelect, OUTPUT);

  Serial.print("Inicializando SD...");

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Fallo o tarjeta no presente");
    for (;;);
  }
  Serial.println("Tarjeta SD OK");
}

void loop() {
  if (a < DIR_ANYO) {
    sprintf(dir_subdir, "%s/%s", anyo[a], mes[m]);
    if (!SD.exists(dir_subdir)) {
      if (SD.mkdir(dir_subdir)) {
        Serial.print("creado: ");
      }
    } else {
      Serial.print("found: ");
    }
    Serial.print(dir_subdir);
    if (m == SUBDIR_MES - 1) {
      m = 0;
      a++;
      Serial.print(" directorio completo");
    } else  {
      Serial.print(" El subdir ya existe");
      m++;
    }
    Serial.println(", creando el siguiente...");

  } else if (!allDone) {
    allDone = true;
    Serial.println("Todos los directorios/subdir creados!");
  }
}