Arduino + LCD w/Shift + Parallax RFID
April 25th, 2008 by TheKidd
Well, the new LCD4×40Bit-ShiftReg library is working quite well. I’m actually thinking about changing the actual name of the library but figure the above is too long. Maybe I should try something like LCD3Wire4×40 or something. Your thought are greatly appreciated!
So anyway, I added my Parallax RFID card to the setup and finally worked out the bug I was initially having with the code. For some reason the RFID code would freeze and not continue looping the loop(). I found out finally after trying everything like an idiot that I had the wrong setup for the initialization of attachInterrupt(). Apparently I had the mode set wrong to CHANGE and I should have had it set to LOW. Makes since now that I think about it. CHANGE would make it continue to go into the interrupt function over and over again. As soon as I changed the mode to LOW everything came together quickly.
So here are some pictures of the RFID + Arduino + Shift Register + LCD setup.
Here is the ‘authorized card’ being read.
Response for authorized tag on LCD screen.
Reading an unauthorized card.
Response on LCD from unauthorized card.
Well there you have it. Now for the source code.
// Parallax-RFID-LCDShiftReg-0.0.2 RFID reader for Arduino with LCD output
// Wiring version by BARRAGAN <http://people.interaction-ivrea.it/h.barragan>
// Modified for Arudino by djmatic
// And pieces from Arduino Playground <http://www.arduino.cc/playground/Learning/PRFID>
// And then modified again by thekidd <http://www.tekcrack.com/category/rfid>
// 25.04.2008 - Added modified LCD4×40Bit-ShiftReg connection to display RFID results.
// Fixed attachInterrupt lockup problem. Was set to CHANGE mode. Now set to LOW mode.
// 12.04.2008 - Added interrupt to ensure program does not hang on RFID reader.
#include <avr/interrupt.h>
#include <SoftwareSerial.h>
#include <LCD4Bit.h>
#define rxPin 2
#define txPin 3
#define ledPin 13
// LCD Variables
int lcdLines = 4; // number of lines in your display
int doutPin = 11; // Dout pin
int strPin = 12; // Strobe pin
int clkPin = 10; // Clock pin
// RFID Variables
int val = 0;
int bytesread = 0;
char code[10];
int flag = 0;
int granted = 0;
// setup construct for lcd
LCD4Bit lcd = LCD4Bit(lcdLines, doutPin, strPin, clkPin);
// set up a soft serial port
SoftwareSerial mySerial(rxPin, txPin);
void setup() {
// define pin modes for tx, rx, led pins:
pinMode(rxPin, INPUT); // Set rxPin as INPUT to accept SOUT from RFID pin
pinMode(txPin, OUTPUT); // Set txPin as OUTPUT to connect it to the RFID /ENABLE pin
pinMode(ledPin, OUTPUT); // Let the user know whats up
// set the data rate for the serial ports
mySerial.begin(2400); // RFID reader SOUT pin connected to Serial RX pin at 2400bps
lcd.init(); // initialize lcd
lcd.cursorTo(1,0); // select first display controller, line 1 & 2
lcd.clear(); // clear lines 1 & 2
lcd.cursorTo(3,0); // select second display controller, line 3 & 4
lcd.clear(); // clear lines 3 & 4
delay(100);
defaultMsg();
attachInterrupt(0, readRFID, LOW); // Setup interrupt for SOUT on RFID reader
digitalWrite(txPin, LOW); // Activate the RFID reader
}
void loop() {
// Do Something
goRFID();
}
void goRFID() {
if (flag == 1) {
delay(1000);
lcd.cursorTo(1,0);
lcd.clear();
lcd.printIn("Welcome to DCA.");
lcd.cursorTo(2,0);
lcd.printIn("TAG code is: "); // possibly a good TAG
lcd.printIn(code); // print the TAG code
validateRFID(code); // Determine if right tag used. */
flag = 0;
delay(5000);
digitalWrite(txPin, LOW); // Activate the RFID reader
defaultMsg();
} else {
delay(1000);
digitalWrite(txPin, LOW); // Activate the RFID reader
defaultMsg();
}
}
void readRFID() {
digitalWrite(ledPin, LOW); // Turn off debug LED - Event triggered!
if((val = mySerial.read()) == 10) { // check for header
bytesread = 0;
while(bytesread<10) { // read 10 digit code
val = mySerial.read();
if((val == 10)||(val == 13)) { // if header or stop bytes before the 10 digit reading
break; // stop reading
}
code[bytesread] = val; // add the digit
bytesread++; // ready to read next digit
}
if(bytesread == 10) { // If 10 digit read is complete
digitalWrite(txPin, HIGH); // deactivate RFID reader
digitalWrite(ledPin, HIGH); // activate LED to show an RFID card was read
flag = 1;
}
bytesread = 0;
}
}
void validateRFID(char* i) {
if( strncmp(i,"0F0302A114", 10) == 0) {
// if 10 digit code equals "0F0302A114"
digitalWrite(ledPin, HIGH);
lcd.cursorTo(3,0);
lcd.clear();
lcd.printIn("Tag matches…Access Granted!");
lcd.cursorTo(4,0);
lcd.printIn("Firing squad avoided!");
digitalWrite(ledPin, LOW);
} else {
lcd.cursorTo(3,0);
lcd.clear();
lcd.printIn("Tag Failed…Access Denied!");
lcd.cursorTo(4,0);
lcd.printIn("Firing squad deployed!");
digitalWrite(ledPin, LOW);
}
}
void defaultMsg() {
// say something
lcd.cursorTo(1,0);
lcd.printIn("Welcome to DCA Corp.");
lcd.cursorTo(2,0);
lcd.printIn("Please swipe your access card.");
lcd.cursorTo(3,0);
lcd.printIn("* Unauthorized access is prohibitted *");
lcd.cursorTo(4,0);
lcd.printIn("** Disobedience meets firing squad! **");
}
You can also download the sketch file here.
Hope this is helpful.