Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 6 of 6

Thread: SD Card filename struggles

  1. #1
    Junior Member
    Join Date
    Mar 2020
    Posts
    2

    SD Card filename struggles

    Hi All,
    First time posting on here, just reached my boiling point and need some assistance. I'm using a Teensy 3.6, with built in SD card, trying to create a filename that simply appends the datestamp to the filename, and appends also ".csv". As I'm looking to create a file titled "DataLog20200318.csv" and of course based on a trigger develop a few different data log file with unique timestamps. My code works fine when providing
    Code:
    File dataFile = SD.open("datalog.csv", FILE_WRITE);
    However, getting so extremely fancy as to...
    Code:
      char fname[] = "DataLog";
      char tmstmp[21];
      sprintf(tmstmp,"%d%02d%02d%02\0",year(),month(),day());  
      char fext[] = ".csv";
      strcat(filename, fname);
      strcat(filename, tmstmp);
      strcat(filename, fext);
    If I simply substitue SD.open("datalog.csv", FILE_WRITE); ...with this... SD.open(filename, FILE_WRITE); it compiles but does not ever create nor find my file. I even tried \" thinking the SD library needed quotes around it as a complete character string to parse into the characters.

    I guess I don't understand the road block I'm running into... Am I overlooking some simply character array size or what is it?

    Here is the demo code I tried from the Data Logging demo that works until I change from a static text to the filename variable:
    Code:
    #include <SD.h>
    #include <SPI.h>
    
    // On the Ethernet Shield, CS is pin 4. Note that even if it's not
    // used as the CS pin, the hardware CS pin (10 on most Arduino boards,
    // 53 on the Mega) must be left as an output or the SD library
    // functions will not work.
    
    // change this to match your SD shield or module;
    // Arduino Ethernet shield: pin 4
    // Adafruit SD shields and modules: pin 10
    // Sparkfun SD shield: pin 8
    // Teensy audio board: pin 10
    // Teensy 3.5 & 3.6 on-board: BUILTIN_SDCARD
    // Wiz820+SD board: pin 4
    // Teensy 2.0: pin 0
    // Teensy++ 2.0: pin 20
    const int chipSelect = BUILTIN_SDCARD;
    char filename[26]= "";
    
    void setup() // ************************* SETUP *************************
    {
     // Open serial communications and wait for port to open:
      Serial.begin(9600);
       while (!Serial) {
        ; // wait for serial port to connect. Needed for Leonardo only
      }
      Serial.println("Initializing SD card...");
    //=================================================================================
    // establish the file name with timestamp and extension
      char fname[] = "DataLog";
      char tmstmp[] = "20200318";
      char fext[] = ".csv";
      strcat(filename, fname);
      strcat(filename, tmstmp);
      strcat(filename, fext);
      Serial.println(filename);
    
    //=================================================================================
      // see if the card is present and can be initialized:
      if (!SD.begin(chipSelect)) {
        Serial.println("Card failed, or not present");
        // don't do anything more:
        return;
      }
      Serial.println("card initialized.");
    
    }
    
    void loop()
    {
      // make a string for assembling the data to log:
      String dataString = "";
    
      // read three sensors and append to the string:
      for (int analogPin = 0; analogPin < 3; analogPin++) {
        int sensor = analogRead(analogPin);
        dataString += String(sensor);
        if (analogPin < 2) {
          dataString += ","; 
        }
      }
    
      // open the file. note that only one file can be open at a time,
      // so you have to close this one before opening another.
      File dataFile = SD.open(filename, FILE_WRITE);
    
      // if the file is available, write to it:
      if (dataFile) {
        dataFile.println(dataString);
        dataFile.close();
        // print to the serial port too:
        Serial.println(dataString);
      }  
      // if the file isn't open, pop up an error:
      else {
        Serial.println("error opening datalog.txt");
      } 
    }
    Thanks, your frustrated programmer

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    10,935
    File names are 8.3 limited and :: "DataLog20200318.csv" is beyond that.

    Perhaps try more like : "DL200318.csv" or ""20200318.csv""

  3. #3
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    10,935
    Confirmed commenting this line works: // strcat(filename, fname);

    This also works:
    Code:
      char fname[] = "DL";
      char tmstmp[] = "20200318";
      char fext[] = ".csv";
      strcat(filename, fname);
      strcat(filename, &tmstmp[2]);
      strcat(filename, fext);
      Serial.println(filename);
    Another thing added to loop() to stop logging and safely remove the SD card - just use SerMon to send any character(s):
    Code:
      if ( Serial.available()) {
      	while(1);
      }

  4. #4
    Junior Member
    Join Date
    Mar 2020
    Posts
    2
    Quote Originally Posted by defragster View Post
    Confirmed commenting this line works: // strcat(filename, fname);

    This also works:
    Code:
      char fname[] = "DL";
      char tmstmp[] = "20200318";
      char fext[] = ".csv";
      strcat(filename, fname);
      strcat(filename, &tmstmp[2]);
      strcat(filename, fext);
      Serial.println(filename);
    Another thing added to loop() to stop logging and safely remove the SD card - just use SerMon to send any character(s):
    Code:
      if ( Serial.available()) {
      	while(1);
      }
    Much appreciation defragster, the above worked and was able to construct a dynamic tmstmp as well. Next I'm going to tackle altering the library to increase the character allowance.

    Thanks again for the help in such a quick reply!!!

  5. #5
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    10,935
    Quote Originally Posted by CP406 View Post
    Much appreciation defragster, the above worked and was able to construct a dynamic tmstmp as well. Next I'm going to tackle altering the library to increase the character allowance.

    Thanks again for the help in such a quick reply!!!
    Glad it worked there.

    I'd suggest it would be better to create subdirectories and leave the 8.3 naming alone. The 8.3 is deeply embedded and a reasonable restriction as the larger that get the fewer DIR entries fit into a block, so more paging.

    Also the root dir is typically 'special' and getting it full is unproductive as well.

    Would be better to have perhaps year_month directories with daily files in each - or something in that direction. If the files are pulled off the SD card they could be renamed at that time as needed.

  6. #6
    Senior Member
    Join Date
    Dec 2013
    Posts
    266
    @CP406
    Next I'm going to tackle altering the library to increase the character allowance.
    Use SdFat-beta library, 255 character limit.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •