Pages: [1]   Go Down
Author Topic: Naming Files Sequentially?  (Read 1221 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

     For the past few weeks I've been creating a data logger for my motorcycle. It (most of the time) records throttle, clutch, and brake position using two rotary potentiometers, and a flexiforce sensor. Some times it stops after 6 or so cycles, and sometimes it stops when the first input changes, but most of the time, it works fine. Up until now though, I've just been using the same file name over and over, which of course puts all the logged data in the same place. While this wasn't a problem while debugging and wiring it all up, it has become one now that we're ready-ish to break the tether to the computer.

     The arduino will turn on when the key is turned to on, and off when the key is off. Now as a result, should I perhaps go to the store and come home, both of those logging sessions will be combined as one. Now I could probably deal with this by adding a few blank lines to the file in 'setup' but that's just not elegant at all. I'd like to create a new file with each session, something like "LOG001.txt", "LOG002.txt" and so on. So I would need the sketch to check the SD card and find the file with the highest value, and then create a new file with a name one value higher. This could of course be passed as a parameter through the rest of the sketch to keep things simple. My problem is, I have no idea where to start smiley-lol My programming experience is scheme (LISP, first and last time they taught that in an intro CSE class at my university), some C++, a little matlab, and some Arduino/Processing experience prior to this undertaking. Really all I'm looking for is for someone to point me in the right direction, so I can give it a go myself smiley

Thanks,
Jeff

Code:

#include <SD.h>
const int chipSelect=8;
#define interval 33
unsigned long waitUntil=0;

void setup()
{
  Serial.begin(9600);
  pinMode(10, OUTPUT);
  !SD.begin(chipSelect);
    waitUntil = millis() + interval;
}

void loop()
{
  while((long) (millis() - waitUntil) >= 0)
  {
    int star = millis();
    
    int t = analogRead(3);
    int c = analogRead(1);
    int b = analogRead(5);
    
    String dataString = "";
    dataString += String(t);
    dataString += ",";
    dataString += String(c);
    dataString += ",";
    dataString += String(b);
    dataString += ",";
    
    File dataFile = SD.open("log.txt", FILE_WRITE);
    dataFile.print(dataString);
    dataFile.close();
    
    int en = millis();
    int dur = en-star;
    Serial.println(dur);
    
    waitUntil = waitUntil + interval;
  }
}

« Last Edit: August 30, 2012, 05:16:57 pm by Jeff_NEN » Logged

Leeds, UK
Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Another solution may be to keep an extra file on the SD card that has the name of the last used datafile - that way you won't end up scanning for the last file to work out which one it is. Just an idea - I haven't tried it on an Arduino.

If the Arduino powers off when you swtich off the ignition do you have plans to ensure that any data not yet written to the SD card is written before power is lost?

Use of String is somewhat hazardous at present due to a bug in the heap memory managment. You can obtain a patch to correct the problem, but may be better off not using Strings at all just use normal character strings.

Paul
Logged

Denmark
Offline Offline
Edison Member
*
Karma: 35
Posts: 1029
Happy Hobbyist
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Another solution may be to keep an extra file on the SD card
I have used that solution in my logger, i just have an extra file which holds a number that will be increased everytime the Arduino is started.
And to avoid using Strings I use sprintf and a character array for the file name


Code:
#include <SdFat.h>
#include <SdFatUtil.h>
#include <ctype.h>

//Create the variables to be used by SdFat Library
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;
int reading;
float tempC;
int tempPin=1;
char newfile[] = "";
const uint8_t SdChipSelect =5;
int test=10;
char name[] = "Test.txt";     //Create an array that contains the name of our file.
char numname[] = "number.txt"; 
//char contents[256];           //This will be a data buffer for writing contents to the file.
char in_char=0;

  void setup(void)
  { 
    analogReference(INTERNAL);
      Serial.begin(9600);        //Start a serial connection.
     pinMode(5, OUTPUT);       //Pin 10 must be set as an output for the SD communication to work.
     pinMode(tempPin,INPUT);
      card.init();               //Initialize the SD card and configure the I/O pins.
      volume.init(card);         //Initialize a volume on the SD card.
      root.openRoot(volume);     //Open the root directory in the volume.
   
      file.open(root, numname,O_READ); //Read the number
      char num=file.read();
      Serial.println(num);
      file.close();
 
    num++;
 
    file.open(root, numname,O_WRITE); //save new number
    file.print(num);
    file.close();
 
  Serial.println("Number:");
  Serial.println(num);
  //Create new filename and file
  num=num-'0';
  sprintf(newfile, "sens%02d.txt", num); 

  }
 
 
void loop(void){
reading = analogRead(tempPin);
delay(100);
reading = analogRead(tempPin);
Serial.println("Reading");
Serial.println(reading);
tempC = reading / 9.31;
 
Serial.println(tempC);

file.open(root,newfile , O_CREAT | O_APPEND | O_WRITE); // Tested OK
Serial.println(card.errorCode());

file.print("temperatur, ");
file.println(tempC);
file.close();

    delay(2000);     //Wait 2 second before repeating the process.

 
}


Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 107
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Another way to do it (this also incorporates the date into the filename--yymmddnn.csv, where nn is a two-digit sequential number).  This was pulled from one of the SDFatLib examples, IIRC:

Code:
#include <Time.h>

char logFile[13];
int i;

boolean createLogFile()

/* Creates /logs/ directory, if not already present, and log file
 * file name is yymmddnn.csv
 * yy = 2-digit year
 * mm = 2-digit month
 * dd = 2-digit day
 * nn = first unused number from 00-99
 * Requires system clock to be set to year >= 2000
 */

{
  if (!sd.exists("/logs/"))
  {
    if (!sd.mkdir("/logs"))
    {
      //couldn't create logs directory
      return false;
    }
  } // create logs directory, if not already present
  if  (!sd.chdir("/logs"))
  {
    // error
    return false;
  }
  time_t t = now();
  int yr = year(t) - 2000;
  sprintf(logFile,"%.2d%.2d%.2d00.csv",
  yr, month(t), day(t));
  for (i = 0; i < 100; i++)
  {
    logFile[6] = i/10 + '0';
    logFile[7] = i%10 + '0';
    if (sd.exists(logFile))
    {
      continue;
    }
    if(myFile.open(logFile,O_RDWR | O_CREAT | O_AT_END))
    {
      //it worked
      return true;
    }
    else
    {
      //error
      return false;
    }
    break;
  }
}

Obviously this isn't a complete sketch; it's only the function to create the logfile.
Logged

Rapa Nui
Offline Offline
Edison Member
*
Karma: 52
Posts: 1990
Pukao hats cleaning services
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

From AnalogLogger.ino example (SdFat examples), increases the "..00.CSV" by one each time the sdcard starts:

Code:
 
..
// create a new file in root, the current working directory
  char name[] = "LOGGER00.CSV";

  for (uint8_t i = 0; i < 100; i++) {
    name[6] = i/10 + '0';
    name[7] = i%10 + '0';
    if (sd.exists(name)) continue;
    logfile.open(name);
    break;
  }
  if (!logfile.is_open()) error("file.open");
 cout << pstr("Logging to: ") << name << endl;
..
« Last Edit: September 01, 2012, 05:53:45 am by pito » Logged

Pages: [1]   Go Up
Jump to: