Help,Arduino problem Sd Card double use of a pin out.

Guys i have stuck.
At void setup() i use SD.begin(4) to read a text file from Sd.
When the reading of Sd completes i want to play music from Sd card (SdPlay.setSDCSPin(4):wink: but i have already set pin(4) for reading the text.
Anyone knows how to reset the pin4 and use it again for listening music from sd?

code:
void setup() {
Serial.begin(9600);

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

if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
myFile = SD.open("test.txt", FILE_WRITE);
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");
myFile.close();
Serial.println("done.");
} else {
Serial.println("error opening test.txt");
}
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
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");
}
//----------------------------------------------------------------------------------------------------------
SdPlay.setSDCSPin(4); //------>Here problem caused
}

Pin 4 is the chip select for the SD card. It should be the same for any operation involving the SD card.

In both of those cases what the function wants is the pin number for the Chip Select pin of the SD card. Or so it would appear. The SD card is communicating over the SPI bus. Google "Arduino SPI" for more information.

SdPlay.setSDCSPin(4); //------>Here problem caused

But what IS the problem? If you no longer need to read from the SD card, then using that pin for some other purpose should not be a problem.

PaulS:

SdPlay.setSDCSPin(4); //------>Here problem caused

But what IS the problem? If you no longer need to read from the SD card, then using that pin for some other purpose should not be a problem.

just it stop the program there.
nothing happend after this line

Post the complete code. Use code tags this time please. Read the "How to use this forum" thread for info on code tags. Please also format the code before posting. Press control-T in the Arduino IDE and it will auto-format for you.

Delta_G:
Post the complete code. Use code tags this time please. Read the “How to use this forum” thread for info on code tags. Please also format the code before posting. Press control-T in the Arduino IDE and it will auto-format for you.

Program code with Cntrl+T:
#include <string.h>
#include <stdio.h>
#include <SimpleSDAudio.h>
#include <SPI.h>
#include <SD.h>

File myFile;

void setup() {
Serial.begin(9600);
//----------------READ AND WRITE TO SD CARD-------------------------
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print(“Initializing SD card…”);

if (!SD.begin(4)) {
Serial.println(“initialization failed!”);
return;
}
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”);
}

//---------------PLAY MUSIC FROM SD------------------
Serial.println(“Try to set pin4”);
SdPlay.setSDCSPin(4); // sd card cs pin
Serial.println(“I set pin4”);
if (!SdPlay.init(SSDA_MODE_FULLRATE | SSDA_MODE_MONO | SSDA_MODE_AUTOWORKER))
{
while (1);
}
Serial.println(“I pass the problem”);//it doesn’t appear
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(10, OUTPUT);

Serial.println(“start”);
}

void loop() {
// put your main code here, to run repeatedly:

}

And the Serial monitor output is :

Initializing SD card…initialization done.
Writing to test.txt…done.
test.txt:
testing 1, 2, 3.
Try to set pin4
I set pin4

Use code tags this time please. Read the "How to use this forum" thread for info on code tags.

Please. Making it harder to read and work with your code isn't going to get more help.

Doesn't look like your code chokes on that particular line. Looks like it gets hung up trying to initialize the SdPlay object.

i use this Scematic for Sd:

Program code with Cntrl+T:

#include <string.h>
#include <stdio.h>
#include <SimpleSDAudio.h>
#include <SPI.h>
#include <SD.h>

File myFile;

void setup() {
 Serial.begin(9600);
 //----------------READ AND WRITE TO SD CARD-------------------------
 while (!Serial) {
   ; // wait for serial port to connect. Needed for native USB port only
 }
 Serial.print("Initializing SD card...");

 if (!SD.begin(4)) {
   Serial.println("initialization failed!");
   return;
 }
 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");
 }
 
 //---------------PLAY MUSIC FROM SD------------------
 Serial.println("Try to set pin4");
 SdPlay.setSDCSPin(4); // sd card cs pin
 Serial.println("I set pin4");
 if (!SdPlay.init(SSDA_MODE_FULLRATE | SSDA_MODE_MONO | SSDA_MODE_AUTOWORKER))
 {
   while (1);
 }
  Serial.println("I pass the problem");//it doesn't appear
 pinMode(2, OUTPUT);
 pinMode(3, OUTPUT);
 pinMode(5, OUTPUT);
 pinMode(6, OUTPUT);
 pinMode(7, OUTPUT);
 pinMode(8, OUTPUT);
 pinMode(10, OUTPUT);

 Serial.println("start");
}

void loop() {
 // put your main code here, to run repeatedly:

}

And the Serial monitor output is :

Initializing SD card…initialization done.
Writing to test.txt…done.
test.txt:
testing 1, 2, 3.
Try to set pin4
I set pin4

From this page:

SdPlay.init fails Have you selected the correct CS pin for your SD-card? Uncomment the line
// SdPlay.setSDCSPin(10);
in the examples and enter correct pin number here.
Your SD shield may be crap and SD communication is only possible with limited speed. Try commenting the line
SPSR |= (1 << SPI2X);
in function SD_L0_SpiSetHighSpeed(). If this doesn’t help, comment out also the first line and try again. If then init works you have a bad SD card shield.

Have you tried any of the library examples?

 if (!SdPlay.init(SSDA_MODE_FULLRATE | SSDA_MODE_MONO | SSDA_MODE_AUTOWORKER))
 {
   while (1);
 }

You could, you know, print the value returned by the init() method. Perhaps a clue-by-four would present itself.