SD card - Creating a new filename based on value of an int

Greeting all.
I am creating a data logger that records date, time and user as they pass through a door. There will be a keypad added later and a real time clock added later but right now I am looking to create a new filename on the SD card every time power is cycled. The unit will be stand alone so the Serial.prints will be removed later.

What I have in mind is a value that is saved to the EEPROM. That value is indexed up one then re-saved to the EEPROM. That new value is then used to “open” a new file on the sd card and everything that occurs in that power on cycle will be recorded. When the power is cycled. The EEPROM value is read, indexed and then resaved and another file is created.

I have searched the forum and have come across bits and pieces of what I am looking for but my arduino kung fu is not strong enough to put all the pieces together. I am basing this on the example of sd datalogger that comes with arduino 1.6.4

The error code that I get is this:

SD_Datalogger_example.ino: In function 'void loop()':
SD_Datalogger_example:60: error: invalid conversion from 'int' to 'const char*' [-fpermissive]
In file included from SD_Datalogger_example.ino:24:0:
C:\Program Files (x86)\Arduino\libraries\SD\src/SD.h:73:8: error:   initializing argument 1 of 'File SDClass::open(const char*, uint8_t)' [-fpermissive]
   File open(const char *filename, uint8_t mode = FILE_READ);
        ^
invalid conversion from 'int' to 'const char*' [-fpermissive]

This is what I have so far

/*
  SD card datalogger

 This example shows how to log data from three analog sensors
 to an SD card using the SD library.

 The circuit:
 * analog sensors on analog ins 0, 1, and 2
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 10

 created  24 Nov 2010
 modified 9 Apr 2012
 by Tom Igoe
massaged yet again by amachinetech 6 Nov 16

 This example code is in the public domain.

 */
#include <EEPROM.h>
#include <SPI.h>
#include <SD.h>

const int chipSelect = 10;
int val = 0;
int val1 = 0;
void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  //read from EEPROM, index up one then resave to EEPROM
  val1 = EEPROM.read(0);
  val++;
  EEPROM.write(0, val1);
  
Serial.print("current val = ");
Serial.println(val1);
Serial.print("Initializing SD card...");

  // 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()
{
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  //if this file name doesnt exist the create the file

//*****how do I change the following "val" to an actual value that is indexed one everytime the EEPROM is read and added to upon powerup ****
//IE - FILE dataFile = SD.open(val1, FILE_WRITE); 

  File dataFile = SD.open("val", FILE_WRITE); //<--------------

  // if the file is available, write to it:
  if (dataFile) {
   // dataFile.println(dataString);
   dataFile.print("Testing my sanity ");
   dataFile.println(val);
   
    dataFile.close();
    val++;
    // print to the serial port too:
   Serial.print("Testing my sanity ");
   Serial.println(val);
 delay(2000); //used to keep the data being streamed to the SD card down while experimenting
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }
}

Currently the sketch creates a file named VAL and then writes to it. I just want it to make a new one every time the sketch is restarted.

Thank you in advance for your assistance

amachinetech

Hello,

i have the same problem, i will add a RTC later. Now i create the file name with random:

// in the setup section
randomSeed(analogRead(0));
randNumber = random(99999999);
filename += String(randNumber);
filename += String(".txt");

regards ThomasD

In the setup section what does the += do I am having a hard time finding that in the reference section.

// in the setup section randomSeed(analogRead(0)); randNumber = random(99999999); filename += String(randNumber); filename += String(".txt");

nevermind, I was looking right at it. thanks

You can also do some browsing here for other terminology things you may come across and links to more explanations. Can be interesting reading. http://www.stroustrup.com/glossary.html

In place of using the eeprom, consider this … you already have a file on the sd card which can be used to increment the file-name-number … the snippet below will try to open files successively until it opens one which doesn’t yet exist, then break out of the loop … works for files from ‘0’ to ‘99’ … you could modify it to work for thousands of files

  // create a new file
  char name[] = "MMMYYxyz.CSV"; // this should be a string, length 12

  strcpy(name, (month]);// 0, 1 & 2
  name[3] = ((year-2000) / 10 + '0');
  name[4] = (year % 10 + '0');
  for (uint8_t i = 0; i < 100; i++) {
    name[6] = i/10 + '0';
    name[7] = i%10 + '0';
    // O_CREAT - create the file if it does not exist
    // O_EXCL - fail if the file exists
    // O_WRITE - open for write only
    if (file.open(name, O_CREAT | O_EXCL | O_WRITE))break;
  }
  // set creation date time
  file.timestamp(T_CREATE, 2016, 11, 10, 1, 2, 3);
  // set write/modification date time
  file.timestamp(T_WRITE, 2016, 11, 11, 4, 5, 6);
  // set access date
  file.timestamp(T_ACCESS, 2016, 11, 12, 7, 8, 9);
  i = 0;
  if (!file.isOpen()) error ("create");
  PgmPrint("Logging to: ");
  Serial.println(name);

  // write data header

amachinetech: In the setup section what does the += do I am having a hard time finding that in the reference section.

filename += String(randNumber); filename += String(".txt");

Hello,

it is the same as:

filename = filename + String(randNumber);

Like counter++;

regards ThomasD