Arduino freezes after some time

Hi guys, a made a project to open and close a door with relays when reading a rfid card with bits and pieces of some codes I found online, initially it works fine, but after some time it looks like the arduino freezes, can someone tell me why?

I read something online about using "string" in the code that might be causing that, but i don't understand why, and i'm quite new at programing :frowning:

here's the code:

/*
 * 
 * 
 */
 
#include <SPI.h>
#include <MFRC522.h>
 
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);
#define RELAY1  6
#define RELAY2  5
#define LED1  8

void setup() 
{
  // Initialise the Arduino data pins for OUTPUT

  pinMode(RELAY1, OUTPUT);
  pinMode(RELAY2, OUTPUT);
  pinMode(LED1, OUTPUT);

 
  Serial.begin(9600);   // Initiate a serial communication
  SPI.begin();      // Initiate  SPI bus
  mfrc522.PCD_Init();   // Initiate MFRC522
  Serial.println("Approximate your card to the reader...");
  Serial.println();

}
void loop() 
{
  
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) 
  {
    return;
  }
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) 
  {
    return;
  }
  //Show UID on serial monitor
  Serial.print("UID tag :");
  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));
  }
  Serial.println();
  Serial.print("Message : ");
  content.toUpperCase();

   (content.substring(1) == "XX XX XX XX" || content.substring(1) == "XX XX XX XX"); //change here the UID of the card/cards that you want to give access

    
    
    
    // read the led output pin:
digitalRead(LED1);

   
   
   
   if ((digitalRead(LED1) == LOW)){

  
  {
    Serial.println("Open");
    Serial.println();
    digitalWrite(RELAY1,HIGH);          
    delay(250);                                     
    digitalWrite(RELAY1,LOW);
    digitalWrite(LED1,HIGH);
    delay(1500);
    goto depois;

  }
} 
 
 

   if ((digitalRead(LED1) == HIGH)){
 
 {
    Serial.println("Close");
    Serial.println();
    digitalWrite(RELAY2,HIGH);           
    delay(250);                                      
    digitalWrite(RELAY2,LOW);         
    digitalWrite(LED1,LOW);    
    delay(1500);

  }
 }
 depois:
 delay(10);
 }

Hi IR Santos,

Perhaps you did it, but I can advise you to check your wiring conscientiously. A wiring issue can fall the SPI network in an infinite while().

Méric.

thank you Meric, I will take a look at the wiring and see if there's something wrong, but that was the last thing I was hoping to be, I solder a lot and I checked the wiring as I was building the circuit board many times, I hope it's not the wiring but thank you again Meric

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

...R

I really don't know how to use that :sweat_smile: but I will try to learn more about it and see if it solves the problem,

thank you Robin2

Hi,
You are controlling power with Arduino and many people have problems with that.

Many questions (like Where Is Ground?) but suggest you start with Arduino Power page HERE

And here are some suggestions on system design that may help.

If you disconnect the power circuits from the relays and run many operating cycles, does the problem still happen? If not it's an ElectroMagnetic Interference issue.

Let us know what you find out...