I tried to do a data search on the data.txt file on the Arduino SD card...
but it can only be read if the data is in the first row (top), the second data and so on cannot be found...
I wonder why?
here is the code that I use
Thank You
/*
SD card read/write
This example shows how to read and write data to and from an SD card file
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
created Nov 2010
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
*/
#include <SD.h>
const int chipSelect = 4;
File myFile;
char buf[8];
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.print("Initializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
//pinMode(10, OUTPUT);
if (!SD.begin(chipSelect)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// re-open the file for reading:
myFile = SD.open("data.txt");
if (myFile) {
Serial.println("data.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
myFile.read(buf,8);
if(strncmp(buf,"18429874",8)==0)
{
Serial.println("Match!");
}
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop()
{
// nothing happens after setup
}
I moved your topic to an appropriate forum category @ranggajulizar .
In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.
may be a function like this would work. (typed here, totally untested)
assuming you have
#include <SdFat.h>
SdFat sd;
File file;
...
// in setup
if (!sd.begin()) return false; // Initialization failed
then the function could be
bool findStringInFile(const char *filename, const char *searchString, size_t &pos) {
file = sd.open(filename, FILE_READ);
if (!file) return false; // File not found
size_t searchLen = strlen(searchString);
size_t index = 0;
pos = 0;
while (file.available()) {
char ch = file.read();
if (ch == searchString[index]) {
index++;
if (index == searchLen) { // Match found
pos = file.position() - searchLen;
file.close();
return true;
}
} else {
index = (ch == searchString[0]) ? 1 : 0; // Reset index or start new match (not foolproof)
}
}
file.close();
return false; // String not found
}
you would use it like this:
size_t position;
if (findStringInFile("data.txt", "18429874", position)) {
Serial.print("String found at position: ");
Serial.println(position);
} else {
Serial.println("String not found");
}
When searching for data using the value sent from the Wiegand RFID, it seems like there are empty characters behind it...
so it doesn't match the values ββin the list.
How do I remove the empty characters??
I used the trim() command but it couldn't and an error like this appeared
if (findStringInFile("data.txt",wg.getCode(), position)) {
Serial.print("String found at position: ");
Serial.println(position);
} else {
Serial.println("String not found");
}
Compilation error: conversion from 'long unsigned int' to 'String' is ambiguous