Hi,
I am making a project with a keypad, RFID scanner, a servo and an LCD screen.
I have used the MFRC522 library and used the getID() code.
My code is attached below.
Any help appreciated.
Thanks.
PS The website I got the library from is here
#include <Arduino.h>
#include <stdint.h>
#include <Keypad.h>
#include <SPI.h>
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
#include <MFRC522.h>
#include <Wire.h>
#define RST_PIN 9
#define SS_PIN 10
byte readCard[4];
char* myTags[100] = {};
int tagsCount = 0;
String tagID = "";
boolean successRead = false;
boolean correctTag = false;
int proximitySensor;
boolean doorOpened = false;
const byte ROWS = 4;
const byte COLS = 4;
MFRC522 mfrc522(SS_PIN, RST_PIN);
Servo myServo;
LiquidCrystal_I2C lcd(0x21, 16, 2);
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {8, 7, 6, s5};
byte colPins[COLS] = {4, 3, 2, 1};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup() {
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
myServo.attach(0);
myServo.write(10);
lcd.backlight();
lcd.init();
lcd.setCursor(0, 0);
lcd.print("Initiating...");
delay(5000);
lcd.print("-No Master Tag!-");
lcd.setCursor(0, 1);
lcd.print(" SCAN NOW");
while (!successRead) {
successRead = getID(); //CHECK THIS [getID()]
if ( successRead == true) {
myTags[tagsCount] = strdup(tagID.c_str());
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Master Tag Set!");
tagsCount++;
}
}
successRead = false;
delay(5000);
lcd.setCursor(0, 0);
lcd.print(" Enter New");
lcd.setCursor(0, 1);
lcd.print(" Password:");
char customKey = customKeypad.getKey();
if (customKey) {
Data[data_count] = customKey;
lcd.setCursor(data_count, 1);
lcd.print(Data[data_count]);
data_count++;
}
if (data_count == Password_Length - 1) {
lcd.clear();
}
lcd.setCursor(0, 0);
lcd.print("---Initiation---");
lcd.setCursor(0, 1);
lcd.print("----Complete----");
delay(5000);
startUp();
uint8_t getID() {
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return 0;
}
if ( ! mfrc522.PICC_ReadCardSerial()) {
return 0;
}
tagID = "";
for ( uint8_t i = 0; i < 4; i++) {
readCard[i] = mfrc522.uid.uidByte[i];
tagID.concat(String(mfrc522.uid.uidByte[i], HEX));
}
tagID.toUpperCase();
mfrc522.PICC_HaltA();
return 1;
}
int proximitySensor = analogRead(A0);
}
void loop() {
if (!strcmp(Data, Master));
lcd.setCursor(0, 0);
lcd.print("Correct Keycode!");
myServo.write(180);
delay(5000);
startUp();
}
else {
lcd.clear
lcd.setCursor(0, 0);
lcd.print("-Incorrect Code-");
delay(2500);
startUp();
}
lcd.clear();
clearData();
}
if (tagID == myTags[0]) {
lcd.clear();
lcd.print("Program mode:");
lcd.setCursor(0, 1);
lcd.print("Add/Remove Tag");
while (!successRead) {
successRead = getID();
if ( successRead == true) {
for (int i = 0; i < 100; i++) {
if (tagID == myTags[i]) {
myTags[i] = "";
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Tag Removed!");
startUp();
return;
}
}
myTags[tagsCount] = strdup(tagID.c_str());
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Tag Added!");
startUp();
tagsCount++;
return;
}
}
}
for (int i = 0; i < 100; i++) {
if (tagID == myTags[i]) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Access Granted!");
myServo.write(180);
startUp();
correctTag = true;
}
}
if (correctTag == false) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Access Denied!");
startUp();
}
}
// If door is open...
else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Door Opened!");
while (!doorOpened) {
proximitySensor = analogRead(A0);
if (proximitySensor > 200) {
doorOpened = true;
}
}
doorOpened = false;
delay(500);
myServo.write(10);
startUp();
}
}
void startUp() {
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Password: ");
lcd.setCursor(0, 1);
lcd.print("--OR SCAN TAG!--");
myServo.write(10);
}
void clearData() {
while (data_count != 0) {
Data[data_count--] = 0;
}
return;
}