Go Down

Topic: ReadWrite example stops working when file name is changed... (Read 274 times) previous topic - next topic

smatchymo

I feel like the answer to this has to be obvious and I'm going to feel really stupid.  I tried integrating my first sd card into my first project.  I tried the example ReadWrite by itself first and that worked fine.  I then incorporated that into my code, but it wouldn't create the text file.  I then went back to the ReadWrite example and the only change that I made was to the file name.  I changed it from test.txt to waterData.txt and it had the same problem.  It wouldn't create the text file.

Here is the waterData code.  This gives me the following output in the serial monitor.

Initializing SD card...initialization done.
error opening waterData.txt
error opening waterData.txt

Code: [Select]
#include <SPI.h>
#include <SD.h>

File myFile;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);

  Serial.print("Initializing SD card...");

  if (!SD.begin(53)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("waterData.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to waterData.txt...");
    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening waterData.txt");
  }

  // re-open the file for reading:
  myFile = SD.open("waterData.txt");
  if (myFile) {
    Serial.println("waterData.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening waterData.txt");
  }
}

void loop() {
  // nothing happens after setup
}


Here is the test code.  This gives the following in serial monitor.

Initializing SD card...initialization done.
Writing to test.txt...done.
test.txt:
testing 1, 2, 3.

Code: [Select]

#include <SPI.h>
#include <SD.h>

File myFile;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);

  Serial.print("Initializing SD card...");

  if (!SD.begin(53)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop() {
  // nothing happens after setup
}


Is there something obvious that I'm overlooking?  I can't for the life of me think of why this change would cause such problems unless I keep making a typo or keep forgetting to change a file name somewhere in the code.

pert

From https://www.arduino.cc/en/Reference/SD:
Quote
It uses short 8.3 names for files.
This means the file name can be a maximum of 8 characters plus a maximum of a 3 character file extension. The filename you're trying to use is 9.3.

sterretje

From https://www.arduino.cc/en/Reference/SD:This means the file name can be a maximum of 8 characters plus a maximum of a 3 character file extension. The filename you're trying to use is 9.3.
The children of nowadays :)
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

smatchymo

I thought I had posted saying thank you a while back.  Not sure why that didn't post.  Thanks, though!


Go Up