-
Notifications
You must be signed in to change notification settings - Fork 0
/
RFID.ino
55 lines (44 loc) · 1.15 KB
/
RFID.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
int markerPin = 8;
byte lastUid[10];
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
pinMode(markerPin, OUTPUT);
digitalWrite(markerPin, LOW);
memset(lastUid, 0, 10);
Serial.println("Scan PICC to see UID and type...");
}
void loop() {
// Look for new cards
if ( !mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if ( !mfrc522.PICC_ReadCardSerial()) {
return;
}
int uidSize = mfrc522.uid.size;
byte uid[10];
memcpy(uid, mfrc522.uid.uidByte, uidSize);
if (memcmp(lastUid, uid, uidSize)) {
memcpy(lastUid, uid, uidSize);
digitalWrite(markerPin, HIGH);
Serial.print(F("Card UID:"));
for (byte i = 0; i < uidSize; i++) {
if(uid[i] < 0x10)
Serial.print(F(" 0"));
else
Serial.print(F(" "));
Serial.print(uid[i], HEX);
}
Serial.println("");
} else {
digitalWrite(markerPin, LOW);
}
}