The function not working as expected

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;

      }
    }
  }
}

The first thing that I would suggest is that you do not use the goto command. It can make understandi a program more difficult and it is entirely unnecessary to use it

Also, this is wrong

  if (alarmIsActive = true) // only run if vib pin detect input

= is for assigning of a value
== is for comparing a value

butwhen i use == the program was not working so i changed it to = then it worked properly.After that i tried to merge security() with locker code.
and i deleted the goto command.

Trust me. Using = is is wrong when doing a comparison. What your statement does is to set alarmIsActive to true every time it is run and the code in the brackets after it is run unconditionally

What code should run if alarmIsActive is true ?

I TRIED USING == but not working.When the vibration sensor is triggered the buzzer activates.But when the password is entered buzzer is not stopping.Buzzer plays continuously.

Hello
Do you know the system behaiviour by the ussage of the delay() function?

Start by getting rid of the goto

Put the code between start: and the goto in a function and call it when you need to run the code

yeah it dealays the function by provided time

I done as said but same problem occuring.......while i provide RFID tag the locker accepts and asks for keypad ....when i provide keypad password the relay is not working....the '*' is showing in the desplay while entering password but nothing happening after that......when i trigger buzzer it is playing continuously and password is not stopping the buzzer.

changed code :persevere:

// 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)

  {
    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
    alarming();

  }
}

void alarming()

{
  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

  if (alarmIsActive = true) // only run if vib pin detect input
  {


    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;



      }
    }
  }
}
  if (alarmIsActive = true) // only run if vib pin detect input

This is wrong for the reasons already noted

To debug your code first fix that then add Serial.print()s of pertinent variables at strategic places in the code so that you can track what is happening. To help with this, print text labels so that you know where you are printing from and which variable is being printed

Another observation

    if (tag.substring(1) == tagUID)

The substring being tested will start at the second character in the tag String. Is that what you intended to do ?

This is same project?

@sreeharipa
@dudeme

Are you the same person with 2 accounts ?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.