My problem is the printing line: Now the card, please!
It should be written only once and after that, the program should wait for the new card.
This code should be saved and compared to the new code from the second go through.
If both codes are the same the key is the new one.
The other question is if I have to use this
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
long code = 0;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
code = ((code + mfrc522.uid.uidByte[i]) * 10);
}
again in the second comparising.
I did not get it if I have to use these lines every time I want to communicate with the cards.
That's the programm:
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
long nCode;
long snCode;
int i = 1;
boolean secondTime = false;
MFRC522 mfrc522(SS_PIN, RST_PIN);
void setup()
{
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
Serial.println("It starts now");
}
void loop()
{
if (i == 1) //only one gothrough
{
boolean firstTime = true;
if (firstTime == true)
{
Serial.println("Now, the new card!");
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
long code = 0;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
code = ((code + mfrc522.uid.uidByte[i]) * 10);
}
nCode = code;
secondTime = true;
if(firstTime == true)
{
Serial.println("Again please");
}
firstTime = false;
snCode = code;
Serial.println(snCode);
if (snCode == nCode)
{
Serial.println("It works!");
i++;
}
else
{
Serial.println("Try again");
firstTime = true;
secondTime = false;
}
}
}
}
Thanks