我有一个智能门锁的Arduino功能,但问题是密码取错了,应该取4个输入作为密码。
假设我是这样输入的:
input: 1
output: Password: 1 1
input: 2
output: Password 1 2112
input: 3
output: Password is Long
这就是LCD屏幕上显示的内容。
这是代码:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include <SPI.h>
#include <MFRC522.h>
#include <Keypad.h>
#define SS_PIN 10
#define RST_PIN 9
String UID1 = "A3 14 95 1A"; // First UID
String UID2 = "BC 71 16 4A"; // Second UID
byte lock = 0;
Servo servo;
MFRC522 rfid(SS_PIN, RST_PIN);
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] = {8, 7, 6, 5};
byte colPins[COLS] = {4, 3, 2, A1};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
char password[] = "1234";
char enteredPassword[5] = "";
int enteredPasswordIndex = 0;
bool enteringPassword = false;
unsigned long unlockedTime = 0;
const unsigned long lockDelay = 5000;
// Initialize the LCD screen with the I2C address
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
Serial.begin(9600);
servo.attach(A2);
servo.write(0);
SPI.begin();
rfid.PCD_Init();
lcd.init();
delay(5);
lcd.backlight();
lcd.setCursor(2, 0);
lcd.print("Put your Card");
lcd.setCursor(2, 1);
lcd.print("or Password :)");
delay(3000); // Wait for 5 seconds
lcd.clear();
}
void loop() {
rfidControl();
keypadControl();
}
void DoorReLock(){
lcd.print("Door will lock itself in 5 seconds: ");
for (int i = 5; i > 0; i--) {
lcd.clear();
lcd.print("Door will lock itself in ");
lcd.setCursor(2,1);
lcd.print(String(i) + " seconds");
delay(1000);
}
lcd.clear();
lcd.print("Locking door...");
servo.write(0);
}
void rfidControl() {
if (!rfid.PICC_IsNewCardPresent())
return;
if (!rfid.PICC_ReadCardSerial())
return;
Serial.print("NUID tag is: ");
String ID = "";
for (byte i = 0; i < rfid.uid.size; i++) {
ID.concat(String(rfid.uid.uidByte[i] < 0x10 ? " 0" : " "));
ID.concat(String(rfid.uid.uidByte[i], HEX));
}
ID.toUpperCase();
if ((ID.substring(1) == UID2 || ID.substring(1) == UID1) && lock == 0) {
servo.write(90);
Serial.println("Door is locked");
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("Door is open");
delay(1500);
lock = 1;
DoorReLock();
} else if ((ID.substring(1) == UID2 || ID.substring(1) == UID1) && lock == 1) {
servo.write(0);
Serial.println("Door is open");
lcd.clear();
lcd.setCursor(3, 0);
DoorReLock();
lock = 0;
} else {
Serial.println("Wrong card!");
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("Wrong card!");
delay(1500);
}
}
void keypadControl() {
char key = keypad.getKey();
if (key != '\0') {
Serial.print("Key pressed: ");
Serial.println(key);
// Append pressed key to entered password
if (enteredPasswordIndex < sizeof(enteredPassword) - 1) {
enteredPassword[enteredPasswordIndex] = key;
enteredPasswordIndex++;
}
// Print entered password on LCD screen
lcd.setCursor(3, 0);
lcd.print("Password: ");
lcd.setCursor(4, 1);
lcd.print(enteredPassword);
// Check if DCB key combination is entered
if (key == 'D' && enteredPasswordIndex == 3 && enteredPassword[0] == 'D' && enteredPassword[1] == 'C' && enteredPassword[2] == 'B') {
// Ask for old password
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("Enter old password");
char oldPassword[sizeof(password)] = "";
int oldPasswordIndex = 0;
bool enteringOldPassword = true;
while (enteringOldPassword) {
char oldKey = keypad.getKey();
if (oldKey != '\0' && oldKey != '#' && oldKey != '*' && oldKey != 'A' && oldKey != 'B' && oldKey != 'C' && oldKey != 'D') {
oldPassword[oldPasswordIndex] = oldKey;
oldPasswordIndex++;
lcd.setCursor(oldPasswordIndex - 1, 1);
lcd.print('*');
}
if (oldPasswordIndex == sizeof(password) - 1) {
enteringOldPassword = false;
}
}
if (strcmp(oldPassword, password) == 0) {
// Ask for new password
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter new password");
char newPassword[sizeof(password)] = "";
int newPasswordIndex = 0;
bool enteringNewPassword = true;
while (enteringNewPassword) {
char newKey = keypad.getKey();
if (newKey != '\0' && newKey != '#' && newKey != '*' && newKey != 'A' && newKey != 'B' && newKey != 'C' && newKey != 'D') {
newPassword[newPasswordIndex] = newKey;
newPasswordIndex++;
lcd.setCursor(newPasswordIndex - 1, 1);
lcd.print('*');
}
if (newPasswordIndex == sizeof(password) - 1) {
enteringNewPassword = false;
}
}
// Update password and notify user
strncpy(password, newPassword, sizeof(password));
password[sizeof(password) - 1] = '\0';
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("Password changed!");
delay(1500);
lcd.clear();
} else {
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("Wrong password");
delay(1500);
lcd.clear();
}
// Reset entered password
enteringPassword = false;
memset(enteredPassword, 0, sizeof(enteredPassword));
enteredPasswordIndex = 0;
} else if (key == '#') {
Serial.println("Password is reset");
memset(enteredPassword, 0, sizeof(enteredPassword));
enteredPasswordIndex = 0;
enteringPassword = false;
unlockedTime = 0;
lcd.print("Password is reset");
lcd.clear();
lcd.setCursor(3, 0);
delay(500); // Wait for 0.5 seconds
lcd.clear();
} else if (key == 'A') {
Serial.println("Door is Locked");
servo.write(0);
memset(enteredPassword, 0, sizeof(enteredPassword));
enteredPasswordIndex = 0;
enteringPassword = false;
unlockedTime = 0;
lcd.print("Door is Locked");
lcd.clear();
lcd.setCursor(3, 0);
delay(500); // Wait for 0.5 seconds
lcd.clear();
} else if (key == '*') {
Serial.println("Submit");
if (strcmp(enteredPassword, password) == 0) {
servo.write(90);
Serial.println("Door unlocked");
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("Door unlocked");
unlockedTime = millis();
DoorReLock();
} else {
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("Wrong password");
delay(1000); // Wait for 1 second
lcd.clear();
}
enteringPassword = false;
memset(enteredPassword, 0, sizeof(enteredPassword));
enteredPasswordIndex = 0;
delay(1500); // Wait for 1.5 seconds
} else if (enteringPassword) {
if (enteredPasswordIndex >= sizeof(enteredPassword) - 1) {
Serial.println("Password too long");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Password is long");
delay(1500); // Wait for 1.5 seconds
lcd.clear();
memset(enteredPassword, 0, sizeof(enteredPassword));
} else {
enteredPassword[enteredPasswordIndex] = key;
enteredPasswordIndex++;
Serial.print("Entered password: ");
Serial.println(enteredPassword);
lcd.setCursor(enteredPasswordIndex - 1, 1);
lcd.print(key);
}
} else {
Serial.println("Enter password");
enteringPassword = true;
enteredPassword[0] = key;
enteredPasswordIndex = 1;
// lcd.setCursor(0, 1);
// lcd.print("Password: ");
}
// If the door is unlocked and the lock delay has passed
if (unlockedTime != 0 && millis() - unlockedTime >= lockDelay) {
servo.write(0);
Serial.println("Door locked");
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("Door locked");
unlockedTime = 0;
}
}
}
我认为密码更改是问题所在,因为添加后出现了问题,但我完全删除了它,它仍然是一样的,所以我可能在试图将其打印到LCD上时做错了什么,但我似乎找不到它。