Hi there,
I need some help on my sketch. I have compiled the code from various other codes. This project should read a rfid card and depending on its status ,activate the red light/green light and display a message on a 16x2lcd. All works fine,except for multiple read on the tags. I have inserted the " Serial.flush();" command but I stil get multiple reads with one swipe of the card.
Could anyone help me on this one please??
My aim with this project at the end is to 1.Add cards/tags to the system without any changes to the sketch.(something like a "master" card).2.To log the "swipe time" on a sd card and enable the user of the system to add a text file to the sd card so that the sketch can refer(read) "names"(people names) to a certain/unique tag/card number from the sd card.3.At each swipe logged the time/date should be logged as-well.
One step at a time ![]()
There are a lot of sketches out there that is similar but not exactly what I am looking for,that is the one reason,the other reason
is: it's easy to get schematics/code....... online and build the projects....but at the end you did not learn anything! So you will never be able to develop something by yourself =(
Thanks
Regards
Pieter
(RSA)
#include<LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int data1 = 0;
int ok=-1;
// define the tag numbers that can have access
int yellowtag[14] = { 2,48,56,48,48,51,49,55,51,53,68,49,55,3}; //
int redtag[14] = { 2,48,57,48,48,55,51,48,53,57,52,69,66,3}; //
int newtag[14] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // used for read comparisons
int okdelay = 500; // this is the time the output will be set high for when an acceptable tag has been read
int notokdelay = 500; // time to show no entry (red LED)
void setup()
{
Serial.flush(); // need to flush serial buffer, otherwise first read from reset/power on may not be correct
pinMode(6, OUTPUT); // Invalid card- LED
pinMode(7, OUTPUT); // Valid Card LED.
Serial.begin(9600); //
lcd.begin(16, 2);
lcd.print("Start up");//0
delay(500);
lcd.clear();
lcd.print("Start up.");//1
delay(500);
lcd.clear();
lcd.print("Start up..");//2
delay(500);
lcd.clear();
lcd.print("Start up...");//3
delay(500);
lcd.clear();
lcd.print("Start up....");//4
delay(500);
lcd.clear();
lcd.print("Start up.....");//5
delay(500);
lcd.clear();
lcd.print("Start up......");//6
delay(500);
lcd.clear();
lcd.print("Start up.......");//7
delay(500);
lcd.clear();
lcd.print("Start up........");//8
delay(500);
lcd.setCursor(0, 1);
lcd.print(".");
delay(500);
lcd.print(".");
delay(500);
lcd.print(".");
delay(500);
lcd.print(".");
delay(500);
lcd.print(".");
delay(500);
lcd.print(".");
delay(500);
lcd.print(".");
delay(500);
lcd.print(".");
delay(500);
lcd.print(".");
delay(500);
lcd.print(".");
delay(500);
lcd.print(".");
delay(500);
lcd.print(".");
delay(500);
lcd.print(".");
delay(500);
lcd.print(".");
delay(500);
lcd.print(".");
delay(500);
lcd.print(".");
delay(500);
lcd.clear();
}
boolean comparetag(int aa[14], int bb[14])
// compares two arrrays, returns true if identical - good for comparing tags
{
boolean ff=false;
int fg=0;
for (int cc=0; cc<14; cc++)
{
if (aa[cc]==bb[cc])
{
fg++;
}
}
if (fg==14)
{
ff=true;
}
return ff;
}
void checkmytags()
//compares each tag against the tag just read
{
ok=0; // this variable helps decision making, if it is 1, we have a match, zero - a read but no match, -1, no read attempt made
if (comparetag(newtag,yellowtag)==true)
{
ok++;
}
if (comparetag(newtag,redtag)==true)
{
ok++;
}
}
void readTag()
// poll serial port to see if tag data is coming in (i.e. a read attempt)
{
ok=-1;
if (Serial.available() > 0) // if a read has been attempted
{
// read the incoming number on serial RX
delay(100); // Needed to allow time for the data to come in from the serial buffer.
for (int z=0; z<14; z++) // read the rest of the tag
{
data1=Serial.read();
newtag[z]=data1;
}
Serial.flush(); // stops multiple reads
// now to match tags up
checkmytags(); // compare the number of the tag just read against my own tags' number
}
//now do something based on tag type
if (ok>0==true) // if we had a match
{
digitalWrite(7, HIGH);
delay(okdelay);
digitalWrite(7, LOW);
ok=-1;
lcd.clear(); //clear Lcd before data write
lcd.setCursor(1, 0);// set cursor to top row of lcd
lcd.print(" Welcome!!");// Valid card message
delay(2000);// 2 seconds delay
lcd.clear();// clear lcd for next action
}
else if (ok==0) // if we didn't have a match
{
digitalWrite(6, HIGH);
delay(notokdelay);
digitalWrite(6, LOW);
ok=-1;
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Invalid Card!!");
delay(2000);
lcd.clear();
}
}
void loop()
{
readTag(); // we should create a function to take care of reading tags, as later on
// we will want other things to happen while waiting for a tag read, such as
// displaying data on an LCD, etc
lcd.setCursor(1,0);
lcd.print(" Swipe card!!");
delay(500);
lcd.clear();
delay(500);
}