Can't print to empty file.

Few odd issues.

A) I can't seem to be able to create a new file. This is intermittent. Sometimes the new file is created.
B) I can't write to an empty file using print(). This is NOT intermittent. Just can't do it.
C) If there is any data in the file? Everything seems to work fine.

This came up working on a library file for event logging. So I pulled out the offending part and set it up in its own .ino file to demonstrate the issue. Can anyone have a look?

#include "SD.h"


#define SD_CS     4
#define MAX_BYTES 200

char  dataBuff[MAX_BYTES+1];
int   i;

// These guys are here to emulate the stuff from the class this stuff was pulled from.
char mPath[] = "/logTest.txt";
bool getLogging() { return true; }
int mSeries = 1;


void setup() {

   i = 0;
   Serial.begin(9600);
   
   if (!SD.begin(SD_CS)) {
      Serial.println("NO SD CARD!");
      while(1);
   }
   Serial.println("Logging's up and running.");
}


bool addEvent(char* eventTxt) {
   
   File  logFile;
   bool  success;
   int   gSec = 99; // Also from the class.
   
   success = false;                                   // Not been a success yet.
   if (getLogging()) {                                // If we -are- logging..
      logFile = SD.open(mPath, FILE_WRITE);           // Lets try to open/create the logfile.
      if (logFile) {                                  // If we had success..

         Serial.print(mSeries);Serial.print('\t');
         Serial.print(gSec);Serial.print('\t');
         Serial.println(eventTxt);  
         
         logFile.print(mSeries);logFile.print('\t');  // First we print out the series number.
         logFile.print(gSec);logFile.print('\t');     // Next the number of seconds in this series.
         logFile.println(eventTxt);                   // And whatever their data ends up being.
         logFile.close();                             // Close the file. (Always leave files closed!)
         success = true;
      }
   }
   return success;
}


// Looks for a string from the terminal. Puts that into the event file.
void loop() {

  char   inChar;
    
  if (Serial.available()) { 
      inChar = Serial.read();                       
      if (inChar=='\n') {
         dataBuff[i] = '\0';
         if (addEvent(dataBuff)) {
            Serial.print(dataBuff);Serial.println(", added to event log.");
         } else {
            Serial.println("No data added.");
         }
         i = 0;
      } else if (i<MAX_BYTES) {
         dataBuff[i] = inChar;
         i++;
      }
   }
}

There you go. Change the chip select to match your system, and it should compile and run.

Thanks millions!

-jim lee

(deleted)

Ran it, its all happy about the SD cartd.

-jim lee

Is you card formatted in the approved manner? There is a sticky about this at the head of the forum.

Yes, I've tried a couple different cards and they are all cards I've used before. Also, If the file is present. with at least a little data in it. everything works fine. So the wiring seems good as does the formatting of the card. I think the deal is that after the first call to open() to a non-existent or empty file, you can't print() to it.

-jim lee

(deleted)

I'm sure you don't need me to tell you that this makes no sense.... If you want to send data to a file that doesn't exist, Arduino creates the file and sends the data. That's the deal. I don't understand the code, but I'm sure a lot of it is extraneous to the problem in hand. You might try a simple "hello new file" with a count as the file name. The only code I would actually question is the absence of any proper timing in the loop - it all seems to be down to if, buts, and luck, and maybe Arduino is falling over its own feet. It might feel more comfortable with a delay(1000); as the last line. Just a guess...

I'm sure you don't need me to tell you that this makes no sense.... If you want to send data to a file that doesn't exist, Arduino creates the file and sends the data. That's the deal.

Hahah! Well of course! That's way I posted it. It seemed extremely odd to me. I'd hoped someone would try it and see if it failed on their system. I guess no one here tried that.

But luckily someone on the Teensy forum did. And it all worked as it was supposed to. Helping in my search.

And in the end, I was able to solve it.

The SD drives I've been using are mounted on an Adafruit 1947 Cap. touchscreens. Theoretically, its just sitting there. I found out that, if you DON'T initialize the screen, even if you don't plan on using it, the SD drive starts acting oddly. This results in failing to create new files occasionally, and not being able to write to empty files at all.

Initialize the screen drivers, the issue corrects itself.

-jimlee

Wow. I thought SDs on screens were quite separate from the display and were there just because it was some vacant real estate.

Glad you solved it. I would suggest that you add an else to [iif (logFile)[/i] and print a message; that would probably have given you a little more to work on :wink: