#include <LiquidCrystal.h>
#include <Keypad.h>
#include <Servo.h>
//iconLocked
uint8_t iconLocked[8] = {
0b01110,
0b10001,
0b10001,
0b11111,
0b11011,
0b11011,
0b11111,
};
//iconunLocked
uint8_t iconUnlocked[8] = {
0b01110,
0b10000,
0b10000,
0b11111,
0b11011,
0b11011,
0b11111,
};
#define SERVO_LOCK_POS 20
#define SERVO_UNLOCK_POS 90
Servo lockServo;
LiquidCrystal lcd(A0,A1,A2,A3,A4,A5);
int pos = 0;
String data = "1234"; //开锁密码
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
uint8_t colPins[COLS] = { 4, 5, 6, 7 }; // Pins connected to C1, C2, C3, C4
uint8_t rowPins[ROWS] = { 0, 1, 2, 3 }; // Pins connected to R1, R2, R3, R4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
//上锁图标
void showiconLocked(){
lcd.createChar(1, iconLocked);
lcd.setCursor(0,0);
lcd.print("\x01");
lcd.setCursor(5,0);
lcd.print("Locked");
lcd.setCursor(15,0);
lcd.print("\x01");
}
//解锁图标
void showiconUnlocked(){
lcd.createChar(1, iconUnlocked);
lcd.setCursor(0,0);
lcd.print("\x01");
lcd.setCursor(4,0);
lcd.print("Unlocked");
lcd.setCursor(15,0);
lcd.print("\x01");
}
void showScreen(){
lcd.setCursor(4,0);
lcd.print("Welcome");
lcd.setCursor(2,1);
String message ="Arduino Door";
for(byte i =0 ;i<message.length();i++){
lcd.print(message[i]);
delay(50);
}
delay(2000);
lcd.clear();
}
//上锁
void lock() {
lockServo.write(SERVO_LOCK_POS);
}
//开锁
void unlock() {
lockServo.write(SERVO_UNLOCK_POS);
}
void setup() {
lcd.begin(16,2);
pinMode(11, OUTPUT); //蜂鸣器
pinMode(12, OUTPUT);
lockServo.attach(9);
lock();
showScreen();
showiconLocked();
}
void loop() {
char key = keypad.getKey();
if (key == '#'){
digitalWrite(12,LOW);
lcd.clear();
lcd.setCursor(2, 1);
lcd.print("[..........]");
lcd.setCursor(3, 1);
for (byte i = 0; i < 10; i++) {
delay(150);
lcd.print("=");
}
lcd.clear();
lcd.setCursor(3,0);
lcd.print("Enter Code");
lcd.setCursor(5, 1);
lcd.print("[____]");
lcd.setCursor(6, 1);
String result = "";
while (result.length() < 4) {
char key = keypad.getKey();
if (key >= '0' && key <= '9') {
lcd.print('*');
result += key;
}
}
if(result.equals(data)){
lcd.clear();
delay(15);
showiconUnlocked();
unlock();
digitalWrite(12,HIGH);
delay(15);
}
else{
lcd.clear();
lcd.setCursor(1,0);
lcd.print("Wrong Password");
lcd.setCursor(2,1);
lcd.print("Please Again!");
}
}
}