Hi,
I am new to arduino and am a fair novice in programing.
I will try to explain what my problem is as best as possible.
I am using the arduino uno along with the arduino ethernet shield which has the micro SD slot, the ID-20 RFID reader and the I2C/TWI LCD1602 Module. I am trying to create an RFID attendance device.
The problem I am struggling with is reading a txt file from the SD card (my RFID database file) and putting each element into an array.
I have the txt file structured in the following way.
firstName lastName,IDnumber,
firstName lastName,IDnumber,
When I store the ID numbers in the program everything works fine and I can output to a txt file no problems.
What I am trying to do is read the rfid card,
load the database into memory,
check the ID against the database and store the name to a variable,
check the names of people already in attendance,
if the name is not in the attendance file add the name to the file,
output to lcd,
The code below compiles, but I do not receive the initial "ready to scan!" on the LCD and it does nothing.
If I comment out the information in the LoadRDIFdb function it will initialise with "ready to scan"
If anyone has any suggestions that would be great.
Thanks.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SdFat.h>
SdFat sd;
SdFile myFile;
using namespace std;
struct RFID
{
char name[50];
char ID[10];
};
const int SIZE = 25; //Lines in the txt file
RFID DB[SIZE]; //db with names and rfids
RFID CHECK[SIZE]; //class list names
// set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27,16,2);
// On the Ethernet Shield, CS is pin 4. SdFat handles setting SS
const int chipSelect = 4;
//Set the RFID Reader Reset switch
int RFIDResetPin = 2;
void setup()
{
//initialize the serial port
Serial.begin(9600);
//Enable SPI Interface
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
//lcd initialize
lcd.init();
lcd.backlight();
lcd.setCursor(1,0);
lcd.print("Ready To Scan!"); //System Ready to scan
pinMode(RFIDResetPin, OUTPUT);
digitalWrite(RFIDResetPin, HIGH);
// Initialize SdFat or print a detailed error message and halt
// Use half speed like the native library.
// change to SPI_FULL_SPEED for more performance.
if (!sd.init(SPI_HALF_SPEED, chipSelect)) sd.initErrorHalt();
}
void loop()
{
char tagString[13];
int index = 0;
boolean reading = false;
//When Card presented to RFID reader
if (Serial.available()){
{
delay(100);
// Clear Screen
lcd.clear();
//Read ID
while (Serial.available() > 0){
//Read ID number Byte by Byte
int readByte = Serial.read();
if(readByte == 2) reading = true; //begingin of tag
if(readByte == 3) reading = false; //end of tag
if(reading && readByte !=2 && readByte != 10 && readByte != 13){
//store the tag byte by byte
tagString[index] = readByte;
index++;
}
}
//Load RFIF database into memory, check tag name
//and output data to file and LCD
loadRFIDdb(tagString);
}
delay(1800);// display infor on screen for 1.8s
clearTag(tagString); //Clear the char
resetReader(); //Reset RFID reader
lcd.clear(); // Clear Screen
lcd.setCursor(1,0);
lcd.print("Ready To Scan!"); //System Ready to scan
}
}
void loadRFIDdb(char tag[]){
// if there is no string exit
if(strlen(tag) == 0) return;
//read RFID DB to memory
ifstream sdin("RFIDDB.TXT");
int i=0;
do
{
sdin.getline(DB[i].ID,50,',');
sdin.getline(DB[i].name,50,',');
i++;
}while (i<SIZE && !(sdin.fail()));
sdin.close();
//check ID in DB and pull name
boolean IdExists = false;
char temp_name[50];
for(int x=0; x<i; x++)
{
if(strcmp(DB[x].ID, tag)!=0)
{
strcpy(DB[x].name, temp_name);
IdExists = true;
}
}
//if ID was not found in the database
if (IdExists == false){
//Out put to LCD, Card ID Not Found
lcd.print("ID was");
lcd.setCursor(0,1);
lcd.print("not found");
return;
}
//Check Name against list allready marked in attendance
int duplicate = readclass(temp_name);
//if its a new student, add them to the file
if(duplicate == 0)
{
lcd.print(temp_name);
lcd.setCursor(0,1);
lcd.print("Is Present");
// open the file for write at end like the Native SD library
if (!myFile.open("CLASS.TXT", O_WRITE | O_CREAT | O_APPEND)) {
sd.errorHalt("opening test.txt for write failed");
}
// if the file opened okay, write to it:
myFile.println(temp_name);
// close the file:
myFile.close();
return;
}
if(duplicate == 1)
{
//output to LCD student is already present
lcd.print("Student already");
lcd.setCursor(0,1);
lcd.print("marked present");
return;
}
}
int readclass(char Name[]){
//read existing CLASS
ifstream sdin("CLASS.TXT");
int i=0;
do
{
sdin.getline(CHECK[i].name,50,'\n');
i++;
}while (i<SIZE && !(sdin.fail()));
sdin.close();
//compare name to class list (see if its there)
for(int x=0; x < i; x++)
{
if(strcmp(CHECK[x].name, Name)!=0)
{
return 1; //duplicate found
}
}
return 0;
}
void clearTag(char one[]){
//clear the array by filling with null - ASCII 0
for(int i = 0; i < strlen(one); i++){
one[i] = 0;
}
}
void resetReader(){
//Reset RFID reader to read again.
digitalWrite(RFIDResetPin, LOW);
digitalWrite(RFIDResetPin, HIGH);
delay(150);
}