1 //整理者:极客工坊bg1lsy ([email protected])
2 //整理时间:2013.05.25
3 #include <SPI.h>
4 #include <RFID.h>
5
6 RFID rfid(10,5); //D10--读卡器MOSI引脚、D5--读卡器RST引脚
7
8 int led = 9;
9 int relay=7;
10
11 bool state=true;
12 void setup()
13 {
14 Serial.begin(9600);
15 SPI.begin();
16 rfid.init();
17 pinMode(led, OUTPUT);
18 pinMode(relay,OUTPUT);
19 digitalWrite(relay,HIGH);
20 }
21
22 void loop()
23 {
24 unsigned char type[MAX_LEN];
25 //找卡
26 if (rfid.isCard( type)) {
27 Serial.println("Find the card!");
28 ;
29 // Show card type
30 ShowCardType(type);
31 //读取卡序列号
32 if (rfid.readCardSerial()) {
33 Serial.print("The card's number is : ");
34 Serial.print(rfid.serNum[0],HEX);
35 Serial.print(rfid.serNum[1],HEX);
36 Serial.print(rfid.serNum[2],HEX);
37 Serial.print(rfid.serNum[3],HEX);
38 Serial.print(rfid.serNum[4],HEX);
39 Serial.println(" ");
40 ShowUser(rfid.serNum);
41 }
42 //选卡,可返回卡容量(锁定卡片,防止多数读取),去掉本行将连续读卡
43 Serial.println(rfid.selectTag(rfid.serNum));
44 }
45
46 rfid.halt();
47 }
48
49 void ShowCardType( unsigned char* type)
50 {
51 Serial.print("Card type: ");
52 if(type[0]==0x04&&type[1]==0x00)
53 Serial.println("MFOne-S50");
54 else if(type[0]==0x02&&type[1]==0x00)
55 Serial.println("MFOne-S70");
56 else if(type[0]==0x44&&type[1]==0x00)
57 Serial.println("MF-UltraLight");
58 else if(type[0]==0x08&&type[1]==0x00)
59 Serial.println("MF-Pro");
60 else if(type[0]==0x44&&type[1]==0x03)
61 Serial.println("MF Desire");
62 else
63 Serial.println("Unknown");
64 }
65
66 void ShowUser( unsigned char* id)
67 {
68 //EE 9B 9C 38 D1
69 if( id[0]==0xEE && id[1]==0x9B && id[2]==0x9C && id[3]==0x38 ) {
70 Serial.println("Hello Mary!");
71 state=RelayStatus(state);
72 }
73 else if(id[0]==0x24 && id[1]==0x12 && id[2]==0xE0 && id[3]==0x13) {
74 Serial.println("Hello MicroHao!");
75 state=RelayStatus(state);
76 }
77 else{
78 Serial.println("Hello unkown guy!");
79 BlinkLED();
80
81 }
82 }
83 bool RelayStatus(bool status)
84 {
85 if(status)
86 {
87 digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
88
89 digitalWrite(relay,LOW);
90 return false;
91 }
92 digitalWrite(led, LOW); // turn the LED on (HIGH is the voltage level)
93
94 digitalWrite(relay,HIGH);
95 return true;
96 }
97 void BlinkLED()
98 {
99 digitalWrite(relay,HIGH);
100 for(int i=0;i<3;i++)
101 {
102 digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
103 delay(1000);
104 digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
105 delay(1000);
106 }
107 }
复制代码