Hi Everyone,
I am trying to make an RFID lock for a cabinet but for the meantime, I would like to make it say "Authenticated" for authorized tags and copy the tag's serial number if unauthorized.
After googling how to do it, I stumbled upon this code:
#include <MFRC522.h>
//Get library from GitHub - ljos/MFRC522: Arduino RFID Library for MFRC522
//Sketch: gudjonholm@gmail.com
#include <SPI.h>
/*
Pins SPI UNO Mega2560 Leonardo
1 (NSS) SAD (SS) 10 53 10
2 SCK 13 52 SCK1
3 MOSI 11 51 MOSI1
4 MISO 12 50 MISO1
5 IRQ * * *
6 GND GND GND GND
7 RST 5 ? Reset
8 +3.3V (VCC) 3V3 3V3 3.3V
- Not needed
1 on ICPS header
*/
#define SAD 10
#define RST 5
MFRC522 nfc(SAD, RST);
#define ledPinOpen 2
#define ledPinClosed 3
void setup() {
pinMode(ledPinOpen , OUTPUT);
pinMode(ledPinClosed, OUTPUT);
SPI.begin();
Serial.begin(115200);
Serial.println("Looking for MFRC522.");
nfc.begin();
byte version = nfc.getFirmwareVersion();
if (! version) {
Serial.print("Didn't find MFRC522 board.");
while(1); //halt
}
Serial.print("Found chip MFRC522 ");
Serial.print("Firmware ver. 0x");
Serial.print(version, HEX);
Serial.println(".");
}
#define AUTHORIZED_COUNT 1 /If you want more Authorized of cards set the count here, and then add the serials below/
byte Authorized[AUTHORIZED_COUNT][6] = {
{0x45, 0x14, 0x39, 0x2E, 0xFF, 0xFF, }
/,{0x10, 0x14, 0x39, 0x2E, 0xFF, 0xFF, }/ /f. example how to add more authorized cards/
};
void printSerial(byte *serial);
boolean isSame(byte *key, byte *serial);
boolean isAuthorized(byte *serial);
void loop() {
byte status;
byte data[MAX_LEN];
byte serial[5];
boolean Opening = false;
digitalWrite(ledPinOpen, Opening);
digitalWrite(ledPinClosed, !Opening);
status = nfc.requestTag(MF1_REQIDL, data);
if (status == MI_OK) {
status = nfc.antiCollision(data);
memcpy(serial, data, 5);
if(isAuthorized(serial))
{
Serial.println("Authenticated");
Opening = true;
}
else
{
printSerial(serial);
Serial.println("is NOT authenticated");
Opening = false;
}
nfc.haltTag();
digitalWrite(ledPinOpen, Opening);
digitalWrite(ledPinClosed, !Opening);
delay(2000);
}//if (status == MI_OK)
delay(500);
}//void loop()
boolean isSame(byte key, byte serial)
{
for (int i = 0; i < 4; i++) {
if (key _!= serial)_
* {*
* return false;*
* }*
* }*
* return true;*
}
boolean isAuthorized(byte *serial)
{
* for(int i = 0; i<AUTHORIZED_COUNT; i++)
_ {_
_ if(isSame(serial, Authorized))
return true;
}
return false;
}
void printSerial(byte *serial)
{
Serial.print("Serial:");
for (int i = 0; i < 4; i++) {
Serial.print(serial, HEX);
Serial.print(" ");
}
}
What it does is it reads RFID tags and cards and if a tag/card is authorized, it would say
"Authenticated" and on the other hand, copy the unauthorized tag's serial and say "Serial:XX 31 XX 53 is NOT authenticated"
I would like to add a beep when a tag is authorized and 3 beeps if tag is not authorized.
I googled for a simple beep code and this is what I came up with:
/
Piezo
This example shows how to run a Piezo Buzzer on pin 9
using the analogWrite() function.
It beeps 3 times fast at startup, waits a second then beeps continuously
at a slower pace
/
void setup() {
// declare pin 9 to be an output:
pinMode(9, OUTPUT);
beep(25);
beep(50);
beep(100);
delay(1000);
}
void loop() {
beep(200);
}
void beep(unsigned char delayms){
analogWrite(9, 20); // Almost any value can be used except 0 and 255*
* // experiment to get the best tone*
* delay(delayms); // wait for a delayms ms*
* analogWrite(9, 0); // 0 turns it off*
* delay(delayms); // wait for a delayms ms
}*
Both codes work individually when uploaded but I have no idea how to merge them together.
Any help is appreciated. ![]()
I also read this post: Demonstration code for several things at the same time - Project Guidance - Arduino Forum but I could not understand it.._