Arduino Uno零基础入门学习笔记——智能门锁(无舵机)
一、电路接线
RFID-RC522
3V3 |
3.3V |
GND |
GND |
MISO |
12 |
MOSI |
13 |
SCK |
13 |
SDA |
10 |
蜂鸣器
LED
二、代码
源代码来源:DIY打造智能门禁锁RFIDRC522原理与应用
功能:假如卡可以进入,则绿灯闪烁,蜂鸣器叫两声
假如卡不可以进入,则红灯闪烁,蜂鸣器长鸣
记得安装RFID的库 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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
| #include <SPI.h> #include <RFID.h>
RFID rfid(10, 9); unsigned char status; unsigned char str[MAX_LEN];
String accessGranted [2] = {"143043708", "123035296"}; int accessGrantedSize = 2; boolean locked = true;
int redLEDPin = 5; int greenLEDPin = 6; int pinBeep= 8;
void setup() { Serial.begin(9600); SPI.begin(); rfid.init();
pinMode(redLEDPin, OUTPUT); pinMode(greenLEDPin, OUTPUT); pinMode(pinBeep, OUTPUT); digitalWrite(pinBeep, HIGH); digitalWrite(redLEDPin, HIGH); delay(200); digitalWrite(greenLEDPin, HIGH); delay(200);
digitalWrite(redLEDPin, LOW); delay(200); digitalWrite(greenLEDPin, LOW);
Serial.println("Please put the card to the reader"); }
void loop() { if (rfid.findCard(PICC_REQIDL, str) == MI_OK) Serial.println("Card found"); String temp = ""; if (rfid.anticoll(str) == MI_OK) { Serial.print("The card's ID number is : "); for (int i = 0; i < 4; i++) { temp = temp + (0x0F & (str[i] >> 4)); temp = temp + (0x0F & str[i]); } Serial.println (temp); checkAccess (temp); } rfid.selectTag(str); rfid.halt(); }
void checkAccess (String temp) { boolean granted = false; for (int i=0; i <= (accessGrantedSize-1); i++) { if(accessGranted[i] == temp) { Serial.println ("Access Granted"); granted = true;
for(int i=0;i<2;i++) { digitalWrite(pinBeep,LOW); delay(100); digitalWrite(pinBeep,HIGH); delay(100); } digitalWrite(greenLEDPin, HIGH); delay(200); digitalWrite(greenLEDPin, LOW); delay(200); digitalWrite(greenLEDPin, HIGH); delay(200); digitalWrite(greenLEDPin, LOW); delay(200);
} } if (granted == false) { Serial.println ("Access Denied"); digitalWrite(pinBeep,LOW); delay(1000); digitalWrite(pinBeep,HIGH); delay(100); digitalWrite(redLEDPin, HIGH); delay(200); digitalWrite(redLEDPin, LOW); delay(200); digitalWrite(redLEDPin, HIGH); delay(200); digitalWrite(redLEDPin, LOW); delay(200); } }
|
# 三、效果
我有两张卡,一张可以接入,一张不可以接入