Ethernet Shield /w microSD + Arduino 168 = problems writing files to SD

I’m using the sdFat library (http://code.google.com/p/sdfatlib/) to get my Arduino to communicate with the microSD card reader.

I had a bunch of problems even getting it to write to the card, but now that works. The code I’m using below is a stripped down version of the “list files” example sketch from the library and some stuff from the “readwrite” example too.

So what happens with the following code is this : it lists the files no problem, but when I sent a byte through serial, it hangs after having created the file, and do NOT see the “made a new file” message. However, when I check on the SD card (or run the script again), I see the new file that has successfully been written.

Any ideas why the code would hang after the “file.println(millis());” line?

Another weird thing - when I send that byte through serial, the whole sketch somehow gets excuted - i.e. I see the whole file listing again, going through the setup again?

Thanks for the help!

#include <SdFat.h>

const uint8_t chipSelect = 4;

SdFat sd;

SdFile file;
#define error(s) sd.errorHalt_P(PSTR(s))

ArduinoOutStream cout(Serial);

void setup() {
  char name[13];

  Serial.begin(9600);

  if (!sd.init(SPI_HALF_SPEED, chipSelect)) sd.initErrorHalt();
  
  while (file.openNext(sd.vwd(), O_READ)) {
    file.getFilename(name);
    cout << name << endl;
    file.close();
  }
  cout << "Done" << endl;
}
void maketest(){
  SdFile file("test.txt", O_WRITE | O_CREAT);
  file.println(millis());
  Serial.println("made a new file");
  file.close();
}

void loop() {
  while (Serial.read() > 0) {
    maketest();
  }
}

A delay in the loop or maketest function may help, also nothing is resetting the Serial.read() so it will always come in as greater than 0 after the first input. Let me know if it works, if it doesn't, I can't help you further as I don't have an Ethernet shield nor an Arduino 168.

The Ethernet library needs a fair amount of memory. The SdFat library needs memory. The streaming library needs memory. The 168 chip does not have all that much memory to go around. I suspect that you are running out of memory.

Hmm. PaulS, you might be right. There’s been some strange things happening with this library - though I have to use it, as all the other ones can’t initialize any of my SD cards.

PoLoMoTo, I tried your suggestion, but not much luck. I broke down the code and isolated where the issues happen, here’s the new version.

#include <SdFat.h>
const uint8_t chipSelect = 4;
SdFat sd;
SdFile file;
ArduinoOutStream cout(Serial);
void setup() {
  Serial.begin(9600);
  if (!sd.init(SPI_HALF_SPEED, chipSelect)) cout << "cant init card" << endl;;
  cout << "all set..." << endl;
}

int maketest(){
  int time = millis();
  char filename[10];
  itoa(time,filename,10);
  cout << filename << endl;
  file.open(filename, O_WRITE | O_CREAT);
  //file.println("some content");
  file.close();
}

void loop() {
  if (Serial.available() > 0 && Serial.read() > 0) {
    delay(300);
    maketest();
  }
}

So the 3 lines

file.open(filename, O_WRITE | O_CREAT);
  //file.println("some content");
  file.close();

is where it all happens. If I take them out, no problem the sketch runs just as it should. Here’s the strange thing. If I have “file.println(“some content”)” commented out, my setup() runs again, as mentioned in the first post (Perhaps worth mentioning the files DO get written tho). If I leave it uncommented, the whole thing just hangs after writing the file, and I have to restart it all.

So…getting a new Arduino /w more memory might be good idea perhaps.

thx guys

So...getting a new Arduino /w more memory might be good idea perhaps.

There is a FreeMemory() function that you could search for that will tell you how much memory you actually have available. Anything less that about 200 bytes indicates that you need a new Arduino.