In my project i have Arduino, RFID , Keypad, LCD with i2c ,Relay and vibration sensor.Which is a Smart locker.
If i use RFID tag and currect password the relay should open.And if vibration sensor detects any in put the buzzer should sound continuously.I used the code of vibration sensor as Security() user defined function.If vibration sensor detects input buzzer should sound infinitely and if a security password " 8 8 8 " is given the buzzer should stop.But when i tried to merge this in my original code it is not working properly.
At fist it asks to enroll RFID card it will bypass it then the system ask for password if i enter the system password it is getting mixed with the system password and security password.And showing WARNING message that i placed inside the SECURITY().
// Include required libraries
#include <MFRC522.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <SPI.h>
// Create instances
LiquidCrystal_I2C lcd(0x27, 16, 2);
MFRC522 mfrc522(53, 2); // MFRC522 mfrc522(SS_PIN, RST_PIN)
// Initialize Pins for led's, servo and buzzer
// Blue LED is connected to 5V
constexpr uint8_t greenLed = 33;
constexpr uint8_t redLed = 35;
constexpr uint8_t relay = 13;
constexpr uint8_t buzzerPin = 12;
constexpr uint8_t vib_pin = 10;
char initial_password[4] = {'1', '2', '3', '4'}; // Variable to store initial password
String tagUID = "C9 60 21 B3"; // String to store UID of tag. Change it with your tag's UID
char password[4]; // Variable to store users password
boolean RFIDMode = true; // boolean to change modes
char key_pressed = 0; // Variable to store incoming keys
uint8_t i = 0; // Variable used for counter
// defining how many rows and columns our keypad have
const byte rows = 4;
const byte columns = 4;
// Keypad pin map
char hexaKeys[rows][columns] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
// Initializing pins for keypad
byte row_pins[rows] = {9, 8, 7, 6};
byte column_pins[columns] = {5, 4, 3};
// Create instance for keypad
Keypad keypad_key = Keypad( makeKeymap(hexaKeys), row_pins, column_pins, rows, columns);
boolean alarmIsActive = false;
void setup() {
// Arduino Pin configuration
pinMode(buzzerPin, OUTPUT);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(relay, OUTPUT);
pinMode(vib_pin, INPUT);
lcd.init(); // LCD screen
lcd.backlight();
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522
lcd.clear(); // Clear LCD screen
Serial.begin(115200);
}
void loop() {
security();
// System will first look for mode
if (RFIDMode == true) {
lcd.setCursor(0, 0);
lcd.print(" Persnl locker ");
lcd.setCursor(0, 1);
lcd.print(" Scan Your Tag ");
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
//Reading from the card
String tag = "";
for (byte j = 0; j < mfrc522.uid.size; j++)
{
tag.concat(String(mfrc522.uid.uidByte[j] < 0x10 ? " 0" : " "));
tag.concat(String(mfrc522.uid.uidByte[j], HEX));
}
tag.toUpperCase();
//Checking the card
if (tag.substring(1) == tagUID)
{
// If UID of tag is matched.
lcd.clear();
lcd.print("Tag Matched");
digitalWrite(greenLed, HIGH);
delay(3000);
digitalWrite(greenLed, LOW);
lcd.clear();
lcd.print("Enter Password:");
lcd.setCursor(0, 1);
RFIDMode = false; // Make RFID mode false
}
else
{
// If UID of tag is not matched.
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Wrong Tag Shown");
lcd.setCursor(0, 1);
lcd.print("Access Denied");
digitalWrite(buzzerPin, HIGH);
digitalWrite(redLed, HIGH);
delay(3000);
digitalWrite(buzzerPin, LOW);
digitalWrite(redLed, LOW);
lcd.clear();
}
}
// If RFID mode is false, it will look for keys from keypad
if (RFIDMode == false) {
key_pressed = keypad_key.getKey(); // Storing keys
if (key_pressed)
{
password[i++] = key_pressed; // Storing in password variable
lcd.print("*");
}
if (i == 4) // If 4 keys are completed
{
delay(200);
if (!(strncmp(password, initial_password, 4))) // If password is matched
{
lcd.clear();
lcd.print("Pass Accepted");
digitalWrite(relay, HIGH);
digitalWrite(greenLed, HIGH);
delay(5000);
digitalWrite(greenLed, LOW);
digitalWrite(relay, LOW);
lcd.clear();
i = 0;
RFIDMode = true; // Make RFID mode true
}
else // If password is not matched
{
lcd.clear();
lcd.print("Wrong Password");
digitalWrite(buzzerPin, HIGH);
digitalWrite(redLed, HIGH);
delay(3000);
digitalWrite(buzzerPin, LOW);
digitalWrite(redLed, LOW);
lcd.clear();
i = 0;
RFIDMode = true; // Make RFID mode true
}
}
}
}
void security()
{
char alarm_password[3] = {'8', '8', '8'}; // Variable to store initial password
char pass[3]; // Variable to store alarm password
int val = 0;
uint8_t j = 0; // Variable used for counter
char key_pressed_alarm = 0;
val = digitalRead(vib_pin); //read vib pin
if (val == 1)
{
digitalWrite(buzzerPin, HIGH); // make vib pin high
lcd.clear();
lcd.print("U R undr theft");
alarmIsActive = true; //global boolean assighned made true if vib pin detect input
}
if (alarmIsActive = true) // only run if vib pin detect input
{
start:
key_pressed_alarm = keypad_key.getKey(); // Storing keys that we input
if (key_pressed_alarm)
{
pass[j++] = key_pressed_alarm ; // Storing in password variable
lcd.print("*");
}
if (j == 3) // If 3 keys entered are completed
{
delay(200);
if (!(strncmp(pass, alarm_password, 3))) // If password is matched
{
lcd.clear();
lcd.print("Pass Accepted");
digitalWrite(buzzerPin, LOW);
j = 0;
alarmIsActive = false;
}
else // If password is not matched
{
lcd.clear();
lcd.print("WARNING");
j = 0;
alarmIsActive = false;
goto start;
}
}
}
}