Hello, help me with the code for arduino uno r3

when I connect the necessary key to the rfid, the current is applied to section 6, and then immediately to 5 and goes out, how to fix it so that the current is not applied to section 5 when installing the correct key (excuse me if something is not clear, I am writing through a translator)

#include "SPI.h"
#include "MFRC522.h"

#define RST_PIN 9 // RES pin
#define SS_PIN  10 // SDA (SS) pin

byte readCard[4];
String cardID = "E037BE13"; // замените на ID своей метки
String tagID = "";

MFRC522 mfrc522(SS_PIN, RST_PIN); // создание объекта mfrc522

void setup() {
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
}

void loop() {
  while (getID()) {
    if (tagID == cardID) {
      Serial.println("Access Granted!");
      digitalWrite(5, LOW);
      digitalWrite(6, HIGH);
      delay(2000);
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
    }
    else {
      Serial.println("Access Denied!");
    }
    Serial.print("ID:");
    Serial.println(tagID);
    digitalWrite(5, HIGH);
    digitalWrite(6, LOW);
    delay(2000);
    digitalWrite(6, LOW);
    digitalWrite(5, LOW);
  }
}

boolean getID() {
  if (! mfrc522.PICC_IsNewCardPresent()) {
    return false;
  }

  if (! mfrc522.PICC_ReadCardSerial()) {
    return false;
  }

  tagID = "";
  
  for (uint8_t i = 0; i < 4; i++) {
    tagID.concat(String(mfrc522.uid.uidByte[i], HEX));
  }

  tagID.toUpperCase();
  mfrc522.PICC_HaltA();
  return true;
}

That's not the problem though. The main problem is that any context about your project is missing. Apparently you're doing something with RFID.

It would help if you provided the following:

  • A description of what your project is intended to do
  • A schematic of your hardware setup
  • Clear photos of your wiring
  • A description of what the actual behavior of your system is, and how that deviates from what you intend it to do

Welcome to the forum

I assume that you do not want the state of pin 5 to be affected when the correct tag is read

If that is the case then comment out the lines of code that write to pin 5

If that is not what you want then please explain in more detail

rfid-led
when i connect the right key the current goes to output 6 then to output 5 but i need the current to go only to output 6

I want the current not to go to pin 5 when I apply the correct key

Deleted my post because I don't think it was helpful

no please return as it was useful and worked (sorry if what I write is not clear, that's why I write through a translator)

The reason I deleted my previous post was because I was guessing what you wanted the behaviour to be. Someone else had asked you to say what you wanted the behaviour to be, so I thought I should wait to see your answer, not guess.

You could write your required behaviour in text, or using a flow chart, or a state diagram.

Your code in post #1 could have a description like this for example. I don't know how to format fixed indentation without using code tags. This is a kind of pseudo code.

if new card present
    if ID correct
        print "Access Granted"
        turn LED 6 on
        wait 2 seconds
        turn LED 6 off
    else
        print "Access Denied"
Always do
    print "ID" + tagID
    turn LED 5 on
    wait 2 seconds
    turn LED 5 off

Perhaps you could define what behaviour you want to happen?

Sorry, but I can't explain in detail what I wanted because the translator doesn't translate correctly, but what you posted in the first post in the photo worked.

So as others can make sense of the discussion about the photo:
This is the image I deleted, along with the question "Does it do what you want if you change the code like this?"

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