Password not working

My project is smart locker system.The components used are RFID reader, Keypad,LCD with I2C, Arduino Mega and relay.

What my project should do is ,initially we provide RFID tag to the system.If the rfid tag is currect the system will ask for password.If the password is matched the locker will OPEN.If someone tries to break the locker the vibration sensor fixed on the locker should find the anomally and trigger the siren which is a buzzer in this case.If a predefined security code is entered the siren should stop.

But the problem here i am having is everything works fine except last part.When the vibration sensor detects the vibration buzzer gets activated but while entering the security code the siren is not stopping.I done many troubleshooting methods including debugging using serial monitor.But the problem is not solving.All modules are working properly.

I think the problem is in the cod, and i have limited knowledge in programming .I would be graceful if someone intent to help me.

// 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()
{
  int val = 0;
  val = digitalRead(vib_pin); //read vib pin
  if (val == 1)
  {
    
    vib_sensor();
    
    alarmIsActive = true; //global boolean assighned made true if vib pin detect input
    if (alarmIsActive = true) // only run if vib pin detect input
    {

      keypass();
      
    }
    else    // If password is not matched
    {

      warning();

    }
  }
}








void vib_sensor() //alarm triggering function
{

  digitalWrite(buzzerPin, HIGH); // make vib pin high
  lcd.clear();
  lcd.print("U R undr theft");
}







void warning() //warning message function
{
  lcd.clear();
  lcd.print("WARNING");
  alarmIsActive = false;
}






void keypass() //password for vibration sensor after triggered function
{
  char alarm_password[3] = {'8', '8', '8'};  // Variable to store initial password
  char pass[3];   // Variable to store alarm password
  char key_pressed_alarm = 0;
  uint8_t j = 0;  // Variable used for counter

  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;

    }
  }
}

Hi,

Just to try to help out, have you placed some Serial print in the digitalWrite(buzzerPin, LOW); to check this code is being executed?

Kind Regards

That part is not working including keypad function....I dont know why and what to do...I changed many times the code but the problem is not solving :frowning_face:

Hi
This line is misspelled.

if (alarmIsActive = true)

Correct to:

if (alarmIsActive == true)

RV mineirin

I checked that just now but problem is still the same.

Hi @dudeme
the "security()" routine is only called once and thus only reads the keyboard once.
The cause of this is that the variable " val " is being reset (int val = 0;) when going back into the routine to type the second digit and then no longer enters the "if (val == 1)".

so i should not put val=0....right...and what to do to check security() in background countinuously.

Hi
Try this way:
Create variable Val as global and zero.
When activating the vibrator set to 1 e and only reset it after confirming the correct password.

RV mineirin

@sreeharipa
@dudeme

Are you the same person with 2 accounts ?