Hi All,
Help with a little problem please as I'm quite new to Arduino.
I have connected to my Arduino R4 an SD card read/writer. On the micro SD card I have two text files:- MUSIC1.TXT and MUSIC2.TXT. One has 13 entries on it and the other 5.
When I read a file into the Arduino program, it reads and displays fine. If I press the reset button on the Arduino, it then repeats to read and display without any problems. If I then choose the other file the file doesn't open and it fails. If I press the reset it continues to fail every time. However, if I remove the SD card from the holder and re-insert it, it then works fine again every time. It is if the file system on the SD card remains open for the previous SD card file. I have inserted the command myFile.flush() prior to myFile.close() to no avail.
The SD card is a SanDisk 32GB HC A1. It is formatted to FAT32.
Any ideas?
#include <SD.h>
const int chipSelect = 10;
File myFile;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
// wait for Serial Monitor to connect. Needed for native USB port boards only:
while (!Serial);
// Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("initialization failed. Things to check:");
Serial.println("1. is a card inserted?");
Serial.println("2. is your wiring correct?");
Serial.println("3. did you change the chipSelect pin to match your shield or module?");
Serial.println("Note: press reset button on the board and reopen this Serial Monitor after fixing your issue!");
while (true);
}
// re-open the file for reading:
myFile = SD.open("MUSIC1.TXT");
if (myFile) {
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.flush();
myFile.close();
// end();
} else {
// if the file didn't open, print an error:
Serial.println("Error opening file");
}
}
void loop() {
// put your main code here, to run repeatedly:
That is not in a <CODE/ > tag. You can also use the IDE Edit/Copy for forum. Put it in a NEW post as a reply to this will be fine. Changing original posts is not advised.
#include <SD.h>
const int chipSelect = 10;
File myFile;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
// wait for Serial Monitor to connect. Needed for native USB port boards only:
while (!Serial);
// Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("initialization failed. Things to check:");
Serial.println("1. is a card inserted?");
Serial.println("2. is your wiring correct?");
Serial.println("3. did you change the chipSelect pin to match your shield or module?");
Serial.println("Note: press reset button on the board and reopen this Serial Monitor after fixing your issue!");
while (true);
}
// re-open the file for reading:
myFile = SD.open("MUSIC1.TXT");
if (myFile) {
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.flush();
myFile.close();
// end();
} else {
// if the file didn't open, print an error:
Serial.println("Error opening file");
}
}
void loop() {
// put your main code here, to run repeatedly:
}
I'm still messing with the software, so I'm manually changing the textfile name to the other one then recompiling. The changeover software comes later. The Arduino board I am using is the Arduino UNO R4 WIFI. The Micro SD card adapter is just one of the standard ones with a voltage regulator on it and an IC.
How about you first just open the file that is actually present.
Although it may be possible to open a file while the initialization was done while a different SD-card was inserted, it is better practice to re-initialize (!SD.begin(chipSelect)) after swapping SD-cards.
For now i would check to see if either of the 2 files you are looking for is present, and if not, then re-initialize.
The two files are both on the same SD card and are the only files on the card.
If I tell the software to look for MUSIC1.TXT and it's the first time after powering up and inserting the card it is fine. No matter how many times I press the Arduino reset button, it is still fine.
If I then leave the power on, leave the SD inserted, change the software to select MUSIC2.TXT and then recompile, the file fails to open every time. Even if I press the reset.
If I leave the power on but remove the SD card and re-insert it, everything is fine again.
I'm not quite sure what you mean by "the problem is obvious" (thanks for the good wishes)..
Obvious as in NOT sketch, NOT any hardware. That only leaves the files.
Test this way.
Power down, remove card, insert card, power up, change sketch to MUSIC2.TXT, upload the sketch. If it does not work the file is bad.
If it does work, do the following.
Power down, remove card, insert card, power up, change sketch to MUSIC1.TXT, upload the sketch. If it does not work the file is bad.
If it does work, your previous testing methodology is flawed.
Thanks for that. I'm afraid that I am down to the last one "my testing methodology is flawed".
I think my problem was that I was leaving the power on and leaving the card inserted whilst changing over the software file selection. I doesn't seem to like that.
interesting...
To clarify the problem, could you make another test?
Do the same as above, but this time do not change the filename, but do any minor change - for example, add a space or an empty line where it doesn't affect anything. The point is to make the new code different from the old one, forcing the compiler to rebuild the project. But don't change the file name.
Upload the new code and check whether the file remains accessible or whether an error occurs, as in your tests.