-
-
Save Mohammadgeek/1e7b780f8b3f4be6c39521d80e2de0c5 to your computer and use it in GitHub Desktop.
RFID checker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SPI.h> | |
#include <MFRC522.h> | |
#include <SPI.h> | |
#include <Wire.h> | |
#include <Adafruit_GFX.h> | |
#include <Adafruit_SSD1306.h> | |
#define TRIG_PIN 6 | |
#define ECHO_PIN 7 | |
#define OLED_RESET 0 // GPIO0 | |
Adafruit_SSD1306 display(OLED_RESET); | |
#define NUMFLAKES 10 | |
#define XPOS 0 | |
#define YPOS 1 | |
#define DELTAY 2 | |
#define LOGO16_GLCD_HEIGHT 16 | |
#define LOGO16_GLCD_WIDTH 16 | |
#define SS_PIN 10 | |
#define RST_PIN 9 | |
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance. | |
void setup() | |
{ | |
pinMode(TRIG_PIN,OUTPUT); | |
pinMode(ECHO_PIN,INPUT); | |
Serial.begin(9600); // Initiate a serial communication | |
SPI.begin(); // Initiate SPI bus | |
mfrc522.PCD_Init(); // Initiate MFRC522 | |
Serial.println("Approximate your card to the reader..."); | |
Serial.println(); | |
} | |
void distance_checker(){ | |
long duration, distanceCm, distanceIn; | |
digitalWrite(TRIG_PIN, LOW); | |
delayMicroseconds(2); | |
digitalWrite(TRIG_PIN, HIGH); | |
delayMicroseconds(10); | |
digitalWrite(TRIG_PIN, LOW); | |
duration = pulseIn(ECHO_PIN,HIGH); | |
// convert the time into a distance | |
distanceCm = duration / 29.1 / 2 ; | |
distanceIn = duration / 74 / 2; | |
Serial.print(distanceIn); | |
Serial.print("in, "); | |
Serial.print(distanceCm); | |
Serial.print("cm"); | |
Serial.println(); | |
delay(250); | |
if(distanceCm < 20 && distanceCm > 10){ | |
Serial.print("object is reached location!"); | |
Serial.println("\n"); | |
Serial.println(distanceCm); | |
display.clearDisplay(); | |
display.setTextSize(1); // Draw 2X-scale text | |
display.setTextColor(WHITE); | |
display.setCursor(33, 10); | |
display.print("USER NEAR!"); | |
}else if (distanceCm < 10){ | |
Serial.print("Reached user!... RFID Enable mode"); | |
Serial.println("\n"); | |
Serial.print(distanceCm); | |
display.clearDisplay(); | |
display.setTextSize(1); // Draw 2X-scale text | |
display.setTextColor(WHITE); | |
display.setCursor(33, 10); | |
display.print("RFID LOCK!"); | |
}else{ | |
Serial.print("OUTSIDE USER"); | |
Serial.println("\n"); | |
Serial.print(distanceCm); | |
display.clearDisplay(); | |
display.setTextSize(1); // Draw 2X-scale text | |
display.setTextColor(WHITE); | |
display.setCursor(33, 10); | |
display.print("OUTSIDE"); | |
} | |
} | |
void loop() | |
{ | |
display.display(); | |
delay(2000); | |
distance_checker(); | |
// Look for new cards | |
if ( ! mfrc522.PICC_IsNewCardPresent()) | |
{ | |
return; | |
} | |
// Select one of the cards | |
if ( ! mfrc522.PICC_ReadCardSerial()) | |
{ | |
return; | |
} | |
//Show UID on serial monitor | |
Serial.print("\r UID tag :"); | |
String content= ""; | |
byte letter; | |
for (byte i = 0; i < mfrc522.uid.size; i++) | |
{ | |
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "); | |
Serial.print(mfrc522.uid.uidByte[i], HEX); | |
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ")); | |
content.concat(String(mfrc522.uid.uidByte[i], HEX)); | |
} | |
Serial.println(); | |
Serial.print("Message : "); | |
content.toUpperCase(); | |
if (content.substring(1) == "0B EE CD 54") //change here the UID of the card/cards that you want to give access | |
{ | |
Serial.println("Authorized access"); | |
display.clearDisplay(); | |
display.setTextSize(1); // Draw 2X-scale text | |
display.setTextColor(WHITE); | |
display.setCursor(33, 10); | |
display.print("RFID OPEN!"); | |
Serial.println(); | |
delay(3000); | |
} | |
else { | |
Serial.println(" Access denied"); | |
display.clearDisplay(); | |
display.setTextSize(1); // Draw 2X-scale text | |
display.setTextColor(WHITE); | |
display.setCursor(33, 10); | |
display.print("Reject!"); | |
delay(3000); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment