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

Thread: DataLogger, how change the name of a folder name

  1. #1

    DataLogger, how change the name of a folder name

    Hi ,
    I use the ready code from IDE library SdFat--examplesV1-->dataLogger-->
    Code:
    /*
     * Simple data logger.
     */
    #include <SPI.h>
    #include "SdFat.h"
    
    // SD chip select pin.  Be sure to disable any other SPI devices such as Enet.
    const uint8_t chipSelect = 7;
    
    // Interval between data records in milliseconds.
    // The interval must be greater than the maximum SD write latency plus the
    // time to acquire and write data to the SD to avoid overrun errors.
    // Run the bench example to check the quality of your SD card.
    const uint32_t SAMPLE_INTERVAL_MS = 1000;
    
    // Log file base name.  Must be six characters or less.
    #define FILE_BASE_NAME "Data"
    //------------------------------------------------------------------------------
    // File system object.
    SdFat sd;
    
    // Log file.
    SdFile file;
    
    // Time in micros for next data record.
    uint32_t logTime;
    
    //==============================================================================
    // User functions.  Edit writeHeader() and logData() for your requirements.
    
    const uint8_t ANALOG_COUNT = 4;
    //------------------------------------------------------------------------------
    // Write data header.
    void writeHeader() {
      file.print(F("micros"));
      for (uint8_t i = 0; i < ANALOG_COUNT; i++) {
        file.print(F(",adc"));
        file.print(i, DEC);
      }
      file.println();
    }
    //------------------------------------------------------------------------------
    // Log a data record.
    void logData() {
      uint16_t data[ANALOG_COUNT];
    
      // Read all channels to avoid SD write latency between readings.
      for (uint8_t i = 0; i < ANALOG_COUNT; i++) {
        data[i] = analogRead(i);
      }
      // Write data to file.  Start with log time in micros.
      file.print(logTime);
    
      // Write ADC data to CSV record.
      for (uint8_t i = 0; i < ANALOG_COUNT; i++) {
        file.write(',');
        file.print(data[i]);
      }
      file.println();
    }
    //==============================================================================
    // Error messages stored in flash.
    #define error(msg) sd.errorHalt(F(msg))
    //------------------------------------------------------------------------------
    void setup() {
      const uint8_t BASE_NAME_SIZE = sizeof(FILE_BASE_NAME) - 1;
      char fileName[13] = FILE_BASE_NAME "00.csv";
    
      Serial.begin(9600);
    
      // Wait for USB Serial
      while (!Serial) {
        SysCall::yield();
      }
      delay(1000);
    
      Serial.println(F("Type any character to start"));
      while (!Serial.available()) {
        SysCall::yield();
      }
    
      // Initialize at the highest speed supported by the board that is
      // not over 50 MHz. Try a lower speed if SPI errors occur.
      if (!sd.begin(chipSelect, SD_SCK_MHZ(16))) {
        sd.initErrorHalt();
      }
    
      // Find an unused file name.
      if (BASE_NAME_SIZE > 6) {
        error("FILE_BASE_NAME too long");
      }
      while (sd.exists(fileName)) {
        if (fileName[BASE_NAME_SIZE + 1] != '9') {
          fileName[BASE_NAME_SIZE + 1]++;
        } else if (fileName[BASE_NAME_SIZE] != '9') {
          fileName[BASE_NAME_SIZE + 1] = '0';
          fileName[BASE_NAME_SIZE]++;
        } else {
          error("Can't create file name");
        }
      }
      if (!file.open(fileName, O_WRONLY | O_CREAT | O_EXCL)) {
        error("file.open");
      }
      // Read any Serial data.
      do {
        delay(10);
      } while (Serial.available() && Serial.read() >= 0);
    
      Serial.print(F("Logging to: "));
      Serial.println(fileName);
      Serial.println(F("Type any character to stop"));
    
      // Write data header.
      writeHeader();
    
      // Start on a multiple of the sample interval.
      logTime = micros()/(1000UL*SAMPLE_INTERVAL_MS) + 1;
      logTime *= 1000UL*SAMPLE_INTERVAL_MS;
    }
    //------------------------------------------------------------------------------
    void loop() {
      // Time for next record.
      logTime += 1000UL*SAMPLE_INTERVAL_MS;
    
      // Wait for log time.
      int32_t diff;
      do {
        diff = micros() - logTime;
      } while (diff < 0);
    
      // Check for data rate too high.
      if (diff > 10) {
        error("Missed data record");
      }
    
      logData();
    
      // Force data to SD and update the directory entry to avoid data loss.
      if (!file.sync() || file.getWriteError()) {
        error("write error");
      }
    
      if (Serial.available()) {
        // Close file and stop.
        file.close();
        Serial.println(F("Done"));
        SysCall::halt();
      }
    }
    This ready code gives the name of file Data00...Data01..Data02.. and when he done Data99 then it makes an error!
    a)How can I change it to go up to Data999 or Data9999...

    thank you!

  2. #2
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    8,987
    You would need to edit the section that looks like:
    Code:
      while (sd.exists(fileName)) {
        if (fileName[BASE_NAME_SIZE + 1] != '9') {
          fileName[BASE_NAME_SIZE + 1]++;
        } else if (fileName[BASE_NAME_SIZE] != '9') {
          fileName[BASE_NAME_SIZE + 1] = '0';
          fileName[BASE_NAME_SIZE]++;
        } else {
          error("Can't create file name");
        }
      }
    Which is incrementing each of the two digits to do the math for 3 or 4 digits.
    Or you could change all of it to be quick and dirty, with increased code size...
    And maybe do something like:
    Code:
      char filename[13] = "Data00.csv";
      uint16_t filaname_index = 1;
      #define MAX_FILE_INDEX 10000
    ...
      while ((filename_index < MAX_FILE_INDEX ) && sd.exists(fileName)) 
      {
        snprintf(filename, sizeof(filename), "Data%04u.csv", file_name_index++);
      }
      if (filename_index == MAX_FILE_INDEX) 
      {
          error("Can't create file name");
      }
    But warning typed on fly probably has issues and brings in libraries with printf...

  3. #3
    thank you for your time KurtE!!
    i will try without changing the code, to make it different, I only differentiated some values ​​of the same code and I see that it start from data0000 ....data0001 and goes up to data0099 and then goes to data1000 .... data1001 .... 1002 I do not know why it puts number 1 to first digit.
    Βut I noticed that it does not make sense to reach more than data0250 because the fat16/32 format does not allow it to increase the number of files. maybe if it is possible to put them in a folder per 200 files but it goes a long way and there is no reason to do something that ..!!

    below are the changes I made:
    before:
    Code:
      const uint8_t BASE_NAME_SIZE = sizeof(FILE_BASE_NAME) - 1;
      char fileName[13] = FILE_BASE_NAME "00.csv";
    after:
    Code:
    const uint8_t BASE_NAME_SIZE = sizeof(FILE_BASE_NAME) - 1;
      char fileName[13] = FILE_BASE_NAME "0000.csv";
    Before:
    Code:
    // Find an unused file name.
      if (BASE_NAME_SIZE > 6) {
        error("FILE_BASE_NAME too long");
    after:
    Code:
    // Find an unused file name.
      if (BASE_NAME_SIZE > 10) {
        error("FILE_BASE_NAME too long");
    Before:
    Code:
    while (sd.exists(fileName)) {
        if (fileName[BASE_NAME_SIZE + 1] != '9') {
          fileName[BASE_NAME_SIZE + 1]++;
        } else if (fileName[BASE_NAME_SIZE] != '9') {
          fileName[BASE_NAME_SIZE + 1] = '0';
          fileName[BASE_NAME_SIZE]++;
        } else {
          error("Can't create file name");
        }
      }
    after:
    Code:
    while (sd.exists(fileName)) {
        if (fileName[BASE_NAME_SIZE + 3] != '9') {
          fileName[BASE_NAME_SIZE + 3]++;
        } else if (fileName[BASE_NAME_SIZE] != '9') {
          fileName[BASE_NAME_SIZE + 3] = '0';
          fileName[BASE_NAME_SIZE]++;
        } else {
          error("Can't create file name");
        }
      }

  4. #4
    Ops,, unfortunately again creates up to 100 files...

Posting Permissions

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