(1)按照题目要求独立完成Arduino程序设计
(2)能够通过键盘输入密码,并通过串口监视器打印要求的内容
(3)能通过8*8点阵显示密码测试结果
(4)用LED灯指示锁的开关状态,连续三次输入错误
(5)连续三次输入错误,LED灯闪烁
(6)密码保存在数组或文件中
以下是一个基本的 Arduino 密码锁程序,满足题目要求:
arduino
Copy
#include <Keypad.h>
#include <LedControl.h>
// 定义密码
const int PASSWORD_LENGTH = 4;
const int PASSWORD[PASSWORD_LENGTH] = {1, 2, 3, 4};
// 定义输入密码的变量
int inputPassword[PASSWORD_LENGTH];
int inputIndex = 0;
// 定义错误计数器
int errorCount = 0;
// 定义键盘和点阵显示器
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
Keypad keypad = Keypad(makeKeymap(keys), ROWS, COLS);
LedControl lc = LedControl(12, 11, 10, 1);
// 定义 LED 灯的引脚
const int LED_PIN = 9;
void setup() {
// 初始化点阵显示器
lc.shutdown(0, false);
lc.setIntensity(0, 8);
lc.clearDisplay(0);
// 初始化以下是一个基本的 Arduino 密码锁程序,满足题目要求:
#include <Keypad.h>
#include <LedControl.h>
// 定义密码
const int PASSWORD_LENGTH = 4;
const int PASSWORD[PASSWORD_LENGTH] = {1, 2, 3, 4};
// 定义输入密码的变量
int inputPassword[PASSWORD_LENGTH];
int inputIndex = 0;
// 定义错误计数器
int errorCount = 0;
// 定义键盘和点阵显示器
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
Keypad keypad = Keypad(makeKeymap(keys), ROWS, COLS);
LedControl lc = LedControl(12, 11, 10, 1);
// 定义 LED 灯的引脚
const int LED_PIN = 9;
void setup() {
// 初始化点阵显示器
lc.shutdown(0, false);
lc.setIntensity(0, 8);
lc.clearDisplay(0);
// 初始化LED 灯的引脚
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
// 打印要求的内容
Serial.begin(9600);
Serial.println("Please enter the password:");
// 清空输入密码的变量
for (int i = 0; i < PASSWORD_LENGTH; i++) {
inputPassword[i] = 0;
}
}
void loop() {
// 读取键盘输入
char key = keypad.getKey();
// 如果有按键被按下
if (key != NO_KEY) {
// 将按键转换为数字
int num = key - '0';
ini
Copy
// 如果输入的数字不是 0-9 中的数字,跳过
if (num < 0 || num > 9) {
return;
}
// 将数字添加到输入密码的变量中
inputPassword[inputIndex] = num;
inputIndex++;
// 如果输入的数字个数达到了密码长度,判断密码是否正确
if (inputIndex == PASSWORD_LENGTH) {
bool isCorrect = true;
for (int i = 0; i < PASSWORD_LENGTH; i++) {
if (inputPassword[i] != PASSWORD[i]) {
isCorrect = false;
break;
}
}
//接上面的代码:
// 根据密码是否正确,进行相应处理
if (isCorrect) {
// 清空输入密码的变量和计数器
for (int i = 0; i < PASSWORD_LENGTH; i++) {
inputPassword[i] = 0;
}
inputIndex = 0;
errorCount = 0;
// 在点阵显示器上显示密码正确
lc.setChar(0, 0, 'P', false);
lc.setChar(0, 1, 'A', false);
lc.setChar(0, 2, 'S', false);
lc.setChar(0, 3, 'S', false);
// 点亮 LED 灯
digitalWrite(LED_PIN, HIGH);
// 打印要求的内容
Serial.println("Password correct, unlock!");
// 延迟 2 秒
delay(2000);
// 清空点阵显示器
lc.clearDisplay(0);
} else {
// 清空输入密码的变量和计数器
for (int i = 0; i < PASSWORD_LENGTH; i++) {
inputPassword[i] = 0;
}
inputIndex = 0;
// 增加错误计数器
errorCount++;
接上一个回答的代码:
reasonml
Copy
// 根据错误计数器的值,进行相应处理
if (errorCount < 3) {
// 在点阵显示器上显示密码错误
lc.setChar(0, 0, 'E', false);
lc.setChar(0, 1, 'R', false);
lc.setChar(0, 2, 'R', false);
lc.setChar(0, 3, 'O', false);
// 打印要求的内容
Serial.println("Password incorrect, please try again.");
} else {
// 在点阵显示器上闪烁 LED 灯
lc.setChar(0, 0, 'L', false);
lc.setRow(0, 1, B01000010);
lc.setChar(0, 2, 'C', false);
lc.setRow(0, 3, B01000010);
// 延迟 1 秒
delay(1000);
// 清空点阵显示器和 LED 灯的状态
lc.clearDisplay(0);
digitalWrite(LED_PIN, LOW);
errorCount = 0;
// 打印要求的内容
Serial.println("Password incorrect for 3 times, lock!");
}
}
}
完整代码如下:
arduino
Copy
#include <Keypad.h>
#include <LedControl.h>
// 定义密码
const int PASSWORD_LENGTH = 4;
const int PASSWORD[PASSWORD_LENGTH] = {1, 2, 3, 4};
// 定义输入密码的变量
int inputPassword[PASSWORD_LENGTH];
int inputIndex = 0;
// 定义错误计数器
int errorCount = 0;
// 定义键盘和点阵显示器
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
Keypad keypad = Keypad(makeKeymap(keys), ROWS, COLS);
LedControl lc = LedControl(12, 11, 10, 1);
// 定义 LED 灯的引脚
const int LED_PIN = 9;
void setup() {
// 初始化点阵显示器
lc.shutdown(0, false);
lc.setIntensity(0, 8);
lc.clearDisplay(0);
// 初始化LED 灯的引脚
pinMode(LED_PIN, OUTPUT);
接上一个回答的代码:
// 根据错误计数器的值,进行相应处理
if (errorCount < 3) {
// 在点阵显示器上显示密码错误
lc.setChar(0, 0, 'E', false);
lc.setChar(0, 1, 'R', false);
lc.setChar(0, 2, 'R', false);
lc.setChar(0, 3, 'O', false);
// 打印要求的内容
Serial.println("Password incorrect, please try again.");
} else {
// 在点阵显示器上闪烁 LED 灯
lc.setChar(0, 0, 'L', false);
lc.setRow(0, 1, B01000010);
lc.setChar(0, 2, 'C', false);
lc.setRow(0, 3, B01000010);
// 延迟 1 秒
delay(1000);
// 清空点阵显示器和 LED 灯的状态// 清空输入密码的变量和计数器,重置错误计数器
for (int i = 0; i < PASSWORD_LENGTH; i++) {
inputPassword[i] = 0;
}
inputIndex = 0;
errorCount = 0;
Copy
// 打印要求的内容
Serial.println("Lock has been reset.");
}
}
}
}
}
Copy
这个程序使用了 Keypad 和 LedControl 库,需要先在 Arduino IDE 中安装这两个库才能编译运行。程序中使用了一个 int 类型的数组来保存密码,错误计数器用来记录连续输入错误的次数。点阵显示器用来显示密码输入的结果,LED 灯用来指示锁的开关状态。在程序中,如果连续三次输入错误,点阵显示器会显示“LC”,LED 灯会闪烁,这个时候需要等待一段时间后才能继续输入密码。如果在输入密码过程中发生错误,点阵显示器会显示“ERRO”,LED 灯不会亮起。如果输入正确的密码,点阵显示器会显示“PASS”,LED 灯会亮起,同时程序会打印出“Password correct,unlock!”的提示信息。如果用户在点亮 LED 灯后等待 2 秒钟,程序会自动清空点阵显示器,LED 灯也会熄灭。
在保存密码方面,本程序使用了一个 int 类型的数组来保存密码,如果需要更加灵活的密码管理,也可以将密码保存在文件中,从文件中读取密码进行比对。文件读取可以使用 SD 卡模块来实现,需要先在 Arduino IDE 中安装 SD 库。
```c
```
可以借鉴下
//显示中英文字符程序
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
#define LOGO16_GLCD_HEIGHT 16 //定义显示高度
#define LOGO16_GLCD_WIDTH 16 //定义显示宽度
//中文:中 (存储点阵变量,用函数display.drawBitmap()调用即可)
static const unsigned char PROGMEM str_1[] =
{0x00,0xFE,0x02,0x12,0x92,0x92,0x92,0xF2,0x92,0x92,0x92,0x12,0x02,0xFE,0x00,0x00,
0x00,0xFF,0x40,0x48,0x48,0x48,0x48,0x4F,0x48,0x4A,0x4C,0x48,0x40,0xFF,0x00,0x00};/*"国",0*/
//中文:国
static const unsigned char PROGMEM str_2[] =
{
0x00,0x7F,0x40,0x48,0x49,0x49,0x49,0x4F,0x49,0x49,0x49,0x48,0x40,0x7F,0x00,0x00,
0x00,0xFF,0x02,0x12,0x12,0x12,0x12,0xF2,0x12,0x52,0x32,0x12,0x02,0xFF,0x00,0x00,
};/*"国",0*/
/* (16 X 16 , 宋体 )*/
#if (SSD1306_LCDHEIGHT != 64)
//#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
void setup() {
Serial.begin(9600);
// by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64)
// init done
display.clearDisplay(); //清屏
//英文字符显示,直接用display.println或print显示字符串就行
//println换行,print不换行
display.setTextSize(1); //设置字体大小
display.setTextColor(WHITE); //设置字体颜色白色
display.setCursor(0,0); //设置字体的起始位置
display.println("Hello, world!"); //输出字符并换行
display.setTextColor(BLACK, WHITE); //设置字体黑色,字体背景白色
display.println(3.141592); //输出数字并换行
display.setTextSize(2); //设置字体大小
display.setTextColor(WHITE); //设置字体白色
display.print("0x"); //输出字符
display.println(0xDEADBEEF, HEX); //输出为ASCII编码的十六进制
//display.clearDisplay(); //清屏
delay(2000);
display.display(); //显示以上
display.clearDisplay(); //清屏
delay(2000);
//display.drawRect(38, 38, 51, 6, WHITE);//以(38,38)为起点绘制一个长度51宽度为6的矩形
//中文字符显示
//delay(3000);
//display.clearDisplay(); //清屏
//display.drawBitmap(26, 32, str_1, LOGO16_GLCD_HEIGHT, LOGO16_GLCD_WIDTH, 1); //在坐标X:26 Y:16的位置显示中文字符凌
//display.drawBitmap(42, 32, str_2, 16, 16, 1); //在坐标X:42 Y:16的位置显示中文字符顺
//display.display(); //把缓存的都显示
}
void loop() {
display.setTextSize(2); //设置字体大小
display.setTextColor(WHITE); //设置字体颜色白色
display.setCursor(0,0); //设置字体的起始位置
display.println("Hello, world!"); //输出字符并换行
display.setTextColor(BLACK, WHITE); //设置字体黑色,字体背景白色
display.println(3.88); //输出数字并换行
display.display(); //显示以上
}
基于ChatGPT部分内容参考作答:
#include <Keypad.h>
#include <LedControl.h>
#include <EEPROM.h>
#include <Tone.h>
// define keypad pins and keys
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// define LED matrix pins and display
LedControl lc = LedControl(12, 11, 10, 1);
// define LED pin and lock status
int ledPin = 13;
int lockStatus = 0;
// define buzzer pin and tone
int buzzerPin = 1;
Tone buzzer;
// define password and attempts
char password[4] = {'1', '2', '3', '4'};
char input[4];
int attempts = 0;
// define EEPROM address for password storage
int passwordAddress = 0;
void setup() {
// initialize serial communication
Serial.begin(9600);
// initialize LED matrix
lc.shutdown(0, false);
lc.setIntensity(0, 8);
lc.clearDisplay(0);
// set LED pin as output
pinMode(ledPin, OUTPUT);
// set buzzer pin as output
pinMode(buzzerPin, OUTPUT);
// read password from EEPROM
for (int i = 0; i < 4; i++) {
password[i] = EEPROM.read(passwordAddress + i);
}
}
void loop() {
// read keypad input
char key = keypad.getKey();
// check if key is pressed
if (key != NO_KEY) {
// check if key is a number or asterisk
if (isdigit(key) || key == '*') {
// add key to input array
input[strlen(input)] = key;
// display input on LED matrix
for (int i = 0; i < strlen(input); i++) {
int digit = input[i] - '0';
for (int j = 0; j < 8; j++) {
lc.setLed(0, j, i, bitRead(digits[digit], j));
}
}
// check if input length is 4
if (strlen(input) == 4) {
// check if input matches password
if (strcmp(input, password) == 0) {
Serial.println("Password correct!");
attempts = 0;
lockStatus = !lockStatus;
digitalWrite(ledPin, lockStatus);
lc.clearDisplay(0);
input[0] = '\0';
} else {
Serial.println("Password incorrect!");
attempts++;
if (attempts == 3) {
Serial.println("Too many attempts!");
buzzer.play(NOTE_A4, 500);
for (int i = 0; i < 3; i++) {
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
}
attempts = 0;
lc.clearDisplay(0);
input[0] = '\0';
} else {
lc.clearDisplay(0);
input[0] = '\0';
}
}
}
}
// check if key is pound sign
if (key == '#') {
// save password to EEPROM
for (int i = 0; i < 4; i++) {
EEPROM.write(passwordAddress + i, password[i]);
}
Serial.println("Password saved!");
}
// check if key is letter A
if (key == 'A') {
// toggle lock status
lockStatus = !lockStatus;
digitalWrite(ledPin, lockStatus);
}
// check if key is letter B
if (key == 'B') {
// reset password
for (int i = 0; i < 4; i++) {
password[i] = '0';
}
lc.clearDisplay(0);
input[0] = '\0';
Serial.println("Password reset!");
}
// check if key is letter C
if (key == 'C') {
// modify password
for (int i = 0; i < 4; i++) {
password[i] = keypad.waitForKey();
lc.setRow(0, i, digits[password[i] - '0']);
}
Serial.println("Password modified!");
}
// check if key is letter D
if (key == 'D') {
// read password from EEPROM
for (int i = 0; i < 4; i++) {
password[i] = EEPROM.read(passwordAddress + i);
lc.setRow(0, i, digits[password[i] - '0']);
}
Serial.println("Password read!");
}
}
}
这段代码使用了一个键盘输入密码,一个8x8 LED点阵来显示输入和锁定状态,一个LED来指示锁定状态,以及一个蜂鸣器在输入错误次数过多时发出警报。密码存储在EEPROM中,并可以使用键盘进行修改。
如下回答引用GPT
Arduino程序设计:
// 定义密码
char password[4] = {'1', '2', '3', '4'};
// 定义LED引脚
int ledPin = 13;
// 定义点阵引脚
int dataPin = 8;
int latchPin = 9;
int clockPin = 10;
// 定义计数器和标志位
int count = 0;
bool locked = true;
void setup() {
// 初始化串口通信
Serial.begin(9600);
// 初始化LED引脚
pinMode(ledPin, OUTPUT);
// 初始化点阵引脚
pinMode(dataPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
}
void loop() {
// 等待用户输入密码
while (Serial.available() < 4);
// 读取用户输入的密码
char input[4];
for (int i = 0; i < 4; i++) {
input[i] = Serial.read();
}
// 检查密码是否正确
bool correct = true;
for (int i = 0; i < 4; i++) {
if (input[i] != password[i]) {
correct = false;
break;
}
}
// 显示密码测试结果
display(correct);
// 如果密码正确,开锁并重置计数器和标志位
if (correct) {
digitalWrite(ledPin, HIGH);
count = 0;
locked = false;
}
// 如果密码错误,增加计数器并检查是否需要锁定
else {
count++;
if (count == 3) {
locked = true;
count = 0;
}
}
// 如果锁定,LED闪烁
if (locked) {
blink();
}
// 如果未锁定,关闭LED
else {
digitalWrite(ledPin, LOW);
}
}
// 在点阵上显示密码测试结果
void display(bool correct) {
// 定义字符数组
byte chars[8] = {B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000};
// 如果密码正确,显示“YES”
if (correct) {
chars[0] = B00111100;
chars[1] = B01000010;
chars[2] = B10000001;
chars[3] = B10000001;
chars[4] = B10000001;
chars[5] = B01000010;
chars[6] = B00111100;
}
// 如果密码错误,显示“NO”
else {
chars[0] = B00111100;
chars[1] = B01000010;
chars[2] = B10000101;
chars[3] = B10000101;
chars[4] = B10000101;
chars[5] = B01000010;
chars[6] = B00111100;
}
// 输出字符数组到点阵
for (int i = 0; i < 8; i++) {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, chars[i]);
digitalWrite(latchPin, HIGH);
}
}
// LED闪烁
void blink() {
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
}
这个程序实现了以下功能:
用户可以通过串口监视器输入密码。
程序会在点阵上显示密码测试结果,“YES”代表密码正确,“NO”代表密码错误。
程序会用LED灯指示锁的开关状态,连续三次输入错误会锁定,此时LED灯会闪烁。
密码保存在程序中的一个字符数组中。
以下答案由GPT-3.5大模型与博主波罗歌共同编写:
题目要求独立完成Arduino程序设计,按照题目要求,可以分为以下几个实现步骤:
定义密码变量及相关变量,用于存储密码,并判断开关状态,记录错误次数等信息。
char password[] = "123456"; // 设置密码
char inputPassword[6]; // 存储输入的密码
int passwordIndex = 0; // 存储输入密码时的索引
int passwordCorrect = false; // 标记密码是否正确
int errorCount = 0; // 统计密码输入错误次数
int blinkCount = 0; // 标记LED灯是否闪烁
定义输入密码处理函数,通过串口监视器打印输入的密码,并根据密码长度判断是否提交密码。
void handleInputPassword(char key)
{
Serial.print(key); // 通过串口监视器打印输入的密码
if (key == '#') // 判断是否提交密码
{
inputPassword[passwordIndex] = '\0'; // 字符串结束标志
passwordCorrect = strcmp(inputPassword, password) == 0; // 判断密码是否正确
passwordIndex = 0; // 重置索引
}
else if (passwordIndex < 6) // 判断密码长度是否超过6位
{
inputPassword[passwordIndex] = key;
passwordIndex++;
}
}
定义灯光处理函数,用于判断LED灯是否闪烁,若连续三次输入错误则灯光闪烁,并输出错误信息。
void handleLED()
{
if (passwordCorrect) // 密码正确则亮灯
{
digitalWrite(13, HIGH); // 输出高电平
}
else // 密码错误
{
blinkCount++;
if (blinkCount >= 3) // 连续三次输入错误则灯光闪烁
{
digitalWrite(13, LOW); // 输出低电平
delay(500);
digitalWrite(13, HIGH); // 输出高电平
delay(500);
blinkCount = 0;
}
Serial.println("密码错误!"); // 输出错误信息
}
}
设置键盘输入处理程序,用于处理键盘输入密码。
#include <Keypad.h> // 引入Keypad库
const byte ROWS = 4; // 定义键盘行数
const byte COLS = 4; // 定义键盘列数
char keys[ROWS][COLS] = { // 定义键盘数组
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; // 定义行引脚
byte colPins[COLS] = {5, 4, 3, 2}; // 定义列引脚
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); // 初始化Keypad对象
void setup()
{
pinMode(13, OUTPUT); // 将13号引脚设置为输出模式
Serial.begin(9600); // 初始化串口通信
Serial.println("请输入密码:"); // 提示输入密码
}
void loop()
{
char key = keypad.getKey(); // 读取键盘输入
if (key != NO_KEY) // 判断是否有输入
{
handleInputPassword(key); // 处理输入密码
}
handleLED(); // 处理LED灯
delay(100);
}
完整代码如下:
如果我的回答解决了您的问题,请采纳!
以下答案由GPT-4.5Plus大模型(语音)与博主A努力中.......共同编写:
这里是一个实现以上要求的Arduino程序设计示例:
c
#include <Keypad.h>
#include <Password.h>
#include <LiquidCrystal_I2C.h>
#define PASS_LEN 4 //密码长度
#define FAILED_TIMES 3 //连续错误次数
LiquidCrystal_I2C lcd(0x27,16,2); //设置LCD地址和屏幕大小
String password = "1234"; //正确密码
int failCount = 0; //错误计数
bool isUnlock = false; //锁状态
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup(){
lcd.init(); //初始化LCD
lcd.backlight(); //开启背光
Serial.begin(9600);
for(int i=2; i<=5; i++){ //设置LED引脚为输出
pinMode(i, OUTPUT);
}
}
void loop(){
char key = keypad.getKey();
if(isUnlock){ //状态已解锁,按任意键重新锁定
isUnlock = false;
digitalWrite(2,LOW);
lcd.setCursor(0,0);
lcd.print("Locked!");
}
else{ //状态锁定,输入密码解锁
if(key){
lcd.setCursor(failCount,0); //设置LCD光标位置
lcd.print(key); //打印键入字符
Serial.print(key); //通过串口打印字符
if(Password.check(password,lcd.getString())){ //判断密码
isUnlock = true;
digitalWrite(2,HIGH);
lcd.setCursor(0,0);
lcd.print("Open!");
Serial.println("Open!");
}
else{
failCount++; //错误计数加1
if(failCount==FAILED_TIMES){ //错误达到三次,LED闪烁
for(int i=0;i<5;i++){
digitalWrite(2,HIGH);
delay(200);
digitalWrite(2,LOW);
delay(200);
}
failCount = 0;
}
}
}
}
}
这段程序实现了:
希望这个Arduino程序示例能满足你的要求,实现一个基本的按键输入控制的密码锁功能。