//////////////////////////////////////////////////////////////////////////////////////////////
/*
與AI協作
功能說明 :
1. 接收並儲存使用者輸入,按 # 比對預設密碼(如 "1234")。
2. OLED顯示輸入狀態、提示文字。
3. 更改密碼模式
使用 A 鍵可進入「更改密碼模式」
輸入格式為:「舊密碼」 + # → 「新密碼」 + #
*/
#include <Keypad.h>
#include <ESP32Servo.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// OLED Setup
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Lock control
Servo lockServo;
const int servoPin = 18;
// Password control
String password = "1234";
String input = "";
String tempOld = "";
String tempNew = "";
bool isChangingPassword = false;
bool waitingNewPassword = false;
// Keypad Setup
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] = {13, 12, 14, 27};
byte colPins[COLS] = {26, 25, 33, 32};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
Serial.begin(115200);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("OLED init failed");
while (true);
}
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
displayMessage("System Ready");
lockServo.attach(servoPin);
lockServo.write(0); // Locked
}
void loop() {
char key = keypad.getKey();
if (key) {
if (key == '*') {
input = "";
displayMessage("Input cleared");
}
else if (key == '#') {
processInput();
input = "";
}
else if (key == 'A') {
isChangingPassword = true;
waitingNewPassword = false;
input = "";
displayMessage("Enter old code:");
}
else {
input += key;
displayInputMasked();
}
}
}
void processInput() {
if (isChangingPassword) {
if (!waitingNewPassword) {
if (input == password) {
displayMessage("Correct. New code:");
waitingNewPassword = true;
} else {
displayMessage("Wrong password");
isChangingPassword = false;
}
} else {
password = input;
displayMessage("Password updated");
isChangingPassword = false;
waitingNewPassword = false;
}
} else {
if (input == password) {
displayMessage("Access Granted");
unlock();
} else {
displayMessage("Access Denied");
lock();
}
}
}
void unlock() {
lockServo.write(90);
delay(5000);
lock();
}
void lock() {
lockServo.write(0);
displayMessage("Locked");
delay(1000);
displayMessage("Enter code");
}
void displayMessage(String msg) {
display.clearDisplay();
display.setCursor(0, 0);
display.println(msg);
display.display();
}
void displayInputMasked() {
display.clearDisplay();
display.setCursor(0, 0);
if (isChangingPassword) {
display.println(waitingNewPassword ? "New:" : "Old:");
} else {
display.println("Code:");
}
for (int i = 0; i < input.length(); i++) {
display.print("*");
}
display.display();
}
//////////////////////////////////////////////////////////////////////////////////////////////
/*
與AI協作
功能說明 :
1. 接收並儲存使用者輸入,按 # 比對預設密碼(如 "1234")。
2. OLED顯示輸入狀態、提示文字。
*/
/*
#include <Keypad.h>
#include <ESP32Servo.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// OLED Setup
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Password
const String password = "1234";
String inputPassword = "";
// Servo setup
Servo lockServo;
const int servoPin = 18;
// Keypad setup
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] = {13, 12, 14, 27};
byte colPins[COLS] = {26, 25, 33, 32};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
Serial.begin(115200);
// OLED init
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Try 0x3D if needed
Serial.println("OLED failed");
while (true);
}
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
displayMessage("System Ready");
// Servo init
lockServo.attach(servoPin);
lockServo.write(0); // Locked
}
void loop() {
char key = keypad.getKey();
if (key) {
if (key == '#') {
if (inputPassword == password) {
displayMessage("Access Granted");
unlock();
} else {
displayMessage("Access Denied");
lock();
}
inputPassword = "";
} else if (key == '*') {
inputPassword = "";
displayMessage("Cleared");
} else {
inputPassword += key;
displayInputMasked();
}
}
}
void unlock() {
lockServo.write(90); // Unlock
delay(5000); // Hold for 5 sec
lock();
}
void lock() {
lockServo.write(0); // Lock
displayMessage("Locked");
delay(1000);
displayMessage("Enter Code");
}
void displayMessage(String msg) {
display.clearDisplay();
display.setCursor(0, 0);
display.println(msg);
display.display();
}
void displayInputMasked() {
display.clearDisplay();
display.setCursor(0, 0);
display.println("Code:");
for (int i = 0; i < inputPassword.length(); i++) {
display.print("*");
}
display.display();
}
*/
//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
/*
與AI協作
功能說明 : 接收並儲存使用者輸入,按 # 比對預設密碼(如 "1234")。
*/
/*
#include <Keypad.h> // 鍵盤函式庫
#include <ESP32Servo.h> // 伺服馬達函式庫
// 定義密碼
const String password = "1234"; // 預設密碼
String inputPassword = ""; // 用來接收輸入
// 設定伺服馬達
Servo lockServo;
const int servoPin = 18; // 伺服馬達接腳
// 鍵盤設定
const byte ROWS = 4; // 4 行
const byte COLS = 4; // 4 列
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {13, 12, 14, 27}; // 根據你的接線調整
byte colPins[COLS] = {26, 25, 33, 32}; // 根據你的接線調整
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
Serial.begin(115200);
lockServo.attach(servoPin);
lockServo.write(0); // 初始為關鎖
Serial.println("電子鎖啟動,請輸入密碼:");
}
void loop() {
char key = keypad.getKey();
if (key) {
Serial.print("*"); // 顯示輸入遮蔽
if (key == '#') {
// 使用者按下 # 表示輸入完畢
if (inputPassword == password) {
Serial.println("\n密碼正確,開鎖!");
unlock();
} else {
Serial.println("\n密碼錯誤!");
lock();
}
inputPassword = ""; // 清除輸入
} else if (key == '*') {
// 按 * 清除輸入
inputPassword = "";
Serial.println("\n輸入已清除");
} else {
// 累加密碼
inputPassword += key;
}
}
}
void unlock() {
lockServo.write(90); // 開鎖角度(依你的硬體調整)
delay(5000); // 開啟 5 秒
lock();
}
void lock() {
lockServo.write(0); // 關鎖角度
}
*/
//////////////////////////////////////////////////////////////////////////////////////////////