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;
}
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?"