Below is the libraries needed to compile the code for the RFID Lock System
Below is the code needed for the RFID Lock System
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library for ST7735
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN -1 // Configurable, see typical pin layout above
#define SS_PIN 5 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
#define TFT_CS 13
#define TFT_RST -1
#define TFT_DC 12
#define TFT_MOSI 14 // Data out
#define TFT_SCLK 27 // Clock out
#define RL1 21
// For ST7735-based displays, we will use this call
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
float p = 3.1415926;
String tagContent = "";
String Master;
int touchValue=0;
void setup(void) {
pinMode(RL1, OUTPUT);
Serial.begin(9600);
SPI.begin(); // Init SPI bus
while (!Serial)
; // Do nothing if no serial port is opened
delay(1000);
mfrc522.PCD_Init(); // Init MFRC522 board.
Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));
Serial.print(F("Hello! ST77xx TFT Test\\n"));
delay(100);
tft.initR(INITR_BLACKTAB);// 1.8TFTscreen are use
// tft.initR(INITR_GREENTAB);// 1.44TFTscreen are use
tft.fillScreen(ST77XX_BLACK);
delay(100);
Serial.println(F("Initialized"));
tft.setRotation(2);
// large block of text
tft.fillScreen(ST77XX_BLACK);
tft.setTextSize(2);
testdrawtext(" Botshop testing code", ST77XX_WHITE);
delay(2000);
tft.fillScreen(ST77XX_BLACK);
int touchValue = touchRead(33); //Test for Button Press
delay(10);
if(touchValue<(32))
{
tft.fillScreen(ST77XX_YELLOW);
tft.setTextSize(2);
tft.setCursor(10,40);
testdrawtext(" THIS is YOUR Bootup menu",ST77XX_BLUE);
tft.setTextWrap(true);
tft.setTextSize(1);
tft.setCursor(10,80);
tft.setTextColor(ST77XX_BLUE);
tft.print("You PRESSED T8 \\n");
tft.print(" Button\\n");
tft.print(" on BOOTup ");
while(touchValue<=(20))
{
int touchValue = touchRead(33);
delay(10);
Serial.println(touchValue);
delay(20);
}
delay(2000);
tft.fillScreen(ST77XX_BLACK);
}
Serial.println("done");
delay(1000);
}
void loop() {
tft.setTextSize(2);
tft.setCursor(15,5);
testdrawtext(" Waitting for TAG", ST77XX_WHITE);
delay(1000); //change value if you want to read cards faster
if (!mfrc522.PICC_IsNewCardPresent()) {
return;
}
if (!mfrc522.PICC_ReadCardSerial()) {
return;
}
tagContent = ""; //Clear for new value to be read
for (byte i = 0; i < mfrc522.uid.size; i++) {
tagContent.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " " : " "));
tagContent.concat(String(mfrc522.uid.uidByte[i], HEX));
}
tagContent.toUpperCase(); //Make value to Uppercase
if (tagContent == Master)
{
tft.fillScreen(ST77XX_YELLOW);
tft.setTextSize(3);
tft.setCursor(10,40);
tft.setTextColor(ST77XX_BLUE);
tft.setTextWrap(true);
tft.print("MASTER");
delay(2000);
}
if (tagContent==(" 7C F4 D4 AB")) //change here the UID of the card/cards that you want to give access== "7C F4 D4 AB"
{
Serial.println("Authorized access"); //serialport monitor card access
Serial.println("With TAG NR:"+String(tagContent)); //serialport monitor card reader ID
tft.fillScreen(ST77XX_GREEN);
delay(1);
tft.fillScreen(ST77XX_BLACK);
tft.setTextSize(1);
tft.setCursor(10,40);
tft.setTextColor(ST77XX_GREEN);
tft.setTextWrap(true);
tft.print("Authorized access");
digitalWrite(RL1,HIGH);
delay(2000);
digitalWrite(RL1, LOW);
}
else {
Serial.println(" Access denied"); //serialport monitor card access not Authorized
Serial.println("With TAG NR:"+String(tagContent)); //serialport monitor card reader ID
tft.fillScreen(ST77XX_RED);
delay(1);
tft.fillScreen(ST77XX_BLACK);
tft.setTextSize(1);
tft.setCursor(20,40);
tft.setTextColor(ST77XX_RED);
tft.setTextWrap(true);
tft.print("Access denied");
delay(2000);
}
int touchValue = touchRead(33);
delay(10);
if(touchValue<=(32))
{
Serial.println(touchValue);
delay(20);
tft.fillScreen(ST77XX_BLACK);
tft.setTextSize(1);
tft.setCursor(10,40);
tft.setTextColor(ST77XX_YELLOW);
tft.setTextWrap(true);
tft.print("Added Master card");
Master=tagContent;
Serial.println(Master);
delay(2000);
}
}
void testdrawtext(char *text, uint16_t color) {
tft.setCursor(0, 0);
tft.setTextColor(color);
tft.setTextWrap(true);
tft.print(text);
}