This code just seems to Hang when SD Card is inserted (SDFat)

The attached code (it's too long to embed) collects data and write it to an SD Card. It is supposed to sleep until it is woken up by a real time clock (DS1305). Full project details here http://interestingto.me.uk/Open%20Source/low-cost-low-power-datalogger (n.b. the attached code is modified from the one on that page).

I just converted it to use SDFat from the SD library and now it hangs before it goes to sleep. The code gets as far as outputting the Flags status at line 297 and then appears to hang. If there is no SD Card connected then it works OK. I have tried various things and have run out of ideas, any thoughts???

RainGuage.ino (15.1 KB)

Bump...

anyone?

Do you actually try to insert the SD card while the program is running ? I may be very old-fashioned and still not used to "plug and play", I would try inserting the SD card before I turn the thing on.

Secondly, it may be more useful to post the relevant excerpts of your code. I read blogs and compile Arduino sketches on two different computers, and it is really too much hassle to download your .ino file, particularly as they don't even seem to be readable as text files.

I am not inserting the SDCard when it is running, I stop it to insert or remove the SD.

It's always a dilemma when posting to forums. If I posted sections of the code I would surely get a "post your whole code" comment but I guess you are right, I could do both.

INO files do open as text files (I just tested it), you just need to choose a text editor to open it with.

So this is the code that initializes the SD:

DEBUGPRINT("Initializing SD card...");
	pinMode(SD_CS, OUTPUT);   
	if (!sd.begin(SD_CS, SPI_HALF_SPEED)) {
            sdUp = false;
	    DEBUGPRINTLN("initialization failed!");
	}
        else {
            sdUp = true;
            DEBUGPRINTLN("SD initialization OK.");
        }
        
        //BMP085
        Wire.begin();
        if (bmpCheck())
        {
          bmp085Calibration();
        }

this is as far as i gets before stopping

void loop()
{
	// Get the time
	ds1305time t;
	rtc.getTime(&t);

	// Format time
	char datebuf[128];
	snprintf(datebuf, 128, "%02d/%02d/%02d %d:%02d:%02d",
		t.year,
		t.month,
		t.day,
		t.hours,
		t.minutes,
		t.seconds
		);

#ifdef DEBUG
	Serial.begin(9600);
#endif	

	// Output time
	DEBUGPRINTLN(datebuf);

	if((actionFlags & ACT_SETUP) == ACT_SETUP) {
                DEBUGPRINTLN("Start up");
		writeSDData(datebuf, "Start Up");
		//unset the flag
		actionFlags = actionFlags & ~ACT_SETUP;
	}

	if((actionFlags & ACT_INT0) == ACT_INT0) {
                DEBUGPRINTLN("Int 0 Flagged");
		intZeroAction(datebuf);
		//unset the flag
		actionFlags = actionFlags & ~ACT_INT0;
	}

	if((actionFlags & ACT_INT1) == ACT_INT1) {
                DEBUGPRINTLN("Int 1 Flagged");
		intOneAction(datebuf);
		//unset the flag
		actionFlags = actionFlags & ~ACT_INT1;
	}

#ifdef DEBUG
	Serial.print("Flags: ");
        Serial.print(actionFlags); Serial.print(" ");
        Serial.print(actionFlags & ACT_INT0, BIN); Serial.print(" ");
	Serial.println(actionFlags & ACT_INT1, BIN);
	Serial.end();

        delay(1000);

this is the code that writes to the SD

void writeSDData(char* datebuf, char* message) {
    if(sdUp) {
	// if the file opened okay, write to it:
	if (myFile.open("test.txt", O_RDWR | O_CREAT | O_AT_END)) {
		myFile.print(datebuf);
		myFile.print("; ");
		myFile.println(message);
		// close the file:
		DEBUGPRINTLN("Wrote to SD.");
                myFile.close();
	} else {
		// if the file didn't open, print an error:
		DEBUGPRINT("error opening " );  DEBUGPRINT(FILENAME);
	}
	
    }
}