RFID_RC522 and button and if query

Hello,
i build with my friends a garage with an arduino, my problem is the if query with or
if i change the or to and it works perfectly... now the code

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

#define SS_PIN 53
#define RST_PIN 23
#define ROT_LED_PIN 10
#define GRUEN_LED_PIN 11

MFRC522 mfrc522(SS_PIN, RST_PIN);
Servo servoblau;
Servo servorot;

long code = 0;
int taster = 7;
int tasterstatus = 0;
bool tuerOffen = false;

void setup() {
  Serial.begin(9600);
  servoblau.attach(8);
  servorot.attach(9);
  servoblau.write(360);
  servorot.write(0);
  SPI.begin();
  mfrc522.PCD_Init();
  pinMode(ROT_LED_PIN, OUTPUT);
  pinMode(GRUEN_LED_PIN, OUTPUT);
  digitalWrite(ROT_LED_PIN, HIGH);
  pinMode(taster, INPUT);
}

void loop() {
  Serial.println(tasterstatus);
  tasterstatus = digitalRead(taster);
  if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
    code = 0;
    for (byte i = 0; i < mfrc522.uid.size; i++) {
      code = ((code + mfrc522.uid.uidByte[i]) << 8);
    }
    code >>= 8;
    Serial.print("Die Kartennummer lautet:");
    Serial.println(code);
    if (tasterstatus == 1 || code == -5609216) {
      if (!tuerOffen) {
        tuerOffen = true;
        openthedoor();
      }
    }
  } else {
    tuerOffen = false;
  }
}

void openthedoor() {
  servoblau.write(90);
  servorot.write(90);
  delay(1000);
  digitalWrite(ROT_LED_PIN, LOW);
  digitalWrite(GRUEN_LED_PIN, HIGH);
  delay(3000);
  digitalWrite(ROT_LED_PIN, HIGH);
  digitalWrite(GRUEN_LED_PIN, LOW);
  servorot.write(0);
  delay(1000);
  servoblau.write(360);
  delay(3000);
}

Welcome to the forum

code == -5609216

Where did this magic number come from ?

Hello, if i put the card on the nfc reader this print in the serial monitor.. but it depends sometimes its 1611203204 or something else

That gives you the clue that you haven't got it right doesn't it?

Hi!

Try this method to check if a card is valid:

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

#define RST_PIN         9         // Configurable, see typical pin layout above
#define SS_PIN          10        // Configurable, see typical pin layout above

MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance

int menuIndex = 0;
const int output_pin = 6;

void readRFID()
{
  int j = -1;
  byte card_ID[4];                                        // card UID size 4byte

  const int numOfCards = 1;                               // the nuber of cards used. this can change as you want
  byte cards[numOfCards][4] = {{0x4D, 0xD5, 0x93, 0xDA}}; // array of UIDs of rfid cards

  for (byte i = 0; i < mfrc522.uid.size; i++)
  {
    card_ID[i] = mfrc522.uid.uidByte[i];
  }

  for (int i = 0; i < numOfCards; i++)
  {
    if ((card_ID[0] == cards[i][0]) && (card_ID[1] == cards[i][1]) && (card_ID[2] == cards[i][2]) && (card_ID[3] == cards[i][3]))
    {
      j = i;
    }
  }

  if(j == -1)  // check the card validity
  {
    Serial.println("Invalid");
    menuIndex = 2;
  }
  else
  {
    Serial.println("Valid");
    menuIndex = 1;
  }
}

void setup()
{
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(output_pin, OUTPUT);

  Serial.begin(9600); // Initialize serial communications with the PC
  while (!Serial)     // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
  {
  }
  SPI.begin();                       // Init SPI bus
  mfrc522.PCD_Init();                // Init MFRC522
  delay(4);                          // Optional delay. Some board do need more time after init to be ready, see Readme
  mfrc522.PCD_DumpVersionToSerial(); // Show details of PCD - MFRC522 Card Reader details
  Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));
}

void dump_byte_array(byte *buffer, byte bufferSize)
{
  for (byte i = 0; i < bufferSize; i++)
  {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], HEX);
  }
}

void loop()
{
  switch (menuIndex)
  {
    case 0:
      if (mfrc522.PICC_IsNewCardPresent())
      {
        if (mfrc522.PICC_ReadCardSerial())
        {
          Serial.println(F("UID:"));
          dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
          Serial.println();
          readRFID();
        }
      }
      break;
    case 1: // Access granted.
      digitalWrite(output_pin, HIGH);
      break;
    case 2: // Acess denied.
      digitalWrite(output_pin, LOW);
      menuIndex = 0;
      break;

    default:
      break;
  }
}

Best regards.

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