Pages: [1]   Go Down
Author Topic: SD card variable file name  (Read 5708 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 17
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am creating the charges logger for a vending machine rebuilt to use RFID cards.  I would like so after every time the card is removed and replaced a new file is created with the current date.  I am using the SD library so the statement in question is file = SD.open("filename.txt", FILE_WRITE); I have an RTC which puts each element of time/date into an int so I have an int minute, int hour, and so on.  I am just starting to try to put minute as the file name but I will need to put pieces together to achieve a full date.  Also, I would need to be able to append a number if there is a duplicate file name as I probably will not use seconds in the file name (or maybe I could).  And finally, I don't how to get the .txt in as I don't want my client to have to modify the file name.  (The txt file gets pasted into excel for processing.)

I know I need some sort of character array but I can't get anything to work.  I am thinking "String - object" may be the right path but that's not working for me either.

Help is always greatly appreciated,

GTech
Logged

0
Offline Offline
God Member
*****
Karma: 0
Posts: 594
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You said character array, so what went wrong when you tried that?

you might need to understand <string.h> a bit more, read this all the way through http://www.nongnu.org/avr-libc/user-manual/group__avr__string.html
Logged

Freelance engineer, consultant, contractor. Graduated from UW in 2013.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I know I need some sort of character array but I can't get anything to work.
Posting what you have tried is always better than hand-waving.

Quote
I am thinking "String - object" may be the right path but that's not working for me either.
Don't think that String, which is just a wrapper around char arrays, is going to solve all of your problems.

There are a number of ways to convert int values to strings (lower case s - NULL terminated char array). The itoa() function can do it, with no formatting, one at a time. The sprintf() function can do it, with formatting, and converting multiple values at one time.

There is a tradeoff with using sprintf() which is that your code size goes up.

Don't forget the restrictions on the length of the name (8 characters). Don't forget the extension for the name.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 17
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Um, WHILE we're on the subject of filenames on SD cards...  :\

My intent is to log sporadic events to an SD card, tagging them with the time.  I'd like to save a file-a-day and create the new file at midnight.  Embedding the ten bold lines below gives me a workable filename which changes correctly at midnight and the code recognizes it and everything's fine.

BUT, when I try to make a function out of it (I'll need to check for the date rollover several times in my code) I can't get the function to return anything when I do a "if(! SD.exists(filename)) {...}" later on.  (the error says "invalid conversion from 'char*' to 'char'" and highlights "return filename;" in the function)

Up until now I've pretty much banged everything out inside the regular setup and loop functions and this is my first real experience with making my own function and returning variables...  Everybody's comments & criticisms are appreciated.

  char getFilename() {
    DateTime now = RTC.now(); int year = now.year(); int month = now.month(); int day = now.day();
    char filename[] = "00000000.CSV";
    filename[0] = '2';
    filename[1] = '0';
    filename[2] = year%10 + '0';
    filename[3] = year%10 + '0';
    filename[4] = month/10 + '0';
    filename[5] = month%10 + '0';
    filename[6] = day/10 + '0';
    filename[7] = day%10 + '0';

    return filename;
  }
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  char getFilename() {
This says that the function is to return a character, which is not the same as an array of characters.

So, hey, look at that. The compiler was right. Well, we knew it was.

Now, before you think that you can actually return filename from that function, forget it. filename is a local variable which goes out of scope when the function ends. If you return a pointer to that memory, it will be garbage as soon as the memory is reused.

What you need to do is pass to the function the array that you want modified. Since arrays act a lot like pointers, define the function as taking a pointer to a char.
Code:
void getFilename(char *fileName)
{
}
and call it like so:
Code:
char myFileName[16];
getFilename(myFileName);

Let's see if you can figure out how to write to the argument in the function then. If not, come on back. We'll be here all week.
Logged

0
Offline Offline
Jr. Member
**
Karma: 3
Posts: 77
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

GTech13,

here is a snippet that might help:
Code:
      char file_name[] = "xxxxxxxx.CSV";    // prototype file name
      char extension[] = "CSV";  // sometimes the extension gets modified

      sprintf(file_name,"%c%c%cCALIB.%s",EEPROM.read(fpf0),EEPROM.read(fpf1),
      EEPROM.read(fpf2),extension);

      /*  open the CAL file on the SD.  New file each time
       The serial number, customer number, file number are on the processor EEPROM
       */

      if (file.open(&root, file_name, O_CREAT | O_APPEND | O_WRITE))
      {
        PgmPrintln("calibration file opened");
        Serial.println(file_name);
      }

 Use your date strings instead of the EE reads in sprintf().  I increment the EE bytes and save them again so each file name is a sequential number.  Should work with date strings.  Are you setting the file creation dates and the file modification dates? They are useful and facilitate sorting file names when looking at the directory.

JC
« Last Edit: September 30, 2011, 12:13:52 pm by jcarrr » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 17
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I got it working - and can see where I was off and that I need to understand pointers better.  I know I probably shouldn't use "filename" inside-of and outside-of the function, but it compiles and the SD card functions accept it with no problems, so: <shrug>.

Thank you Paul.
Ex uno disce omnes
___
in setup:
char filename[] = "00000000.CSV";

function call:
getFilename(filename);

function:
  void getFilename(char *filename) {
    DateTime now = RTC.now(); int year = now.year(); int month = now.month(); int day = now.day();
    filename[0] = '2';
    filename[1] = '0';
    filename[2] = year%10 + '0';
    filename[3] = year%10 + '0';
    filename[4] = month/10 + '0';
    filename[5] = month%10 + '0';
    filename[6] = day/10 + '0';
    filename[7] = day%10 + '0';
    filename[8] = '.';
    filename[9] = 'C';
    filename[10] = 'S';
    filename[11] = 'V';
    return;
  }
Logged

Leighton Buzzard, UK
Offline Offline
Edison Member
*
Karma: 21
Posts: 1339
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I got it working - and can see where I was off and that I need to understand pointers better.  I know I probably shouldn't use "filename" inside-of and outside-of the function, but it compiles and the SD card functions accept it with no problems, so: <shrug>.

Thank you Paul.
Ex uno disce omnes
___
in setup:
char filename[] = "00000000.CSV";

function call:
getFilename(filename);

function:
  void getFilename(char *filename) {
    DateTime now = RTC.now(); int year = now.year(); int month = now.month(); int day = now.day();
    filename[0] = '2';
    filename[1] = '0';
    filename[2] = year%10 + '0';
    filename[3] = year%10 + '0';
    filename[4] = month/10 + '0';
    filename[5] = month%10 + '0';
    filename[6] = day/10 + '0';
    filename[7] = day%10 + '0';
    filename[8] = '.';
    filename[9] = 'C';
    filename[10] = 'S';
    filename[11] = 'V';
    return;
  }


suggest
filename[2] = year/10 + '0';
Logged

there are only 10 types of people
them that understands binary
and them that doesn't

0
Offline Offline
Edison Member
*
Karma: 17
Posts: 1413
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

very usefull code thanks a lot !!

as for the year, if i use the /10 suggested i get this.
Code:
   filename[0] = year/10 + '0';
    filename[1] = year%10 + '0';

Code:
ù1

I am using this as I assume no registries before year 2000 and this way if arduino reboots cause of a file corruption, it will create a new file with a different HOUR.

Code:
void getFilename(char *filename) {
    DateTime now = RTC.now(); int year = now.year(); int month = now.month(); int day = now.day(); int hour = now.hour();

    filename[0] = '1';  //year/10 + '0';        // im getting error here so for now force it to 1.
    filename[1] = year%10 + '0';
    filename[2] = month/10 + '0';
    filename[3] = month%10 + '0';
    filename[4] = day/10 + '0';
    filename[5] = day%10 + '0';
    filename[6] = hour/10 + '0';
    filename[7] = hour%10 + '0';
    filename[8] = '.';
    filename[9] = 'C';
    filename[10] = 'S';
    filename[11] = 'V';
    return;
  }

ive printed year and contains "2011", so i thing thats the problem.

I have done this and works
Code:
    filename[0] = (year-2000)/10 + '0';
    filename[1] = year%10 + '0';
I guess its good enough for me that it works till 2099 hehe
« Last Edit: October 03, 2011, 06:04:57 am by Sergegsx » Logged

* Si preguntas, pon el código de tu programa, hace mucho mas fácil ayudarte. Y me ahorro un mensaje pidiendo que lo hagas.
* Si consigues solucionar tu problema, dedica unos minutos a explicar en tu post como lo conseguiste para beneficio de todos.
* Cambia el 'Subject' de tu hilo y añade 'SOLUCIONADO' cuando hayas llegado a una solución al problema que planteaste.
* Utiliza un 'Subject' para tu hilo que explique de que va el hilo.
Si estas empezando:
* Comienza a usar Arduino
* Guías de iniciación a Arduino
* Ejemplos
* Referencia del Lenguaje
* Conceptos básicos
Guia de usuario de arduino
Tutoriales en Ingles
Si necesitas que alguien te escriba el código: http://www.freelancer.com/  o esta  http://www.guru.com/

Leighton Buzzard, UK
Offline Offline
Edison Member
*
Karma: 21
Posts: 1339
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yup
(year-2000)/10 is even better
i suspect the original worked as it's 2011!!!
Logged

there are only 10 types of people
them that understands binary
and them that doesn't

Pages: [1]   Go Up
Jump to: