My arduino always return to void setup after scanning a RFID-badge.

Hello,

I am working on a project that involves to make a memory game with bluetooth instead of buttons. But
before you can play with the memory game. You have to scan the right RFID-badge to get the bluetooth pin-code. So now my problem is that always when I scan a RFID-badge, well it always return in void setup. Normally, void setup should only runs once. Here I added my code and a link for some pictures.

https://drive.google.com/drive/folders/1L_rQ5QzZpZxSZNmpEyFXmrbWX0e1fY4m?usp=sharing

#include <SPI.h>

#include <deprecated.h>
#include <MFRC522.h>
#include <MFRC522Extended.h>
#include <require_cpp11.h>

#include <Wire.h>

#include <LiquidCrystal_I2C.h>

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);

const int pinRST = 9;
const int pinSS = 10;
MFRC522 mfrc522(pinSS, pinRST);

int incomingByte;
int rood = 2;
int wit = 3;
int geel = 4;
int groen = 5;
int lijstGetallen[14];
int teller = 4;
int tellerr = 1;
boolean doorOfNiet = true;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();
  randomSeed((analogRead(0)));
  pinMode(6, OUTPUT);
  pinMode(rood, OUTPUT);
  pinMode(wit, OUTPUT);
  pinMode(geel, OUTPUT);
  pinMode(groen, OUTPUT);
  for(int a = 0; a < sizeof(lijstGetallen); a++){
    lijstGetallen[a] = random(2,6);
  }
  lcd.begin();
  lcd.setCursor(0,0);
  lcd.print("Luister en scan");
  lcd.setCursor(0,1);
  lcd.print("de juiste badge");
    delay(2000);
    tone(6, 500);
    delay(250);
    noTone(6);
    delay(250);
    tone(6, 500);
    delay(250);
    noTone(6);
    delay(250);
    tone(6, 500);
    delay(1000);
    noTone(6);
    delay(250);
    tone(6, 500);
    delay(1000);
    noTone(6);
    delay(500);
    tone(6, 500);
    delay(1000);
    noTone(6);
    delay(500);
}


void loop() {
  // put your main code here, to run repeatedly:
  //hier wordt de memory game afgespeeld
  while ( ! mfrc522.PICC_IsNewCardPresent()){
    
  }

  // Select one of the cards
  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) == "2B 41 71 0D") //change here the UID of the card/cards that you want to give access
  {
    lcd.clear();
    digitalWrite(5, HIGH);
    delay(1000);
    digitalWrite(5, LOW);
    lcd.setCursor(0,0);
    lcd.print("proficiat, pass:");
    lcd.setCursor(0,1);
    lcd.print("1234");
    
  }
 
 else   {
    lcd.clear();
    digitalWrite(2, HIGH);
    delay(1000);
    digitalWrite(2, LOW);
    lcd.print("foute badge");
  }

  lcd.setCursor(0,0);
  lcd.print("level " + (String)tellerr);
  for(int b = 0; b < teller; b++){
     digitalWrite(lijstGetallen[b], HIGH);
     delay(500);
     digitalWrite(lijstGetallen[b], LOW);
     delay(500);
  }
  
  //hier voert de gebruiker zijn gegevens
  for(int c = 0; c < teller; c++){
    while(Serial.available() <= 0){
    
     }
     if (Serial.available() > 0) {
      incomingByte = Serial.read();
      switch(incomingByte){
        case '5':
          digitalWrite(groen, HIGH);
          delay(500);
          digitalWrite(groen, LOW);
          break;
        case '4':
          digitalWrite(geel, HIGH);
          delay(500);
          digitalWrite(geel, LOW);
          break;
        case '3':
          digitalWrite(wit, HIGH);
          delay(500);
          digitalWrite(wit, LOW);
          break;
        case '2':
          digitalWrite(rood, HIGH);
          delay(500);
          digitalWrite(rood, LOW);
          break;
      }
      delay(200);
      if((incomingByte-48) != lijstGetallen[c]){
        doorOfNiet = false;
        lcd.clear();
        lcd.print("failed");
        tone(6,500);
        for(int d = 0; d < 4; d++){
          digitalWrite(rood, HIGH);
          digitalWrite(wit, HIGH);
          digitalWrite(geel, HIGH);
          digitalWrite(groen, HIGH);
          delay(250);
          digitalWrite(rood, LOW);
          digitalWrite(wit, LOW);
          digitalWrite(geel, LOW);
          digitalWrite(groen, LOW); 
          delay(250);
        }
        noTone(6);
      }
   }
  }
  //blinkende lampen en buzzer indien alles juist
  delay(200);
  if(doorOfNiet){
    tellerr++;
    if(teller < 14){
       teller++;
    }
    tone(6,1000);
    digitalWrite(rood, HIGH);
    digitalWrite(wit, HIGH);
    digitalWrite(geel, HIGH);
    digitalWrite(groen, HIGH);
    delay(500);
    digitalWrite(rood, LOW);
    digitalWrite(wit, LOW);
    digitalWrite(geel, LOW);
    digitalWrite(groen, LOW);
    noTone(6);
    delay(1000); 
  }
}
  if ( ! mfrc522.PICC_ReadCardSerial())
  {
    return;
  }

You have a serious problem in setup() where you are writing outside the bounds of the array lijstGatellen. sizeof(lijstGetellen) will be 28 on most arduino boards (int is a 16-bit integer). To get the correct size, use sizeof(lijstGetellen) / sizeof(lijstGetellen[ 0 ])

int lijstGetallen[14];

void setup() {
  ...
  ...
  for(int a = 0; a < sizeof(lijstGetallen); a++){
    lijstGetallen[a] = random(2,6);
  }
  ...
  ...

david_2018:
You have a serious problem in setup() where you are writing outside the bounds of the array lijstGatellen.

And writing off the end of an array generally results in a reset.

DrAzzy:
And writing off the end of an array generally results in a reset.

Thanks for your answer DrAzzy. You are literally a hero too.

david_2018:
You have a serious problem in setup() where you are writing outside the bounds of the array lijstGatellen. sizeof(lijstGetellen) will be 28 on most arduino boards (int is a 16-bit integer). To get the correct size, use sizeof(lijstGetellen) / sizeof(lijstGetellen[ 0 ])

int lijstGetallen[14];

void setup() {
 …
 …
 for(int a = 0; a < sizeof(lijstGetallen); a++){
   lijstGetallen[a] = random(2,6);
 }
 …
 …

Thank you David, you are a hero. It works ! But I don’t understand why ? The length of my array is 14 so why it will be on most arduino’s 28 ?Thanks again for your help hero.

sizeof() returns the number of bytes that an array occupies in memory. An int is a 16-bit integer, which is two bytes, so an array of 14 integers occupies 28 bytes in memory.

Note - most arduino boards use two bytes for int, but there are some (the Arduino Due, the SAMD based boards, etc) that use four bytes for an int, in which case the size of your array would be 56 bytes.

If you want to know the number of elements in (any type of) array, you can define a macro. Demonstrated in below

#define NUMELEMENTS(x) (sizeof(x)/ sizeof(x[0]))

int arrayInt[33];
float arrayFloat[12];

void setup()
{
  Serial.begin(57600);
  Serial.print("arrayInt contains ");
  Serial.print(NUMELEMENTS(arrayInt));
  Serial.print(" elements and occupies ");
  Serial.print(sizeof(arrayInt));
  Serial.println(" bytes");
  Serial.print("arrayFloat contains ");
  Serial.print(NUMELEMENTS(arrayFloat));
  Serial.print(" elements and occupies ");
  Serial.print(sizeof(arrayFloat));
  Serial.println(" bytes");
}

void loop()
{
}