imranahmed
Advanced Member level 3
- Joined
- Dec 4, 2011
- Messages
- 817
- Helped
- 3
- Reputation
- 6
- Reaction score
- 3
- Trophy points
- 1,298
- Location
- Karachi,Pakistan
- Activity points
- 6,492
Hi,
What's the problem?
SD card hardware?
File system?
Interface?
Microcontroller?
Software?
Anything else?
You are not the first one to read a SD card with a microcontroller. Thus there are many descriptions, examples code examples, application notes, videos...
Klaus
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <SD.h>
File myFile;
String incomingString = "2741#";
String myString = "";
unsigned int myArrayIndex = 0;
LiquidCrystal_I2C lcd(0x27, 20, 4);
void setup() {
if (!SD.begin(10)) { //make sure sd card was found
while (true);
}
lcd.begin();
lcd.setCursor(0, 0);
lcd.print("DRIVERID.txt");
delay(2000);
lcd.clear();
}
void loop() {
myFile = SD.open("driverid.txt"); // open the file for reading
lcd.setCursor(0, 0); //
//lcd.autoscroll(); //enable auto-scrolling
if (myFile) {
while (myFile.available()) { //execute while file is available
String letter = myFile.readString(); //read next character from file
if (letter == "2741#"){
lcd.print(letter);
}
// delay(80);
}
myFile.close(); //close file
}
// lcd.clear();
}
Still, no specific question.
Why not sént few more words to depict what exactly is working or not ?
Yes SD Card working properly and question is not specific to SD card reading itself, it is reading good but I want to read specific driverid from file driverid.txt.Ok, there is some advance in the question.
At least now we can presume that SD reading is working properly and the question is not specific to SD card reading itself.
Are you aware of the C standard String libraries ?
Whith these funcions you can find text from another.
please give hint, I am stuck
I found many articles most of them using example of Arduino IDE of SD Card but I do not need it.There are many articles on the Web with comprehensive tutorials., why not start doing so ?
You have the above String variable "letter" within which you should seek for the keyword you need to find.
Your question is still unclear.I found many articles most of them using example of Arduino IDE of SD Card but I do not need it.
I want to read only 5805# in driverid.txt, but is it not working it is only read first driverid 2741# only.
Thank you I am trying it sorry I am not good in English but please try to understand.Your question is still unclear.
I said the question was no longer related to SD card, but rather to string manipulation
BTW, the purpose of this forum is people ask for help, providing as many information as possible as expect for tips.
Since you did not seem able how to find the answer, consider doing that:
Replace this:
if (letter == "2741#")
By this:
if( letter .indexOf(" 5805#") > 0)
My best brother thank you very much now I am successful because of your support thank you brother, from past 6 hours I was stuck. You are my bestd brother.Your question is still unclear.
I said the question was no longer related to SD card, but rather to string manipulation
BTW, the purpose of this forum is people ask for help, providing as many information as possible and expect for tips.
Since you did not seem able how to find the answer, consider doing that:
Replace this:
- if (letter == "2741#")
By this:
- if( letter .indexOf(" 5805#") > 0)
No code, no guess...I wrote a code for reading data from SD Card but the function verify only runs first timer properly second time it is not working why?
No code, no guess...
#include <SPI.h>
#include <SD.h>
bool verify = false;
String myString = "";
String type = "AZ-7773#";
String ID = "Vehicle ID";
File myFile;
bool verifydata(String typeID, String ID) {
Serial.print("SCAN DATA:");
Serial.println(typeID);
Serial.println(ID + ".txt below Scan Data ");
if (ID.equals("Driver ID"))
{
Serial.println("OPEN DRIVER ID");
SDcardinitialize();
myFile = SD.open("DRIVERID.txt");
}
else if(ID.equals("Vehicle ID"))
{
Serial.println("OPEN TRUCK ID");
SDcardinitialize();
myFile = SD.open("TRUCKID.txt");
}
if (myFile)
{
Serial.println(ID + ".txt in myFile");
// read from the file until there's nothing else in it:
while (myFile.available())
{
myString = myFile.readString();
if (myString.indexOf(typeID) >= 0)
{
Serial.println("FOUND");
myString = "";
myFile.close();
return 1;
}
else
{
Serial.println("DOES NOT FOUND");
myString = "";
myFile.close();
return 0;
}
}
// close the file:
myFile.close();
}
else
{
// if the file didn't open, print an error:
Serial.println("error opening " + ID + ".txt");
}
}
void SDcardinitialize()
{
Serial.print("Initializing SD card...");
if (!SD.begin(10)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
}
void setup() {
Serial.begin(9600);
SDcardinitialize();
delay(2000);
}
void loop() {
verify = verifydata(type , ID);
if(verify == 0){
Serial.print("Not Verify ");
Serial.println(verify);
}else{
Serial.print("Verified ");
Serial.println(verify);
}
delay(4000);
}
But I closed the file after reading in IF ELSE condition but is it not working, only first time running but after that showing un-expected behaviour.At a glance, once you are repeatedly calling the SD.open() funtion, seems like you missed the SD.close() function somewhere.
I tried many options but I do not know why it is not working, it is seems like no bugs but do not know why it is not working please ask to any expert.You close the file, however you cyclically instantiate the SD.begin() without having closed it. One thing is the SD card instance ( e.g: you could read more than one SD card in your application ), other thing is the file instance ( you could have several files in your card ). As said, seems like the last instance was not closed when you try to 'begin' another one after each delay.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?