Pages: [1]   Go Down
Author Topic: using sd.exist(file) and sd.mkdir when having file in a directory  (Read 2943 times)
0 Members and 1 Guest are viewing this topic.
Sarajevo
Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So this is what i need to do.
I have to make a time (use time.h) and to check if there is a directory with the date like this year/month/day, if not i need to create it. If i just type mkdir("2013/12/02") it works, but if I try to make a char which consists of the number of the dates, it doesnt work smiley-sad
this is my code

#include <avr/pgmspace.h>
/* Include the serial library for communicating with the COM port */
#include <SPI.h>
/* Include the standard Ethernet library */
#include <Ethernet.h>
/* Include the standard SD card library */
#include <SD.h>
// include time for clock
#include <Time.h>
/* DIO pin used to control the modules CS pin */
#define SD_CARD_CD_DIO 4
File SDFileData;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 10, 55 );
EthernetServer server(80);
char dir[10],godina[4],mjesec[2],dan[2];

void setup()

  /* Initialise the serial port */
  Serial.begin(9600);
  /* Start the Ethernet interface */
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());

  /* DIO pin used for the CS function. Note that even if you are not driving this
     function from your Arduino board, you must still configure this as an output
     otherwise the SD library functions will not work. */
  pinMode(10, OUTPUT);

 
 
setTime(12, 30 , 15 , 22, 12, 2013);
  /* Initialise the SD card */
  if (!SD.begin(SD_CARD_CD_DIO))
  {
    /* If there was an error output this to the serial port and go no further */
    Serial.println("ERROR: SD card failed to initialise");
    while(1);
  }else
  {
    Serial.println("SD CARD OK");
  }
         
           sprintf(godina,"%d", year());
           dir[0]=godina[0];
           dir[1]=godina[1];
           dir[2]=godina[2];
           dir[3]=godina[3];
           dir[4]=dir[7]='/';
           sprintf(mjesec,"%d", month());
           dir[5]=mjesec[0];
           dir[6]=mjesec[1];
           sprintf(dan,"%d", day());
           dir[8]=dan[0];
           dir[9]=dan[1];

           
                   Serial.println(dir);
 
       if (!SD.exists(dir))
    {      Serial.println("uso");
      SD.mkdir(dir);
    }
}

As you can see i made a char named dir in which i put all the parts together, i tried using for function (i 0 to 10) but in serial.println (dir) it got me 2013/12/2220131222
u see the year and month is ok, but day :S, so i deleted for function and modified it like the code above, unfortunately i get the same. To make the situation more wired, on the sd card is made a file named 2013 but its not a folder, and it has 0 or 4 kb size :S

I am trying now for days to solve this, i tried to make a function which returns char array of dir but that had also some complications, i assume some memory problems appear if i use that method, because something with pointers it is what cant work properly on the arduino mega.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 652
Posts: 50868
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
As you can see i made a char named dir
Actually, I can't see that. I can see that you have a char ARRAY named dir.

I can also see that you posted your code incorrectly, and the you have not chosen a programming style that anyone else uses. NONE of the recognized styles allow for anythingfollowingtheclosecurlybrace. Especially without spaces.

I can also see that you haven't bothered to learn about, or use, Tools + Auto Format.

Or strcat() or strcpy()...

Or much about sprintf:
Code:
           sprintf(godina,"%d", year());
This does not guarantee exactly 4 characters in the output.

If year() is a 4 digit number, godina is not large enough to hold 4 characters AND the terminating NULL that sprintf() IS going to put in the array.

Nor are the other arrays big enough.
Logged

Sarajevo
Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry i am using arduino, and programming it the first time, i had C and C++ in school but thats it.
I Will first read about strcat() and the other functin that u wrote and if i cannot solve it ether i will reply again.
One question tough, why is godina not large enough, and is terminating NULL something like a NULL that it ads at the and of the array so that it represents an end ?
Thank you for you reply despite i dont write the code proffesional.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 652
Posts: 50868
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
is terminating NULL something like a NULL that it ads at the and of the array so that it represents an end ?
Exactly like that.

Quote
why is godina not large enough
Month has 2 characters. Day has two characters. Year has 4 characters. With separators between month, day, and year, that's 2 + 1 + 2 + 1 + 4 + 1 (for the NULL) =  11. godina is sized to hold 10 characters. Even with shoes on, I can see that 11 is greater than 10.
Logged

Sarajevo
Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, that was the problem, i forgot about that NULL ( havent used C nor C++ since 2010.). Now it works, thanks a lot smiley-grin
Logged

Sarajevo
Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Now i want to make a function that returns an char array, i just made the function by copying the previous code for making the directory name into the function, but it doesnt work right, here is the function
Code:
char *direktorij()
 {
   char dir[11];
   sprintf(godina,"%d", year());

  sprintf(mjesec,"%d", month());

  sprintf(dan,"%d", day());
  strcpy (dir,godina);
  strcat (dir,"/");
  strcat (dir,mjesec);
  strcat (dir,"/");
  strcat (dir,dan);
  return dir;
 }

if i tried char[11] direktorij.....
it didnt worked at all while complaining
but if i use this, then it gives false results.
« Last Edit: July 29, 2013, 05:49:49 am by nino90 » Logged

Offline Offline
Edison Member
*
Karma: 50
Posts: 1698
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Put your code in code tags: How to post code properly

Pete
Logged

Atlanta, USA
Offline Offline
Edison Member
*
Karma: 56
Posts: 1847
AKA: Ray Burne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@nino90:

There is lots of existing code libraries and examples for SD cards. 

I do not remember one off the top that uses a date directory structure, but "date" is just a fixed-length string.  Checking for a file-name or a directory name is the same function call, in this SD library:
http://arduino.cc/en/Reference/SDexists

Use your PC to create a few directories on the SD card, plug it into the Arduino and write some snippets to detect the directories.  Then convert the code to a function if you wish.  And test again. 




Logged

Sarajevo
Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

well, i decided to make a separate function which makes that directory, so i avoid using arrays for returns or sending into the functions.
Now i have another question, i want to SD.open a file, it works if i write  podaci=SD.open("example.txt", FILE_WRITE);
But my file name and its directory will bi variable. So i created a variable called file, type char and made podaci=SD.open(file, FILE_WRITE);
when i use Serial.print(file), it gives me 2013/5/6/6-5-2013-12-30-35.txt, so it should work but it doesnt create the file, and i dont know why :S
On the internet i cant finde an sample that uses directory like this

Code:
void loop()
{

  sprintf(godina,"%d", year());
  sprintf(mjesec,"%d", month());
  sprintf(dan,"%d", day());
  sprintf(sat,"%d", hour());
  sprintf(minut,"%d", minute());
  sprintf(sekunda,"%d",second());
  strcpy (dir,godina);
  strcat (dir,"/");
  strcat (dir,mjesec);
  strcat (dir,"/");
  strcat (dir,dan);

  strcpy (file,dir);
  strcat (file,"/");
  strcat (file,dan);
  strcat (file,"-");
  strcat (file,mjesec);
  strcat (file,"-");
  strcat (file,godina);
  strcat (file,"-");
  strcat (file,sat);
  strcat (file,"-");
  strcat (file,minut);
  strcat (file,"-");
  strcat (file,sekunda);
  strcat (file,".txt");
  Serial.println(file);
  podaci=SD.open(file, FILE_WRITE);
  if (podaci)
  {
    for (int i=1;i<=10;i++){
      podaci.print(i); podaci.print(" ");
      podaci.println(analogRead(I1));
      Serial.println(i);
      Serial.println(analogRead(I1));
      // close the file:
      podaci.close();
      delay(1000);
    }
  }
  else {

    //Serial.println("Cannot create file");
  }
its interesting that if i name the file only hh-mm-ss (without year month and day part) it works, maybe the filename are limited how much lenght it can have?
« Last Edit: July 29, 2013, 03:15:16 pm by nino90 » Logged

Sarajevo
Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

seems like it can have maximum 8 characters.
Code:
podaci=SD.open(file, FILE_WRITE);
  if (podaci)
  {
    for (int i=0;i<10;i++){
     
      podaci.print(i); podaci.print(" ");
      podaci.println(analogRead(I1));
      Serial.println(i);
      Serial.println(analogRead(I1));
      // close the file:
      podaci.close();
      delay(1000);
    }
  }
  else {

    //Serial.println("Cannot create file");
  }
Can you see something wrong in this code? It doesnt input all the readings it should, only one or two , not 10 :/
here is my output file
1 617
1 617
U see only on reading, and only i=1  not the others i=2 3 ....
Logged

Offline Offline
Edison Member
*
Karma: 50
Posts: 1698
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You close the SD file inside the for loop. Not a good idea.

Pete
Logged

Sarajevo
Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i decided to use sdfat.h because i have found an example how to donwload files via internet from the sd using this library. It is not compatible with SD.h so i need to finde functions that are in sdfat library, first i found out that SD.exists() replace with file.exists()
/But whats functions is used for SD.mkdir ? and for make file for write?
Logged

Pages: [1]   Go Up
Jump to: