whats wrong? can't get it

Hi Guys,

to unlock a PC with RFID, there are a lot of scripts out there, but those only read out an RFID tag and then type in the password in the PC. I want my PC to be unlocked only as long as the RFID card is near to the reader. When I remove the card, I want the PC to be locked again.
Unlocking works as far, but every second time the reader reads out the data from the card, it doesn’t recognize one, so it locks the PC. Then it unlocks it again and so on…

I have replaced the unlock and lock commands with an LED that turns on and off.

#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);

void setup()
{
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
pinMode (2, OUTPUT);
digitalWrite (2, LOW);
}

void loop()
{
long code = 0;
int state = 0;
start:
Serial.println("start");

if (state==1)
  {
    Serial.println("Status is '1'");
  }

else
  {
    Serial.println("Status is '0'");
  }

  
if ( ! mfrc522.PICC_IsNewCardPresent())
    {
      Serial.println("No card found, goto nocard");
        code=0;  //is it code=0 or long code=0?
        delay (3000);
        goto nocard;
    }
  else
      {
        Serial.println("New card found");
        delay (3000);
      }

if ( ! mfrc522.PICC_ReadCardSerial())
{
Serial.println("Couldn't read out card!");
code=0;
}

Serial.println("Card will be read out now");

    
      for (byte i = 0; i < mfrc522.uid.size; i++)
        {
          code=((code+mfrc522.uid.uidByte[i])*10);
          Serial.print("Number of the card is: ");
          Serial.println(code);
        }
    

nocard:

if ((state==1) && (code==2132730))
    {
      Serial.println("Card valid. Status is still '1'");
      delay (3000);
        goto start;
    }
  else
      {
        if (state==1)
            {
              digitalWrite (2, LOW);
              Serial.println("Computer unlocked");
              state=0;
              delay (3000);
                goto start;
            }
          else
              {
                if (code==2132730)
                    {
                      Serial.println("Computer locked");
                      digitalWrite (2, HIGH);
                      state=1;
                      delay (3000);
                        goto start;
                    }
              }
      }
}

The serial output looks like this:

start
Status is '0'
No card found, goto nocard
start
Status is '0'
No card found, goto nocard
start
Status is '0'
New card found
Card will be read out now
Number of the card is: 1930
Number of the card is: 21180
Number of the card is: 213070
Number of the card is: 2132730
Computer locked
start
Status is '1'
No card found, goto nocard
Computer unlocked
start
Status is '0'
New card found
Card will be read out now
Number of the card is: 1930
Number of the card is: 21180
Number of the card is: 213070
Number of the card is: 2132730
Computer locked
start
Status is '1'
No card found, goto nocard
Computer unlocked
start
Status is '0'
No card found, goto nocard
.
.
.

Can anyone find the mistake?

Thanks so far,
CueMaxX

Don’t use GOTO - goodness knows what sort of tangle it causes.

I’m not even going to try to figure out what is going on - my brain is already turning to porridge.

…R

Your code stays in the loop() and does not exit.

Normally, when your loop() returns, flow of control goes back to the underlying main() function, which does certain hardware things that I don't know much about. Maybe stuff is getting weird because this isn't happenning.

Try moving code and state into global space outside the loop() function, and changing those 'goto start' into returns. This will exit the loop() function and let the underlyuing main() do its thing.

You need realistic expectations. Typically, an RFID reader reads the card ONCE when it is presented, and sends that information to the controller. It does nothing when the card is removed.