-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrfid-reader.ino
80 lines (68 loc) · 1.68 KB
/
rfid-reader.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* RFID CARD READER
*
* Reads RFID cards and does stuff as a consequence.
*
* by Trapped Room Escape
*/
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 3
// Create MFRC522 instance.
MFRC522 mfrc522(SS_PIN, RST_PIN);
const int LED = 13;
// Vitt kort
const unsigned long CARD1 = 4294945317;
// Blå nyckelring
const unsigned long CARD2 = 4294963849;
const unsigned long CARDS[2] = {
4294945317, // Vitt kort
4294963849 // Blå nyckelring
};
const int CARDS_LENGTH = sizeof(CARDS)/sizeof(CARDS[0]);
void setup() {
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
Serial.begin(9600); // Initialize serial communications with the PC
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
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;
}
// Dump debug info about the card. PICC_HaltA() is automatically called.
//mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
// Read card UID
unsigned long uid = readCard();
// Check if card UID is accepted
int i;
for ( i=0; i<CARDS_LENGTH; i++ ) {
if ( uid == CARDS[i] ) {
cardCorrect();
}
}
}
unsigned long readCard() {
unsigned long uid;
uid = mfrc522.uid.uidByte[0] << 24;
uid += mfrc522.uid.uidByte[1] << 16;
uid += mfrc522.uid.uidByte[2] << 8;
uid += mfrc522.uid.uidByte[3];
mfrc522.PICC_HaltA(); // Stop reading
Serial.println(uid);
return uid;
}
void cardCorrect() {
Serial.println("Card recognized");
digitalWrite(LED, HIGH);
delay(1500);
digitalWrite(LED, LOW);
}