Task 2 Code
Task 2 Code
*
-----------------------------------------------------------------------------------
---------------------------------
* Example sketch/program showing how to read new NUID from a PICC to serial.
*
-----------------------------------------------------------------------------------
---------------------------------
* This is a MFRC522 library example; for further details and other examples see:
https://github.com/miguelbalboa/rfid
*
* Example sketch/program showing how to the read data from a PICC (that is: a RFID
Tag or Card) using a MFRC522 based RFID
* Reader on the Arduino SPI interface.
*
* When the Arduino and the MFRC522 module are connected (see the pin layout
below), load this sketch into Arduino IDE
* then verify/compile and upload it. To see the output: use Tools, Serial Monitor
of the IDE (hit Ctrl+Shft+M). When
* you present a PICC (that is: a RFID Tag or Card) at reading distance of the
MFRC522 Reader/PCD, the serial output
* will show the type, and the NUID if a new card has been detected. Note: you may
see "Timeout in communication" messages
* when removing the PICC from reading distance too early.
*
* @license Released into the public domain.
*
* Typical pin layout used:
*
-----------------------------------------------------------------------------------
------
* MFRC522 Arduino Arduino Arduino Arduino
Arduino
* Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro
Micro
* Signal Pin Pin Pin Pin Pin Pin
*
-----------------------------------------------------------------------------------
------
* RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
* SPI SS SDA(SS) 10 53 D10 10 10
* SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
*
* More pin layouts for other boards can be found here:
https://github.com/miguelbalboa/rfid#pin-layout
*/
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 53
#define RST_PIN 5
int a = 0;
int b = 0;
void setup() {
Serial.begin(115200);
SPI.begin(); // Init SPI bus
rfid.PCD_Init(); // Init MFRC522
pinMode(40, OUTPUT);
void loop() {
// Reset the loop if no new card present on the sensor/reader. This saves the
entire process when idle.
if ( ! rfid.PICC_IsNewCardPresent())
return;
if (rfid.uid.uidByte[0] != nuidPICC[0] ||
rfid.uid.uidByte[1] != nuidPICC[1] ||
rfid.uid.uidByte[2] != nuidPICC[2] ||
rfid.uid.uidByte[3] != nuidPICC[3] ) {
Serial.println(F("A new card has been detected."));
if (rfid.uid.uidByte[0]==144){
Serial.println("Yes");
digitalWrite(40, 1);
delay(1000);
digitalWrite(40, 0);
a = a+1;
Serial.println(a);
}
else if (rfid.uid.uidByte[0]!=144){
Serial.println("No");
digitalWrite(41, 1);
delay(1000);
digitalWrite(41, 0);
delay(1000);
b = b+1;
Serial.println(b);
}
else Serial.println(F("Card read previously."));
// Halt PICC
rfid.PICC_HaltA();
/**
* Helper routine to dump a byte array as hex values to Serial.
*/
void printHex(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], HEX);
}
}
/**
* Helper routine to dump a byte array as dec values to Serial.
*/
void printDec(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], DEC);
}
}