How to read data on SD Card and compare them with some parameter? Please see a code below here
I need to create new text file on SD Card > Save 4 characters text from serial into text file on SD Card> Read text file> Compare value in text file with string ("AAAA") on code. But when I did a process that be described earlier. The result show unable to read text AAAA from file on SD Card always. What wrong with my code?
if (Serial.available() > 0) {
Serial.println("Creating example.txt...");
myFile = SD.open("example.txt", FILE_WRITE);
myFile.close();
myFile = SD.open("example.txt", FILE_WRITE);
int f;
char serialdata[4];
for (f = 0; f < 4; f++) {
serialdata[f] = Serial.read();
}
serialdata[4] = '\0';
delay(2000);
myFile.println(serialdata);
Serial.println("Write a massage ");
Serial.println(serialdata);
Serial.println(" into example.txt...");
myFile.close();
myFile = SD.open("example.txt", FILE_WRITE);
int e;
char command[4];
for (e = 0; e < 4; e++) {
command[e] = myFile.read();
delay(2000);
}
command[4] = '\0';
delay(2000);
Serial.write(command);
if (strcmp(command, "AAAA") == 0) {
Serial.print(" I get massage AAAA.");
} else {
Serial.print(" I don't get a massage.");
}
myFile.close();
}
wildbill:
Probably that you're checking whether there's one character available and very shortly afterwards reading four.
Changing to this might improve matters:
if (Serial.available() > 3) {
What does the file contain - read it in something other than the arduino, preferably with a hex editor.
Thank you for your help. I have more question. As you see my source code, when I key Character "AAAA" into Serial Text box. then this Character save into file example.txt in a SD Card. Finally Arduino read data from example.txt the result show weird Character. Why It didn't display "AAAA" ?
if (Serial.available() > 0) {
Serial.println("Creating example.txt...");
myFile = SD.open("example.txt", FILE_WRITE);
myFile.close();
myFile = SD.open("example.txt", FILE_WRITE);
int f;
char serialdata[4];
for (f = 0; f < 4; f++) {
serialdata[f] = Serial.read();
}
serialdata[4] = '\0';
delay(2000);
myFile.println(serialdata);
Serial.println("Write a massage ");
Serial.println(serialdata);
Serial.println(" into example.txt...");
myFile.close();
myFile = SD.open("example.txt", FILE_WRITE);
int e;
char command[4];
for (e = 0; e < 4; e++) {
command[e] = myFile.read();
delay(2000);
}
command[4] = '\0';
delay(2000);
Serial.write(command);
if (strcmp(command, "AAAA") == 0) {
Serial.print(" I get massage AAAA.");
} else {
Serial.print(" I don't get a massage.");
}
myFile.close();
}
As you see in my source code, First I create example.txt in Sd card > Second Keep Txt in Serial text box into example.txt >Third make uno read text I already wrote into example.txt > Forth show text from example.txt on Serial monitor windows. So a result is wrong , wrote AAAA into example.txt. But Uno read it as any symbol.
As you see in my source code, First I create example.txt in Sd card
You open and close the file if there is one byte of serial data available. Then, you read 4 of the 1 characters available. Then, you write beyond the end of the array. Then, you close a file that is already closed. Then, you read 4 bytes from an empty file. Then, you write beyond the end of that array.
It would have taken less time to point out what you did right.