If Function with RFID and input

Hello everyone,
I can't get any further, any help would be appreciated.

The program should show the white LED if the karte_check or chip_check was recognized and not the input control.
And the red_LED if karte_check or chip_check and the control input.

Reading the card works, it's only the if function that causes problems.

Below is the full program.

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

#define RST_PIN 9 // SPI Reset Pin
#define SS_PIN 10 // SPI Slave Select Pin

MFRC522 mfrc522(SS_PIN, RST_PIN); // Instanz des MFRC522 erzeugen

byte karte_uid[] = {0x55, 0x25, 0x8A, 0x22};
byte chip_uid[] = {0xBA, 0xFA, 0xD3, 0x81};

int white_led = 5;
int red_led = 4;
int control = 3;

int karte_check = false;
int chip_check = false;

void setup() {
// Diese Funktion wird einmalig beim Start ausgeführt
Serial.begin(9600); // Serielle Kommunikation mit dem PC initialisieren
SPI.begin(); // Initialisiere SPI Kommunikation
mfrc522.PCD_Init(); // Initialisiere MFRC522 Lesemodul

pinMode(white_led, OUTPUT);
pinMode(red_led, OUTPUT);
pinMode(control, INPUT);
}

void loop() {
// Diese Funktion wird in Endlosschleife ausgeführt

// Nur wenn eine Karte gefunden wird und gelesen werden konnte, wird der Inhalt von IF ausgeführt
// PICC = proximity integrated circuit card = kontaktlose Chipkarte
if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial() ) {
Serial.print("Gelesene UID:");
for (byte i = 0; i < mfrc522.uid.size; i++) {
// Abstand zwischen HEX-Zahlen und führende Null bei Byte < 16
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
}
Serial.println();

//KARTE // vergleiche eingelesene ID mit Karte
karte_check = true;
for (int j=0; j<4; j++) {
  if (mfrc522.uid.uidByte[j] != karte_uid[j]) {
    karte_check = false;
  }
}
//KARTE // vergleiche eingelesene ID mit chip
chip_check = true;
for (int j=0; j<4; j++) {
  if (mfrc522.uid.uidByte[j] != chip_uid[j]) {
    chip_check = false;
  }
}

if (( karte_check || chip_check ) == control){
  digitalWrite(red_led, HIGH);
}
if (( karte_check || chip_check ) != control){
  digitalWrite(white_led, HIGH);  
}

// Versetzt die gelesene Karte in einen Ruhemodus, um nach anderen Karten suchen zu können.
mfrc522.PICC_HaltA();
delay(1000);
digitalWrite(red_led, LOW);
digitalWrite(white_led,LOW);

}
}

You are comparing the result of a logical statement with the number 3 ? I presume that is not what you want to do? Maybe you want to read the value of pin 3 and compare that?

Yes, I actually wanted to compare the value...

then you need to compare against digitalRead(control)

if (( karte_check || chip_check ) == digitalRead (control)){
digitalWrite(red_led, HIGH);
}
if (( karte_check || chip_check ) != digitalRead (control)){
digitalWrite(white_led, HIGH);
}

Do you mean that?

And thank you for your quick reply!!

Yes

It now always switches the red_led

Sorry, I just made a mistake, everything is working now!
Thank you very much

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