Go Down

Topic: [SOLVED] Can't open .txt file on SD card (Read 7378 times) previous topic - next topic

Power_Broker

Mar 31, 2017, 07:39 pm Last Edit: Mar 31, 2017, 07:58 pm by Power_Broker
Hello,

I'm using this SD card reader, this SD card (16GB - formatted to FAT32), and an Arduino Nano to read a .txt file from the SD card and print the contents to the serial monitor. My code is below.

Code: [Select]

/*
  This example shows how to read data to and from an SD card file
  The circuit:

   SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)
*/

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

File myFile;

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

  Serial.print("Initializing SD card...");
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  if (SD.exists("soundFile.txt")) {
    Serial.println("soundFile.txt exists.");
  } else {
    Serial.println("soundFile.txt doesn't exist.");
  }

  // open the file
  myFile = SD.open("soundFile.txt", FILE_READ);
  if (myFile) {
    Serial.println("soundFile.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 soundFile.txt");
  }
}

void loop() {
  // nothing happens after setup
  delay(2000);
}




Unfortunately, the code successfully initializes communication with the SD card, but for some reason, it throws an error when I try to open the text file to read:

Quote
Initializing SD card...initialization done.
soundFile.txt doesn't exist.
error opening soundFile.txt
All electrical connections are correct (grounds, etc) and the .txt has been successfully uploaded to the SD card (verified - including name spelling). In case anyone is interested, I have attached the .txt file I want to use.


This is my first time using an SD card, am I doing something wrong??? Any help is appreciated!
"The desire that guides me in all I do is the desire to harness the forces of nature to the service of mankind."
   - Nikola Tesla

Power_Broker

I found the answer here. Much thanks to LarryD!!

Looks like my file name had too many characters, lel.
"The desire that guides me in all I do is the desire to harness the forces of nature to the service of mankind."
   - Nikola Tesla

Go Up