Accelerologger: error opening test.txt problem!!!

Now I have solved my silly mistake I have run into another problem!!!

I looked in the serial monitor and I am getting the:

error opening test.txt

Part of the code being carried out! Here is a Fritzing diagram of the Accelerologger:

The diagram is a bit messy but it’ll do to show the circuit config. I ran the default SD Card Reader code on it:

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

File myFile;

int pinCS = 10;

void setup() {

Serial.begin(9600);
pinMode(pinCS, OUTPUT);

// SD Card Initialization
if (SD.begin())
{
Serial.println("SD card is ready to use.");
} else
{
Serial.println("SD card initialization failed");
return;
}

// Create/Open file
myFile = SD.open("test.txt", FILE_WRITE);

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

// Reading the file
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("Read:");
// Reading the whole file
while (myFile.available()) {
Serial.write(myFile.read());
}
myFile.close();
}
else {
Serial.println("error opening test.txt");
}

}
void loop() {
// empty
}

With this code, it worked perfectly. Here is the response received in the serial monitor:

SD card is ready to use.
Writing to file…
Done.
Read:
Testing text 1, 2,3…
Testing text 1, 2,3…
Testing text 1, 2,3…
Testing text 1, 2,3…
Testing text 1, 2,3…
Testing text 1, 2,3…

Geez!!! all these problems! I hope I eventually get to the bottom of them all!

Thanks for everyone’s help!

EJBuilder

6 Replies to “Accelerologger: error opening test.txt problem!!!”

  1. Yeah I did, works perfectly. Here is here is the standard code:

    #include
    #include

    File myFile;

    int pinCS = 10;

    void setup() {

    Serial.begin(9600);
    pinMode(pinCS, OUTPUT);

    // SD Card Initialization
    if (SD.begin())
    {
    Serial.println(“SD card is ready to use.”);
    } else
    {
    Serial.println(“SD card initialization failed”);
    return;
    }

    // Create/Open file
    myFile = SD.open(“test.txt”, FILE_WRITE);

    // if the file opened okay, write to it:
    if (myFile) {
    Serial.println(“Writing to file…”);
    // Write to file
    myFile.println(“Testing text 1, 2 ,3…”);
    myFile.close(); // close the file
    Serial.println(“Done.”);
    }
    // if the file didn’t open, print an error:
    else {
    Serial.println(“error opening test.txt”);
    }

    // Reading the file
    myFile = SD.open(“test.txt”);
    if (myFile) {
    Serial.println(“Read:”);
    // Reading the whole file
    while (myFile.available()) {
    Serial.write(myFile.read());
    }
    myFile.close();
    }
    else {
    Serial.println(“error opening test.txt”);
    }

    }

    Here is the response in the serial monitor:

    SD card is ready to use.
    Writing to file…
    Done.
    Read:
    Testing text 1, 2 ,3…
    Testing text 1, 2 ,3…
    Testing text 1, 2 ,3…
    Testing text 1, 2 ,3…
    Testing text 1, 2 ,3…

    I hope we can figure this out!

    EJBuilder

  2. @EJBuilder: So.. I think you’ve got a test code to write to the SD card, but the older code (from your earlier post) is not working. Is that right?

    Could you post the current code that you have, for the accelerologger? Do you have a github account you could leave a gist on?

  3. Hi again,

    So… we went through the code at the Robotics Lab today and I think that I see what the problem is: need to open the File before you attempt to println to it. In the (working) sample code above, it opend the myFile object in the setup() function. Need to do that with your code too.

    Take a look at my gist, Revision 2, line 27:

    https://gist.github.com/sinewalker/d552648e5835f557898f8e32b7eb4dab/cb224197740723199deb8448a5d2575dd0a3b620#file-accellerologger-ino-L27

    Also check out Revision 3 where I add more to the logging:

    https://gist.github.com/sinewalker/d552648e5835f557898f8e32b7eb4dab/15142799af36646607152c8c071550c173fa5fc6#file-accellerologger-ino-L45

Leave a Reply