Go Down

Topic: Datalogging on SD problem (Read 489 times) previous topic - next topic

Hi everybody;
I didn't know where to bring up my problem so i came here.
I am using this code to write the data i get from analog input on an SD card.


Code: [Select]


      file.open(file_name, ios::app | ios::out);
      if (!file.is_open())
      {
        GLCD.CursorTo(0,3);
        GLCD.print("open error!!");
      }
      file << _hour << ':' << _minute << ':' << _second;
      file << "     " << "sensor " << analog_counter << " : ";
      file << analog_input(analog_counter) << endl;
      if (file.bad())
      {
        GLCD.CursorTo(0,4);
        GLCD.print("write error!!");
      }
      file.close();
      file.clear();
    }




as you may see file is an instance of ofstream and analog_input is the data which is read from analog pins. I have a DS1307 which gives me the real time and i get _hour and _minute and _second from that.
i use this code as a function and call it whenever i need it which is pretty much more than 50 times a second i think.
I don't see why, but i keep receiving the error codes on my LCD.
file_name is the date like 20130716.txt.
I use this function to open the file and append to it. i have another function to make the file_name and make another file for me in case day or month or year have changed.


Code: [Select]


    _year=RTC.get(DS1307_YR,true);
    _month=RTC.get(DS1307_MTH,true);
    _day=RTC.get(DS1307_DATE,true);
      itoa(_year,name1,10);
      itoa(_month,name2,10);
      itoa(_day,name3,10);
      if (name2[1]=='\0')
      {
        key=name2[0];
        name2[0]='0';
        name2[1]=key;
      }
      if (name3[1]=='\0')
      {
        key=name3[0];
        name3[0]='0';
        name3[1]=key;
      }
      for (byte i=0;i<4;i++) file_name[i]=name1[i];
      file_name[4]=name2[0];
      file_name[5]=name2[1];
      file_name[6]=name3[0];
      file_name[7]=name3[1];
      file_name[8]='.';
      file_name[9]='t';
      file_name[10]='x';
      file_name[11]='t';
      if (sd.exists(file_name)) return;
      logfile.open(file_name, ios::app | ios::out);
      if (!file.is_open())
      {
        GLCD.CursorTo(0,1);
        GLCD.print("make error!!");
      }
      logfile << "some text1" << endl;
      logfile << "some text2" << endl;
      logfile << "some text3" <<endl;
      if (!file)
      {
        GLCD.CursorTo(0,2);
        GLCD.print("file error!!");
      }
      file.close();
      file.clear();
      logfile.close();





i use this code to make another file. at first run this code works pretty fine and makes me a file. but when i change date and call this function i start getting the error codes i have defined on LCD and no file is made for me. when i reset the arduino it make me my new file because the date that i have changed stays in DS1307.
can anyone make any sense of this program and tell me what i have done wrong?
any help is greatly appreciated.


Thanks everyone who viewed this topic and everyone who had an answer for but didn't get time to reply me.
I found mt own answer. the answer is the null character at the end of the file_name. That's it. the dumbest thing that you may ever think of. and of course stuck me for a whole week to find the answer.
I hope you keep that in mind and don't stuck like me.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy