Go Down

Topic: [SD] Weird-Unable to write into files, reading works ! (Read 574 times) previous topic - next topic

Hi all!

I am having quite strange problem with my current project.

I am using Arduino Mega 2560r3 with RTC (I2C), SD (SPI) and B/W dot matrix lcd (UART).

Basic operation of my project - read 8 analog samples, create average and write it into SD with timestamp.

Finally connected SD module with compatible SD (worst experience ever - tried 8 cards and only the last one worked!!!). Checked it with "CardInfo" sketch.

THE PROBLEM: My program reads current date from RTC and creates string which is used as filename. But program only writes s file, but nothing into it! It is still 0 bytes... If I change filename, file is written, but again - empty.
Once I have done "something" and everything worked correctly - had data in files. Later againt "something" happened and now I have again empty files :(

I thought about some bad connections (something like WR - write protection), but SD doesnt have anything like this.

Could it be software related? I am suspicious about function "sprintf()" that caused some problems early...

Thanks for any ideas!



I may post full code, but dont want to paste all (over 300 lines and still growing - not finished), so here is part of code that writes into SD:


Code: [Select]

//filename
  sprintf (DateField, "20%02u%02u%02u.CSV",year,date,month); // http://forum.arduino.cc/index.php?PHPSESSID=go7j8f5fpvr23ooqtbr23lnmd0&topic=106697.msg800508#msg800508
  mydisp.setPrintPos(0, 5);
  mydisp.print("FILE: ");
  mydisp.print(DateField);
  mydisp.print("  ");
  //ukladani do souboru
  String dataString = "";
  File dataFile = SD.open(DateField, FILE_WRITE);
  dataString += String(mVvalueInt);
  // if the file is available, write to it:
  if (dataFile) {
    SDerror=false;
    if (FirstStart==true) {
      dataFile.println("SYSTEM ONLINE");
      //dataFile.println("FILENAME     - TIME     - DOSE (nSv/h)");
    }
    dataFile.print(DateField);
    dataFile.print(" - ");
    sprintf (TimeField, "%02u:%02u:%02u",hour,minute,second);
    dataFile.print(TimeField);
    dataFile.print(" - ");
    dataFile.print(dataString);
    dataFile.println("");
    dataFile.close();
  } 
  // if the file isn't open, pop up an error:
  else {
    SDerror=true;
  }



OK, so did some tests and tweaks:

- Tried slow down SPI speed to HALF and QUARTER, no luck.
- Also checked all resistors onboard and they are 10k as marked.
- Twisted data wires and added a ferrite core filter.

- Finally checked SD with fdisk (on Kubuntu). Because after each write from arduino  card apperas in my notebook as read-only.  Fdisk reported to me there are clusters with out of range - doe to count of them, it seems that these are which arduino attempted to write into.

Here is output from fdisk:
Code: [Select]

helium@ThinkPad:~$ sudo fsck -a /dev/mmcblk0p1
fsck z util-linux 2.20.1
dosfsck 3.0.16, 01 Mar 2013, FAT32, LFN
FATs differ but appear to be intact. Using first FAT.
Cluster 20 out of range (135323653 > 501545). Setting to EOF.
Cluster 22 out of range (6309504 > 501545). Setting to EOF.
Cluster 23 out of range (6947848 > 501545). Setting to EOF.
Cluster 24 out of range (267911169 > 501545). Setting to EOF.
Cluster 25 out of range (268212223 > 501545). Setting to EOF.
Cluster 26 out of range (268208127 > 501545). Setting to EOF.
Cluster 27 out of range (268208127 > 501545). Setting to EOF.
Cluster 28 out of range (268234751 > 501545). Setting to EOF.
Cluster 29 out of range (268216319 > 501545). Setting to EOF.
Cluster 30 out of range (268224511 > 501545). Setting to EOF.
Cluster 31 out of range (268210175 > 501545). Setting to EOF.
Cluster 32 out of range (268208127 > 501545). Setting to EOF.
Cluster 33 out of range (268208127 > 501545). Setting to EOF.
Cluster 34 out of range (268208127 > 501545). Setting to EOF.
Cluster 35 out of range (268208127 > 501545). Setting to EOF.
Cluster 36 out of range (268208127 > 501545). Setting to EOF.
Cluster 37 out of range (268238847 > 501545). Setting to EOF.
Cluster 38 out of range (268224511 > 501545). Setting to EOF.
Cluster 39 out of range (268238847 > 501545). Setting to EOF.
Cluster 40 out of range (268404735 > 501545). Setting to EOF.
Cluster 41 out of range (9865215 > 501545). Setting to EOF.
Cluster 42 out of range (135405632 > 501545). Setting to EOF.
Cluster 43 out of range (234947076 > 501545). Setting to EOF.
Cluster 44 out of range (184582144 > 501545). Setting to EOF.
Cluster 45 out of range (152051904 > 501545). Setting to EOF.
Cluster 50 out of range (8401921 > 501545). Setting to EOF.
Cluster 51 out of range (268377888 > 501545). Setting to EOF.
Cluster 52 out of range (33880304 > 501545). Setting to EOF.
Cluster 53 out of range (1). Setting to EOF.
Cluster 54 out of range (2097216 > 501545). Setting to EOF.
Cluster 57 out of range (2113664 > 501545). Setting to EOF.
Cluster 58 out of range (67110912 > 501545). Setting to EOF.
Cluster 60 out of range (2113664 > 501545). Setting to EOF.
Cluster 63 out of range (4194432 > 501545). Setting to EOF.
Cluster 64 out of range (67637248 > 501545). Setting to EOF.
Cluster 66 out of range (2113664 > 501545). Setting to EOF.
Cluster 67 out of range (33816584 > 501545). Setting to EOF.
Cluster 69 out of range (2113664 > 501545). Setting to EOF.
Cluster 70 out of range (33816584 > 501545). Setting to EOF.
Cluster 72 out of range (2097216 > 501545). Setting to EOF.
Cluster 75 out of range (2113664 > 501545). Setting to EOF.
Cluster 76 out of range (524288 > 501545). Setting to EOF.
Cluster 79 out of range (134221856 > 501545). Setting to EOF.
Cluster 81 out of range (1). Setting to EOF.
Cluster 82 out of range (2113536 > 501545). Setting to EOF.
Cluster 83 out of range (33816584 > 501545). Setting to EOF.
Cluster 84 out of range (8388609 > 501545). Setting to EOF.
Cluster 85 out of range (1056768 > 501545). Setting to EOF.
Cluster 87 out of range (1). Setting to EOF.
Cluster 88 out of range (4194304 > 501545). Setting to EOF.
Cluster 89 out of range (67633168 > 501545). Setting to EOF.
Cluster 90 out of range (16777728 > 501545). Setting to EOF.
Cluster 91 out of range (4227072 > 501545). Setting to EOF.
Cluster 92 out of range (135266336 > 501545). Setting to EOF.
Cluster 93 out of range (16908292 > 501545). Setting to EOF.
Cluster 94 out of range (8388608 > 501545). Setting to EOF.
Cluster 95 out of range (135274560 > 501545). Setting to EOF.
Cluster 96 out of range (16909312 > 501545). Setting to EOF.
Cluster 97 out of range (4227072 > 501545). Setting to EOF.
Cluster 98 out of range (33818640 > 501545). Setting to EOF.
Cluster 100 out of range (1056832 > 501545). Setting to EOF.
Cluster 101 out of range (33555464 > 501545). Setting to EOF.
Cluster 104 out of range (33818640 > 501545). Setting to EOF.
Cluster 106 out of range (2113664 > 501545). Setting to EOF.
Cluster 108 out of range (8388609 > 501545). Setting to EOF.
Cluster 109 out of range (134221856 > 501545). Setting to EOF.
Cluster 111 out of range (8388609 > 501545). Setting to EOF.
Cluster 112 out of range (67637280 > 501545). Setting to EOF.
Cluster 115 out of range (135266336 > 501545). Setting to EOF.
Cluster 117 out of range (4227072 > 501545). Setting to EOF.
Cluster 118 out of range (135266336 > 501545). Setting to EOF.
Cluster 120 out of range (8388609 > 501545). Setting to EOF.
Cluster 121 out of range (135266336 > 501545). Setting to EOF.
Cluster 123 out of range (2113664 > 501545). Setting to EOF.
Cluster 124 out of range (67633168 > 501545). Setting to EOF.
Cluster 125 out of range (16777728 > 501545). Setting to EOF.
/test
  File size is 12288 bytes, cluster chain length is 4096 bytes.
  Truncating file to 4096 bytes.
/FSCK0001.REC
  Contains a free cluster (32790). Assuming EOF.
/FSCK0001.REC
  File size is 49152 bytes, cluster chain length is 36864 bytes.
  Truncating file to 36864 bytes.
Reclaimed 90 unused clusters (368640 bytes) in 89 chains.
Performing changes.
/dev/mmcblk0p1: 109 files, 126/501544 clusters

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