RFID with LED turning red and green light

Hello everyone,
im a really new in Arduino World. I would be really happy, if you guys can help me out to for my school project. Im trying to make a security system with RFID and LED. It is actually really simple but im not sure how should i change my Code. I have already a code that works. I can light the greed led up, if i use the right card. If i use the second card the red goes on. But i want to upgrade it. If i show the right card i need to get green led goes on otherwise red. I need the change it with else commend. But i have no idea how i do it. I wrote my all code here so you can guys see, what i reached. But i cant go far more then. I would be really happy if you guys can help me :slight_smile:

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

#define PIN_RESET  5  // SPI Reset Pin
#define PIN_SS    53  // SPI Slave Select Pin

#define PIN_LED_GREEN 2
#define PIN_LED_RED  7


MFRC522 mfrc522(PIN_SS, PIN_RESET);
bool isGreen, isRed;

// insert the IDs of your personal NFC tags
byte uidGreen[] = {0xDB, 0x7F, 0xEA, 0xD5};
byte uidRed[]  = {0x3A, 0x56, 0xDA, 0x29};

void setup()
{
    SPI.begin();
    mfrc522.PCD_Init();

    pinMode(PIN_LED_GREEN, OUTPUT);
    pinMode(PIN_LED_RED, OUTPUT);
}

void loop()
{
    // PICC = proximity integrated circuit card
    if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
        isGreen = true;
        isRed = true;
        for (byte i=0; i<4; i++) {
            if (mfrc522.uid.uidByte[i] != uidGreen[i]) {
                isGreen = false;
            }
            if (mfrc522.uid.uidByte[i] != uidRed[i]) {
                isRed = false;
            }
        }

        if (isGreen) {
            digitalWrite(PIN_LED_GREEN, HIGH);
        }
        if (isRed) {
            digitalWrite(PIN_LED_RED, HIGH);
        }

        mfrc522.PICC_HaltA();
        delay(1000);

        digitalWrite(PIN_LED_GREEN, LOW);
        digitalWrite(PIN_LED_RED, LOW);
    }
}

i suggest you consider using a sub-function to compare ID for each ID you have to determine if there is a match and if there is a match, turn an LED.

the sub-function would return true if there's a match and can be the condition in an if statement.

perhaps you want to simply turn the green LED if there is a match and the red LED if there isn't

post your attempt

if else
else - Arduino Reference

Try this:

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

#define PIN_RESET  5  // SPI Reset Pin
#define PIN_SS    53  // SPI Slave Select Pin

#define PIN_LED_GREEN 2
#define PIN_LED_RED  7


MFRC522 mfrc522(PIN_SS, PIN_RESET);

// insert the IDs of your personal NFC tags
byte uidGreen[] = {0xDB, 0x7F, 0xEA, 0xD5};
byte uidRed[]  = {0x3A, 0x56, 0xDA, 0x29};

void setup()
{
    SPI.begin();
    mfrc522.PCD_Init();

    pinMode(PIN_LED_GREEN, OUTPUT);
    pinMode(PIN_LED_RED, OUTPUT);
}

void loop()
{
    // PICC = proximity integrated circuit card
    if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
        isGreen = true;
        isRed = true;
        for (byte i=0; i<4; i++) {
            if (mfrc522.uid.uidByte[i] == uidGreen[i]) {
                digitalWrite(PIN_LED_RED, LOW);
                digitalWrite(PIN_LED_GREEN, HIGH);
                break;
            } else {
                digitalWrite(PIN_LED_RED, HIGH);
                digitalWrite(PIN_LED_GREEN, LOW);
                break;
       }

        mfrc522.PICC_HaltA();
        delay(1000);

        digitalWrite(PIN_LED_GREEN, LOW);
        digitalWrite(PIN_LED_RED, LOW);
    }
}

these lines wouldn't be necessary -- but i don't think that's what the OP is after

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