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.