|
本帖最后由 XlinliY.Zhang 于 2020-8-9 01:49 编辑
首先引用库文件[mw_shl_code=arduino,true]#include <Servo.h>
#include <Keypad.h>
#include <MsTimer2.h>[/mw_shl_code]
舵机库驱动舵机开门
定时中断用于清除密码
键盘库处理矩阵键盘输入
[mw_shl_code=arduino,true]#define PWD1 1234 //密码1
#define PWD2 1234 //密码2
#define lock 130 // 锁门舵机角度
#define unlock 60 //开门舵机角度
#define dead_lock_time 15 //输错锁死时间
#define error_number 5 //多少次错误锁死
#define BLE_Serial Serial //定义蓝牙连接串口[/mw_shl_code]
新建一些变量
[mw_shl_code=arduino,true]String scan = ""; //储存输入的密码
int input_error = 0; //记录输错次数
const byte ROWS = 4; // 矩阵键盘大小
const byte COLS = 4;[/mw_shl_code]
下面是矩阵键盘定义和引脚
[mw_shl_code=arduino,true]char hexaKeys[ROWS][COLS] = {
{'1', '2', '3', '0'},
{'4', '5', '6', '0'},
{'7', '8', '9', '0'},
{'C', '0', '0', 'D'}
};
byte rowPins[ROWS] = {6, 7, 8, 9};
byte colPins[COLS] = {2, 3, 4, 5};[/mw_shl_code]
实例矩阵键盘和舵机对象
[mw_shl_code=arduino,true]Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
Servo myservo;[/mw_shl_code]
下面是初始化蓝牙串口和声明舵机引脚
[mw_shl_code=arduino,true] Serial.begin(38400);
// BLE_Serial.begin(38400);
myservo.attach(11);
pinMode(13, INPUT_PULLUP);//干簧管,检测门状态
pinMode(12, OUTPUT);
pinMode(10, OUTPUT);//蜂鸣器
digitalWrite(12, LOW);
MsTimer2::set(2500, clean);//此处启动定时中断,用于清除密码,因为定时器关系,此处中断实际时间为设置时间的两倍,单位ms
[/mw_shl_code]
loop内容
[mw_shl_code=arduino,true]check_error_number();
check_Serial();//检查串口输入
char customKey = customKeypad.getKey();//检查键盘输入
///////////处理键盘输出
if (customKey) {
Buzzer(1);
//处理正常输入
if (customKey != 'D' && customKey != 'C') {
scan += customKey;
Serial.print("INPUT:");
Serial.println(customKey);
MsTimer2::start();
}
//没有自动校验的话,用于确认,即确认键
else if (customKey == 'D')
{
MsTimer2::stop();
check();
}
//退格
else if (customKey == 'C')
{
MsTimer2::stop();
Buzzer(3);
int alter = scan.toInt();
scan = "";
scan += alter / 10;
Serial.println(scan);
MsTimer2::start();
}
}
//达到位数自动校对密码
automated_proof();
}[/mw_shl_code]
开门函数
[mw_shl_code=arduino,true]
void opening() {
//do while 函数用于检测13号引脚,如果门没开,则一直循环开门
do {
Serial.println("OPENING!");
myservo.write(unlock);
delay(3000);
myservo.write(lock);
} while (digitalRead(13) == 1);
}[/mw_shl_code]蜂鸣器函数,用于各种情况的蜂鸣器发声
[mw_shl_code=arduino,true]void Buzzer(int Type) {
if (Type == 1) {
tone(10, 3000);
delay(50);
noTone(10);
}
else if (Type == 2) {
tone(10, 500);
delay(1000);
noTone(10);
}
else if (Type == 3) {
tone(10, 2000);
delay(50);
noTone(10);
}
}[/mw_shl_code]
检查函数,用于输入密码的校验
[mw_shl_code=arduino,true]void check() {
int inputPWD = scan.toInt();
Serial.println(inputPWD);
if (inputPWD == PWD1 || inputPWD == PWD2) {
Serial.println("Passwaord Ringht!");
opening();
scan = "";
}
else {
scan = "";
Serial.println("Password Error!");
input_error++;
Serial.print("Error number:");
Serial.println(input_error);
Buzzer(2);
}
}[/mw_shl_code]
定时中断服务函数,用于清除输入的密码
[mw_shl_code=arduino,true]void clean() {
scan = "";
Serial.println("Clean all input");
MsTimer2::stop();
}[/mw_shl_code]
检查输入错误密码次数,次数超过阈值用delay锁死程序
[mw_shl_code=arduino,true]void check_error_number() {
if (input_error == 5) {
MsTimer2::stop();
for (int i = 0; i < dead_lock_time; i++) {
delay(1000);
Serial.print("Dead lock");
Serial.print(dead_lock_time - i);
Serial.println("seconds left");
}
input_error = 0;
}
}[/mw_shl_code]
检测蓝牙串口信息函数,用于处理蓝牙数据
[mw_shl_code=arduino,true]void check_Serial() {
if (BLE_Serial.available()) {
if (BLE_Serial.read() == '5') {
Serial.println("BLE Received Opening!");
opening();
}
else {
Serial.println("BLE Received RebootWiFi!");
}
}
}[/mw_shl_code]
自动校验函数,密码到达一定长度自动校对
[mw_shl_code=arduino,true]void automated_proof() {
if (scan.length() == 4) {
MsTimer2::stop();
check();
}
}[/mw_shl_code]
蓝牙软件可以使用任何可以发ascii码的,我使用官方软件,具体可以去谷歌商店下载
|
|