Please help for RFİD reader

When ı start arduino my code and rfid reader running very well. but after few card reading rfid is not working any more. when ı restart arduino it works until i reading few card
here is my code

#include <MFRC522.h>
#include <Keypad.h>
#include <SPI.h>


MFRC522 mfrc522(10, 9); // MFRC522 mfrc522(SS_PIN, RST_PIN)

constexpr uint8_t greenLed = 7;
constexpr uint8_t redLed = 6;
constexpr uint8_t buzzer = 8;
int kilit=5;
char initial_password[4] = {'1', '2', '3', '4'};  
String tagUID = "BE F7 20 71";  
char password[4];   
boolean RFIDMode = true; // 
char key_pressed = 0; // 
uint8_t i = 0;  
const byte rows = 4;
const byte columns = 3;
// Keypad pin map
char hexaKeys[rows][columns] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};
// Initializing pins for keypad
byte row_pins[rows] = {A0, A1, A2, A3};
byte column_pins[columns] = {4, 3, 2};
// Create instance for keypad
Keypad keypad_key = Keypad( makeKeymap(hexaKeys), row_pins, column_pins, rows, columns);
void setup() {
  // Arduino Pin configuration
  pinMode(buzzer, OUTPUT);
  pinMode(kilit,OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(redLed, OUTPUT);
  digitalWrite(kilit,HIGH);
  SPI.begin();      // Init SPI bus
  mfrc522.PCD_Init();   // Init MFRC522
  
}
void loop() {
  
  // System will first look for mode
  if (RFIDMode == true) {
    
    // 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.
      digitalWrite(buzzer,HIGH);
      delay(80);
      digitalWrite(buzzer,LOW);
      delay(80);
      digitalWrite(buzzer,HIGH);
      delay(80);
      digitalWrite(buzzer,LOW);
      delay(80);
      digitalWrite(buzzer,HIGH);
      delay(80);
      digitalWrite(buzzer,LOW);
      delay(80);
      RFIDMode = false; // Make RFID mode false
    }
    else
    {
      // If UID of tag is not matched.
       digitalWrite(redLed,HIGH);
      digitalWrite(buzzer, HIGH);
      delay(1000);
      digitalWrite(buzzer, LOW);
      digitalWrite(redLed,LOW);
      delay(50);
      
    }
  }
  // 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
      digitalWrite(buzzer,HIGH);
      delay(80);
      digitalWrite(buzzer,LOW);
      delay(80);
    }
    if (i == 4) // If 4 keys are completed
    {
      delay(200);
      if (!(strncmp(password, initial_password, 4))) // If password is matched
      {
      digitalWrite(buzzer,HIGH);
      delay(80);
      digitalWrite(buzzer,LOW);
      delay(80);
      digitalWrite(buzzer,HIGH);
      delay(80);
      digitalWrite(buzzer,LOW);
      delay(80);
       digitalWrite(greenLed,HIGH);
      digitalWrite(kilit,LOW);
      delay(2000);
       digitalWrite(greenLed,LOW);
      digitalWrite(kilit,HIGH);
     delay(2000);      

        i = 0;
        RFIDMode = true; // Make RFID mode true
      }
      else    // If password is not matched
      {
       
        digitalWrite(buzzer, HIGH);
        digitalWrite(redLed,HIGH);
        delay(1000);
        digitalWrite(buzzer, LOW);
        digitalWrite(redLed,LOW);

        
        i = 0;
        RFIDMode = true;  // Make RFID mode true
      }
    }
  }
}

Why not use serial.Print() to explore the logic of your program and see what occurred just before the failure?

What type of Arduino are you using? Your use of the String class, and specifically the concat function, can fragment your heap memory. If you're using something small like an UNO then that can crash your program after a short time.

You would be better served to collect those bytes into an array.

ı tried 3 different methods for coding. and it crashes even if ı print on serial without using strings

Right now you're the only one who can see the code that doesn't use String. Would you like help with it?

I only see one code here and it has that known issue I already pointed out. I can't speak to the code you're not sharing.

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