RFID freezes and I have to restart the serial monitor or the arduino board

When I scan a card and press the button to activate the servos the RFID stops working and I have to restart the serial monitor or the board. I need to finish this today! This is the code:

#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
#define RST_PIN         5
#define SS_PIN          53
MFRC522 mfrc522(SS_PIN, RST_PIN);
Servo Barrier1;
Servo Barrier2;
const int red = 48, green = 49, buzzer = 46,trigPin=33,echoPin=32,button=47,pot=2;
long duration; 
int distance,eu_passports=0,other_passports=0,button_state=0,persons,scans=0,persons_needed=0;
String uid1 = "53 AF F6 38";
String uid2 = "FA 96 D4 BF";
void setup() {
Barrier1.attach(31);
Barrier2.attach(30);
pinMode(button, INPUT);
pinMode(pot, INPUT);
pinMode(trigPin, OUTPUT); 
pinMode(echoPin, INPUT); 
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(buzzer,OUTPUT);
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
delay(50);
}

void loop() {
 

  button_state = digitalRead(button);
  persons = analogRead(pot);
  persons = map(persons,0,1023,0,11);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034 / 2;
  
  if (distance >= 1 and distance <=20)
  {
      if (button_state==HIGH and scans>=1)
      {
        tone(buzzer,400);
        delay(2000);
        noTone(buzzer);
        if (eu_passports>=1 and other_passports<1 and persons==persons_needed)
        {
          Barrier2.write(0);
          delay(3000);
          Barrier2.write(90);
          other_passports = 0;
          eu_passports = 0;
          scans = 0;
          persons_needed=0;
          Serial.print("1");
        }
        else 
        {
          Barrier1.write(0);
          delay(3000);
          Barrier1.write(90);
          other_passports = 0;
          eu_passports = 0;
          scans = 0;
          persons_needed=0;
          Serial.print("2");
        }
      }
    digitalWrite(red, HIGH);
    digitalWrite(green, LOW);
  }
  else
  {
    digitalWrite(red, LOW);
    digitalWrite(green, HIGH);
  }
  
  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    return;
  }


  if ( ! mfrc522.PICC_ReadCardSerial()) {
    return;
  }

  String content = "";
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++)
  {
    Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
    Serial.print(mfrc522.uid.uidByte[i], HEX);
    content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
    content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  content.toUpperCase();
  if (content.substring(1) == uid1) 
  {
    
    Serial.println("European passport!");
    persons_needed=persons_needed+1;
    scans=scans+1;
    eu_passports=eu_passports+1;
    tone(buzzer,1000);
    delay(500);
    noTone(buzzer);
  }
  else if (content.substring(1) == uid2)
  {
    scans=scans+1;
    other_passports=other_passports+1;
    persons_needed=persons_needed+1;
    Serial.println("Other passport!");
    tone(buzzer,500);
    delay(500);
    noTone(buzzer);
  }
  delay(2000);
}

Thanks for any help!

Reopening the serial port will usually cause a restart - so that’s no a surprise…
Check your arrays aren’t being overwritten… remember to leave space for the trailing NULL, and other array specific precautions.

I can`t find the problem but I know that if I remove the else from line 62 it works. But the thing is that I need that else.

I really don't like seeing Strings and delay()s anywhere in code that's supposed to be reliable on small format microcontrollers.

Otherwise - try some Serial.print() - with debugging text, and you might get lucky finding the bugs.

Show what you are using for power and a photo and drawing of the layout.

Hi,

A circuit diagram would not go astray here either?
Thanks for using code tags. :+1:

Tom... :smiley: :+1: :coffee: :australia: