Problem SD Card and Bluetooth

Hey guys, this is my code:

#include <SoftwareSerial.h>

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

File myFile;

const int ON = 1;
const int OFF = 0;

static int state = 0;

SoftwareSerial mySerial(10, 11); // RX, TX
int LED = 7;
char character;
String a;

void setup()  
{
  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
  pinMode(LED, OUTPUT);

  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  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.");

  myFile = SD.open("a.txt");
  if (myFile) {
    while (myFile.available()) {
      character = myFile.read();
      a.concat(character);
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening a.txt");
  }
}

void loop() // run over and over
{
    String Data = "";

    while(mySerial.available()) {
       character = mySerial.read();
       Data.concat(character);
       delay (10);
    }

    if (Data == a) 
      if (state == OFF)
      {
        digitalWrite(LED, HIGH);
        state = ON;
      }
      else
      {
        digitalWrite(LED, LOW);
        state = OFF;
      }
}

I'm doing an app to turn on a led if I send the right "password". When I remove the code about reading a file from an SD it work, but with that part it's like it doesn't enter the while in the loop() cause of SD.begin(4) (I guess). I'm new at these things, can you help me? Thanks.

If you look at any example in the IDE using the SD card, you will see that pin 10 must be set as output. If you use a Mega, it is pin 53. You do neither. If you must use software serial, it might be a good idea not to use pin 10.

Nick_Pyner:
If you look at any example in the IDE using the SD card, you will see that pin 10 must be set as output. If you use a Mega, it is pin 53. You do neither. If you must use software serial, it might be a good idea not to use pin 10.

Thanks for the answer. I've tried using SoftwareSerial mySerial(11, 12); but still doesn't work. I'm using Arduino Uno.

Check the dumpfile example in the IDE.

Have you set pin 10 as output? Not using pin 10 for software serial might not be a good idea, but failing to call it as output is definitely a bad idea.

Nick_Pyner:
Check the dumpfile example in the IDE.

Have you set pin 10 as output? Not using pin 10 for software serial might not be a good idea, but failing to call it as output is definitely a bad idea.

Do I have to set it with pinMode?

You can't use D10 to D13 for SoftwareSerial in your application. Those are the SPI pins. The SD card uses those.

Kiyo5:
Do I have to set it with pinMode?

Yes. As I said, check examples in the IDE and, as Surfer says, stay away from those pins for software serial.

Solved, thanks!