制作一个简易宿舍蓝牙密码锁
本帖最后由 XlinliY.Zhang 于 2020-8-9 01:49 编辑首先引用库文件#include <Servo.h>
#include <Keypad.h>
#include <MsTimer2.h>
舵机库驱动舵机开门
定时中断用于清除密码
键盘库处理矩阵键盘输入
#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 //定义蓝牙连接串口
新建一些变量
String scan = ""; //储存输入的密码
int input_error = 0; //记录输错次数
const byte ROWS = 4; // 矩阵键盘大小
const byte COLS = 4;
下面是矩阵键盘定义和引脚
char hexaKeys = {
{'1', '2', '3', '0'},
{'4', '5', '6', '0'},
{'7', '8', '9', '0'},
{'C', '0', '0', 'D'}
};
byte rowPins = {6, 7, 8, 9};
byte colPins = {2, 3, 4, 5};
实例矩阵键盘和舵机对象
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
Servo myservo;
下面是初始化蓝牙串口和声明舵机引脚
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
loop内容
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();
}
开门函数
void opening() {
//do while 函数用于检测13号引脚,如果门没开,则一直循环开门
do {
Serial.println("OPENING!");
myservo.write(unlock);
delay(3000);
myservo.write(lock);
} while (digitalRead(13) == 1);
}蜂鸣器函数,用于各种情况的蜂鸣器发声
void Buzzer(intType) {
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);
}
}
检查函数,用于输入密码的校验
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);
}
}
定时中断服务函数,用于清除输入的密码
void clean() {
scan = "";
Serial.println("Clean all input");
MsTimer2::stop();
}
检查输入错误密码次数,次数超过阈值用delay锁死程序
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;
}
}
检测蓝牙串口信息函数,用于处理蓝牙数据
void check_Serial() {
if (BLE_Serial.available()) {
if (BLE_Serial.read() == '5') {
Serial.println("BLE Received Opening!");
opening();
}
else {
Serial.println("BLE Received RebootWiFi!");
}
}
}
自动校验函数,密码到达一定长度自动校对
void automated_proof() {
if (scan.length() == 4) {
MsTimer2::stop();
check();
}
}
蓝牙软件可以使用任何可以发ascii码的,我使用官方软件,具体可以去谷歌商店下载
库文件arduino库管理器下载即可 首先引用库文件#include <Servo.h>
#include <Keypad.h>
#include <MsTimer2.h>
舵机库驱动舵机开门
定时中断用于清除密码
键盘库处理矩阵键盘输入
#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 //定义蓝牙连接串口
新建一些变量
String scan = ""; //储存输入的密码
int input_error = 0; //记录输错次数
const byte ROWS = 4; // 矩阵键盘大小
const byte COLS = 4;
下面是矩阵键盘定义和引脚
char hexaKeys = {
{'1', '2', '3', '0'},
{'4', '5', '6', '0'},
{'7', '8', '9', '0'},
{'C', '0', '0', 'D'}
};
byte rowPins = {6, 7, 8, 9};
byte colPins = {2, 3, 4, 5};
实例矩阵键盘和舵机对象
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
Servo myservo;
下面是初始化蓝牙串口和声明舵机引脚
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
loop内容
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();
}
开门函数
void opening() {
//do while 函数用于检测13号引脚,如果门没开,则一直循环开门
do {
Serial.println("OPENING!");
myservo.write(unlock);
delay(3000);
myservo.write(lock);
} while (digitalRead(13) == 1);
}蜂鸣器函数,用于各种情况的蜂鸣器发声
void Buzzer(intType) {
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);
}
}
检查函数,用于输入密码的校验
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);
}
}
定时中断服务函数,用于清除输入的密码
void clean() {
scan = "";
Serial.println("Clean all input");
MsTimer2::stop();
}
检查输入错误密码次数,次数超过阈值用delay锁死程序
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;
}
}
检测蓝牙串口信息函数,用于处理蓝牙数据
void check_Serial() {
if (BLE_Serial.available()) {
if (BLE_Serial.read() == '5') {
Serial.println("BLE Received Opening!");
opening();
}
else {
Serial.println("BLE Received RebootWiFi!");
}
}
}
自动校验函数,密码到达一定长度自动校对
void automated_proof() {
if (scan.length() == 4) {
MsTimer2::stop();
check();
}
}
蓝牙软件可以使用任何可以发ascii码的,我使用官方软件,具体可以去谷歌商店下载 代码写的挺好的
建议楼主可以考虑使用blinker进行蓝牙接入
话说这个锁有照片视频参考一下吗?
页:
[1]