系統(tǒng)描述
題目:《arduino 密碼鎖設計》
功能:
可以設置密碼,開鎖成功
五次輸入密碼,錯誤報警(蜂鳴器)
液晶顯示(1602)
?
使用的模塊:
?
模塊實物圖
?
接線圖
LCD1602
LCD1602? IIC -- IIC
?
紅外接收
紅外接收?-- 5
?
繼電器
繼電器?-- 2
?
蜂鳴器
蜂鳴器?-- 3
?
RC522
SDA--10
SCK--13
MOSI--11
MISO--12
其余信號線閑置。
?
紅外遙控信號
?
源代碼
/***************************
?*?基于 Arduino 的智能門鎖
?*?作者:劉增林
?*?時間:2020.12.05
?***************************
?*/
?
#include??
#include??//1602LCD?驅動??引用 I2C 庫
#include?
#include?
#include?
#define?SS_PIN?10
#define?RST_PIN?9
unsigned?char?i?=?0,?j?=?0,?m?=?0,?n?=?0;??????// 循環(huán)變量
unsigned?char?code[6];???????????// 保存密碼
unsigned?char?code_temp[6];??????// 保存臨時輸入密碼
int?rf522_flag?=?0;??????????????// 門禁卡標志位
int?Setpassword_flag?=?1;????????// 設置密碼標志位
int?Inputpassword_flag?=?1;??????// 輸入密碼標志位
int?door_flag?=?0;???????????????// 門標志位
// 管腳定義
const?int?buzzer?=?3;
const?int?realy?=?2;
LiquidCrystal_I2C?lcd(0x27,16,2);??// 配置 LCD 地址及行列
int?RECV_PIN?=?5;
IRrecv?irrecv(RECV_PIN);???// 定義接收引腳為 11 腳 .
decode_results?results;????// 聲明一個 IRremote 庫函數(shù)獨有的變量類型(decode_resultc)
MFRC522?rfid(SS_PIN,?RST_PIN);?// 實例化類
?
//?初始化數(shù)組用于存儲讀取到的 NUID?
byte?nuidPICC[4];
void?setup()?{
??//?put?your?setup?code?here,?to?run?once:
??// 管腳初始化
??pinMode(realy,?OUTPUT);
??digitalWrite(realy,?HIGH);
??pinMode(buzzer,?OUTPUT);
??digitalWrite(buzzer,?HIGH);
??
??SPI.begin();?//?初始化 SPI 總線
??rfid.PCD_Init();?//?初始化?MFRC522?
????
??//LCD1602
??lcd.init();??????// 初始化 LCD?
??lcd.backlight();?// 設置 LCD 背光
??lcd.setCursor(0,0);????????????// 設置顯示指針??
??lcd.print("?Set?Clded?Lock?");?// 輸出字符到 LCD1602 上??
??lcd.setCursor(0,1);????????????// 設置顯示指針??
??lcd.print("Password:");????????// 輸出字符到 LCD1602 上??
??
??// 紅外接收
??irrecv.enableIRIn();?// 開始接收信號
??Serial.begin(9600);?
}
void?loop()?{
??//?put?your?main?code?here,?to?run?repeatedly:
??while?(Setpassword_flag)?// 設置密碼
??{
????if?(irrecv.decode(&results))??// 檢查是否接收到紅外遙控信號
????{?
??????Serial.println(results.value,?HEX);?// 輸出 16 進制接收到的數(shù)據(jù)指令信息
??????switch(results.value)// 通過這個循環(huán),比對
??????{
????????case?0xFF9867:?
??????????lcd.print("0");delay(500);code[i]=0;i++;break;
????????case?0XFFA25D:?
??????????lcd.print("1");delay(500);code[i]=1;i++;break;
????????case?0xFF629D:?
??????????lcd.print("2");delay(500);code[i]=2;i++;break;
????????case?0xFFE21D:?
??????????lcd.print("3");delay(500);code[i]=3;i++;break;
????????case?0xFF22DD:?
??????????lcd.print("4");delay(500);code[i]=4;i++;break;
????????case?0xFF02FD:?
??????????lcd.print("5");delay(500);code[i]=5;i++;break;
????????case?0xFFC23D:?
??????????lcd.print("6");delay(500);code[i]=6;i++;break;
????????case?0xFFE01F:?
??????????lcd.print("7");delay(500);code[i]=7;i++;break;
????????case?0xFFA857:?
??????????lcd.print("8");delay(500);code[i]=8;i++;break;
????????case?0xFF906F:?
??????????lcd.print("9");delay(500);code[i]=9;i++;break;
????????default:
??????????break;??
??????}
??????
??????irrecv.resume();?// 接收下一指令??
??
??????if?(i?==?6)
??????{
????????Setpassword_flag?=?0;?// 清除設置密碼標志位
????????Serial.print("Password?set?successfullyn");
????????for?(j?=?0;?j?<?6;?j++)
????????{
??????????Serial.print(code[j]);
??????????}
????????Serial.println("?");
????????
????????digitalWrite(realy,?LOW);??// 密碼設置成功,開門
????????delay(2000);
????????digitalWrite(realy,?HIGH);?
????????
????????lcd.clear();???????????????????// 清屏
????????lcd.setCursor(0,0);????????????// 設置顯示指針??
????????lcd.print("===Clded?Lock===");?// 輸出字符到 LCD1602 上??
????????lcd.setCursor(0,1);????????????// 設置顯示指針??
????????lcd.print("Password:");????????// 輸出字符到 LCD1602 上
????????}
????}
??}??// 密碼設置完成
??while?(Inputpassword_flag)
??{
????if?(irrecv.decode(&results))??// 檢查是否接收到紅外遙控信號
????{?
??????Serial.println(results.value,?HEX);?// 輸出 16 進制接收到的數(shù)據(jù)指令信息
??????switch(results.value)// 通過這個循環(huán),比對
??????{
????????case?0xFF9867:?
??????????lcd.print("0");delay(500);code_temp[m]=0;m++;break;
????????case?0XFFA25D:?
??????????lcd.print("1");delay(500);code_temp[m]=1;m++;break;
????????case?0xFF629D:?
??????????lcd.print("2");delay(500);code_temp[m]=2;m++;break;
????????case?0xFFE21D:?
??????????lcd.print("3");delay(500);code_temp[m]=3;m++;break;
????????case?0xFF22DD:?
??????????lcd.print("4");delay(500);code_temp[m]=4;m++;break;
????????case?0xFF02FD:?
??????????lcd.print("5");delay(500);code_temp[m]=5;m++;break;
????????case?0xFFC23D:?
??????????lcd.print("6");delay(500);code_temp[m]=6;m++;break;
????????case?0xFFE01F:?
??????????lcd.print("7");delay(500);code_temp[m]=7;m++;break;
????????case?0xFFA857:?
??????????lcd.print("8");delay(500);code_temp[m]=8;m++;break;
????????case?0xFF906F:?
??????????lcd.print("9");delay(500);code_temp[m]=9;m++;break;
????????default:
??????????break;??
??????}
??????
??????irrecv.resume();?// 接收下一指令??
??????
??????if?(m?==?6)
??????{
????????
????????m?=?0;
??
????????if?((code[0]?==?code_temp[0])?&&?(code[1]?==?code_temp[1])?&&?(code[2]?==?code_temp[2])?&&?(code[3]?==?code_temp[3])?&&?(code[4]?==?code_temp[4])?&&?(code[5]?==?code_temp[5]))
????????{
??????????digitalWrite(realy,?LOW);??// 密碼設置成功,開門
??????????delay(2000);
??????????digitalWrite(realy,?HIGH);?
??????????}
????????else
????????{
??????????n++;
??????????}
??????????
????????lcd.clear();???????????????????// 清屏
????????lcd.setCursor(0,0);????????????// 設置顯示指針??
????????lcd.print("===Clded?Lock===");?// 輸出字符到 LCD1602 上??
????????lcd.setCursor(0,1);????????????// 設置顯示指針??
????????lcd.print("Password:");????????// 輸出字符到 LCD1602 上
????????
??????}
????????
??????while?(n?==?5)// 錯誤五次蜂鳴器報警
??????{
????????// 蜂鳴器報警 2s
????????digitalWrite(buzzer,?LOW);
????????}??????????
????}
??//?找卡
??if?(?!?rfid.PICC_IsNewCardPresent())
??{
????rf522_flag?=?1;
????return;
????}
????
??if?(rf522_flag?==?1)
??{
????digitalWrite(realy,?LOW);
????delay(2000);
????rf522_flag?=?0;
????digitalWrite(realy,?HIGH);
????}???
??}
}
閱讀全文