I recently purchased two RC522 RFID Reader/Writers. I've been playing with them for about three weeks and have done many hours of research and reading in an attempt to understand the programming. There are many YouTube videos and a few code examples, but very little explanation. I now seek your assistance. I found the base code below through one of those YouTube videos, and I'm able to use it, edit it, and make it work, but I don't understand some critical parts of it.
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);
int greenLED = 6;
int redLED = 4;
void setup() {
SPI.begin();
mfrc522.PCD_Init();
pinMode(greenLED,OUTPUT); //green LED
pinMode(redLED,OUTPUT); //red LED
Serial.begin(9600);
}
void loop() {
Serial.println("Scan your RFID card now.");
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
byte myid[4] ={0x04,0x00,0x41,0x82}; //Card B
byte myid2[4] ={0x04,0x26,0xA5,0x82}; //Card A
byte isok= 0;
byte isok2= 0;
for (byte i = 0; i < mfrc522.uid.size; i++) {
if (mfrc522.uid.uidByte[i]==myid[i]) {
isok= isok+1;
}
}
for (byte i = 0; i < mfrc522.uid.size; i++) {
if (mfrc522.uid.uidByte[i]==myid2[i]) {
isok2= isok2+1;
}
}
if (isok==4){
Serial.println("Hello Emma! Welcome to my world!");
digitalWrite(greenLED,HIGH);
delay(2000);
digitalWrite(greenLED,LOW);
delay(2000);
digitalWrite(greenLED,HIGH);
delay(2000);
digitalWrite(greenLED,LOW);
delay(2000);
}
if (isok2==4){
Serial.println("Hello Benjamin. Prepare for the Robot Invasion!");
delay(250);
digitalWrite(greenLED,HIGH);
delay(250);
digitalWrite(greenLED,LOW);
delay(250);
digitalWrite(greenLED,HIGH);
delay(250);
digitalWrite(greenLED,LOW);
delay(250);
}
else if (isok < 4){
Serial.println("Denied!");
digitalWrite(redLED,HIGH);
delay(3000);
digitalWrite(redLED,LOW);
delay(2000);
}
}
Question 1) I know that the byte myid[4] = {0x04,0x00,0x41,0x82}; is my RFID card's unique ID. I can change this and get it to register fine. What I don't understand is the [4]. What does the bracket 4 get used to do? And why does byte myid2 also use [4]?
Question 2) What are the two for/if statements doing? Correction, I believe that this is where the sketch is matching the unique ID of the card to the unique ID found in the myid[4], but I don't understand how. I'm not sure how to read this section aloud or explain it. In those if statements there is also a part that adds 1 to the isok variable. I'm not sure why this is happening.
Thank you in advance for any help in explaining how/why this functions. I know that I can continue to use it as is and get away with it, but I'd really like to understand it.