Devices & Components
Arduino Mega 2560 Rev3
16x2 LCD display with I²C interface
Breadboard - 400 contacts
RFID-RC522
9V 1A Switching Wall Power Supply
Tower Pro SG90 Servo Motor
Buzzer Module
MG90S 9G Servo Motor
Numerical keypad 3x4
Breadboard Power Supply Module (5 pcs)
Software & Tools
Arduino IDE
Project description
Code
SPE Safe code
cpp
code
1#include <SPI.h> 2#include <MFRC522.h> 3#include <Wire.h> 4#include <LiquidCrystal_I2C.h> 5#include <Keypad.h> 6#include <Servo.h> 7 8#define RST_PIN 5 9#define SS_PIN 53 10#define BUZZER_PIN 8 11#define SERVO_CARD_PIN 6 12#define SERVO_PIN_PIN 7 13 14MFRC522 mfrc522(SS_PIN, RST_PIN); 15LiquidCrystal_I2C lcd(0x27, 16, 2); 16Servo servoCard; 17Servo servoPIN; 18 19const String correctUID = "00:00:00:00"; 20const String correctPIN = "4567"; 21String enteredPIN = ""; 22 23bool cardUnlocked = false; 24bool pinUnlocked = false; 25 26const byte ROWS = 4; 27const byte COLS = 3; 28char keys[ROWS][COLS] = { 29 {'1','2','3'}, 30 {'4','5','6'}, 31 {'7','8','9'}, 32 {'*','0','#'} 33}; 34byte rowPins[ROWS] = {A0, A1, A2, A3}; 35byte colPins[COLS] = {A4, A5, A6}; 36 37Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); 38 39void beep() { 40 digitalWrite(BUZZER_PIN, HIGH); 41 delay(100); 42 digitalWrite(BUZZER_PIN, LOW); 43} 44 45void setup() { 46 Serial.begin(9600); 47 SPI.begin(); 48 mfrc522.PCD_Init(); 49 lcd.init(); 50 lcd.backlight(); 51 52 pinMode(BUZZER_PIN, OUTPUT); 53 54 servoCard.attach(SERVO_CARD_PIN); 55 servoPIN.attach(SERVO_PIN_PIN); 56 57 servoCard.write(0); 58 servoPIN.write(0); 59 60 lcd.setCursor(0, 0); 61 lcd.print("Scan your card"); 62} 63 64void loop() { 65 if (!cardUnlocked) { 66 if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) { 67 beep(); 68 69 String readUID = ""; 70 for (byte i = 0; i < mfrc522.uid.size; i++) { 71 readUID += String(mfrc522.uid.uidByte[i], HEX); 72 if (i < mfrc522.uid.size - 1) readUID += ":"; 73 } 74 readUID.toUpperCase(); 75 Serial.println("UID: " + readUID); 76 77 lcd.clear(); 78 if (readUID == correctUID) { 79 lcd.print("Card OK"); 80 servoCard.write(90); 81 cardUnlocked = true; 82 delay(1000); 83 lcd.clear(); 84 lcd.print("Enter PIN:"); 85 lcd.setCursor(0, 1); 86 } else { 87 lcd.print("Wrong card"); 88 delay(1000); 89 lcd.clear(); 90 lcd.print("Scan your card"); 91 } 92 93 mfrc522.PICC_HaltA(); 94 mfrc522.PCD_StopCrypto1(); 95 } 96 } else if (!pinUnlocked) { 97 char key = keypad.getKey(); 98 if (key) { 99 beep(); 100 if (key == '*') { 101 enteredPIN = ""; 102 lcd.setCursor(0, 1); 103 lcd.print(" "); 104 lcd.setCursor(0, 1); 105 } else if (key == '#') { 106 if (enteredPIN == correctPIN) { 107 lcd.clear(); 108 lcd.print("PIN Correct"); 109 servoPIN.write(90); 110 pinUnlocked = true; 111 delay(2000); 112 lcd.clear(); 113 lcd.print("Safe Opened"); 114 } else { 115 lcd.clear(); 116 lcd.print("Wrong PIN"); 117 servoCard.write(0); // relock 118 enteredPIN = ""; 119 cardUnlocked = false; 120 delay(2000); 121 lcd.clear(); 122 lcd.print("Scan your card"); 123 } 124 } else if (enteredPIN.length() < 4 && isDigit(key)) { 125 enteredPIN += key; 126 lcd.print("*"); 127 } 128 } 129 } else { 130 // Once open, rescan card to lock again 131 if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) { 132 beep(); 133 lcd.clear(); 134 lcd.print("Safe Locked"); 135 servoCard.write(0); 136 servoPIN.write(0); 137 enteredPIN = ""; 138 cardUnlocked = false; 139 pinUnlocked = false; 140 delay(2000); 141 lcd.clear(); 142 lcd.print("Scan your card"); 143 } 144 } 145}
Comments
Only logged in users can leave comments