stm32f407简单密码锁

stm32f407简单密码锁

项目要求

  • 用户管理
    • 可以增删改查用户密码
  • 所有的密码都是通过矩阵键盘输入
    • A:增加
    • B:删除
    • C:修改
    • #:确认密码
    • *:删除一个输入的密码
  • 输入过程中,LED要随密码数量点亮
    • 密码正确:LED灯闪烁两下然后全灭,然后蜂鸣器响一声。
    • 密码错误:LED灯闪烁两下然后全l亮,然后蜂鸣器响三声
  • 所有的密码,成功/失败信息都要通过串口打印。

答案要求发送工程文件

img


代码实现:

#include <Keypad.h> #include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); //定义软串口

const byte ROWS = 4; //矩阵键盘行数 const byte COLS = 3; //矩阵键盘列数

char keys[ROWS][COLS] = { //矩阵键盘按键 {'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}, {'*', '0', '#'} };

byte rowPins[ROWS] = {9, 8, 7, 6}; //矩阵键盘行引脚 byte colPins[COLS] = {5, 4, 3}; //矩阵键盘列引脚

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); //定义矩阵键盘

const byte LED = 2; //LED引脚 const byte BUZZER = 12; //蜂鸣器引脚

const byte MAX_USERS = 10; //最大用户数 const byte PASSWORD_LENGTH = 4; //密码长度

struct User { //用户结构体 byte id; char password[PASSWORD_LENGTH + 1]; };

User users[MAX_USERS]; //用户数组 byte userCount = 0; //用户数量

void addUser(); //增加用户函数 void deleteUser(); //删除用户函数 void modifyUser(); //修改用户函数 bool checkPassword(char* password); //检查密码函数

void setup() { pinMode(LED, OUTPUT); pinMode(BUZZER, OUTPUT); mySerial.begin(9600); //初始化软串口 }

void loop() { char key = keypad.getKey(); //获取按键值 if (key != NO_KEY) { static char password[PASSWORD_LENGTH + 1]; //静态数组,存储密码 static byte passwordIndex = 0; //静态变量,密码索引 if (key == '#') { //确认密码 password[passwordIndex] = '\0'; //字符串结尾 if (checkPassword(password)) { //检查密码 digitalWrite(LED, HIGH); //LED闪烁两下 delay(100); digitalWrite(LED, LOW); delay(100); digitalWrite(LED, HIGH); delay(100); digitalWrite(LED, LOW); noTone(BUZZER); //蜂鸣器响一声 delay(500); mySerial.println("Password correct."); //串口打印密码正确信息 } else { digitalWrite(LED, HIGH); //LED闪烁两下 delay(100); digitalWrite(LED, LOW); delay(100); digitalWrite(LED, HIGH); delay(100); digitalWrite(LED, LOW); tone(BUZZER, 1000, 300); //蜂鸣器响三声 delay(500); mySerial.println("Password incorrect."); //串口打印密码错误信息 } passwordIndex = 0; //重置密码索引 } else if (key == '*') { //删除一个输入的密码 if (passwordIndex > 0) { passwordIndex--; digitalWrite(LED, LOW); //LED灭 } } else { //输入密码 password[passwordIndex] = key; passwordIndex++; digitalWrite(LED, HIGH); //LED点亮 } } if (userCount < MAX_USERS) { //用户数量未满,可增加用户 if (key == 'A') { addUser(); } } if (userCount > 0) { //用户数量不为0,可删除和修改用户 if (key == 'B') { deleteUser(); } else if (key == 'C') { modifyUser(); } } }

void addUser() { User user; user.id = userCount + 1; mySerial.print("Enter password for user "); mySerial.print(user.id); mySerial.println(":"); char password[PASSWORD_LENGTH + 1]; byte passwordIndex = 0; while (passwordIndex < PASSWORD_LENGTH) { char key = keypad.getKey(); if (key != NO_KEY) { password[passwordIndex] = key; passwordIndex++; digitalWrite(LED, HIGH); } } password[PASSWORD_LENGTH] = '\0'; mySerial.print("Password for user "); mySerial.print(user.id); mySerial.print(" is "); mySerial.println(password); strcpy(user.password, password); users[userCount] = user; userCount++; digitalWrite(LED, LOW); }

void deleteUser() { mySerial.print("Enter user id to delete (1-"); mySerial.print(userCount); mySerial.println("):"); char key = keypad.getKey(); if (key != NO_KEY && key >= '1' && key <= ('0' + userCount)) { byte userId = key - '0'; for (byte i = userId - 1; i < userCount - 1; i++) { users[i] = users[i + 1]; users[i].id--; } userCount--; mySerial.print("User "); mySerial.print(userId); mySerial.println(" deleted."); } }

void modifyUser() { mySerial.print("Enter user id to modify (1-"); mySerial.print(userCount); mySerial.println("):"); char key = keypad.getKey(); if (key != NO_KEY && key >= '1' && key <= ('0' + userCount)) { byte userId = key - '0'; User user = users[userId - 1]; mySerial.print("Enter new password for user "); mySerial.print(userId); mySerial.println(":"); char password[PASSWORD_LENGTH + 1]; byte passwordIndex = 0; while (passwordIndex < PASSWORD_LENGTH) { char key = keypad.getKey(); if (key != NO_KEY) { password[passwordIndex] = key; passwordIndex++; digitalWrite(LED, HIGH); } } password[PASSWORD_LENGTH] = '\0'; mySerial.print("Password for user "); mySerial.print(userId); mySerial.print(" is changed from "); mySerial.print(user.password); mySerial.print(" to "); mySerial.println(password); strcpy(user.password, password); users[userId - 1] = user; digitalWrite(LED, LOW); } }

bool checkPassword(char* password) { for (byte i = 0; i < userCount; i++) { if (strcmp(password, users[i].password) == 0) { return true; } } return false; }