#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
const uint8_t rows = 4;
const uint8_t cols = 4;
const uint8_t I2C_addr = 0x27;
const uint8_t LCD_cols = 16;
const uint8_t LCD_rows = 2;
char keys[rows][cols] = {
{ '1', '2', '3', 'l' },
{ '4', '5', '6', 'u' },
{ '7', '8', '9', ' ' },
{ 'e', '0', 'b', ' ' }
};
uint8_t Lock[8] = {
0b01110,
0b10001,
0b10001,
0b11111,
0b11011,
0b11011,
0b11111,
0b00000,
};
uint8_t Unlock[8] = {
0b01110,
0b10000,
0b10000,
0b11111,
0b11011,
0b11011,
0b11111,
0b00000,
};
uint8_t colPins[cols] = {6, 7, 8, 9}; // Pins connected to C1, C2, C3, C4
uint8_t rowPins[rows] = {2, 3, 4, 5}; // Pins connected to R1, R2, R3, R4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);
LiquidCrystal_I2C lcd(I2C_addr, LCD_cols, LCD_rows);
Servo lock;
int code = 0;
void setup() {
lcd.init();
lcd.backlight();
lock.attach(10);
lock.write(0);
String s = "Arduino lock",
s2 = "v1.0";
lcd.setCursor(2, 0);
for (byte i = 0; i < s.length(); i++) {
lcd.print(s[i]);
delay(100);
}
lcd.setCursor(6, 1);
for (byte i = 0; i < s2.length(); i++) {
lcd.print(s2[i]);
delay(100);
}
delay(500);
lcd.createChar(1, Unlock);
lcd.createChar(2, Lock);
lcd.clear();
}
void loop() {
lcd.setCursor(0, 0);
lcd.print("\x01 unlocked \x01");
lcd.setCursor(3, 1);
lcd.print("\x02 to lock");
while (keypad.getKey() != 'l');
if (code == 0) {
lcd.clear();
while (true) {
lcd.setCursor(0, 0);
lcd.print("Enter a new code");
while (code / 10000 == 0) {
lcd.setCursor(5, 1);
int code2 = code, len = 0;
while (code2 > 0) {
lcd.print("*");
code2 /= 10;
len++;
}
for (int i = 0; i < (5 - len); i++) {
lcd.print("_");
}
char key;
while (true) {
do {
key = keypad.getKey();
} while (key == NO_KEY);
bool flag = false;
for (int i = '0'; i <= '9'; i++) {
if (key == i) {
flag = true;
break;
}
}
if (flag) break;
}
code *= 10;
code += key - '0';
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Confirm the code");
int input = 0;
while (input / 10000 == 0) {
lcd.setCursor(5, 1);
int code2 = input, len = 0;
while (code2 > 0) {
lcd.print("*");
code2 /= 10;
len++;
}
for (int i = 0; i < (5 - len); i++) {
lcd.print("_");
}
char key;
while (true) {
do {
key = keypad.getKey();
} while (key == NO_KEY);
bool flag = false;
for (int i = '0'; i <= '9'; i++) {
if (key == i) {
flag = true;
break;
}
}
if (flag) break;
}
input *= 10;
input += key - '0';
}
if (input != code) {
int l = 0;
while (l < 10) {
lcd.setCursor(3, 1);
for (int i = 0; i < l; i++) {
lcd.print("=");
}
for (int i = l; i < 10; i++) {
lcd.print(".");
}
delay(100);
l++;
}
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Code mismatch");
lcd.setCursor(0, 1);
lcd.print("Safe not locked!");
code = 0;
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("\x01 unlocked \x01");
lcd.setCursor(3, 1);
lcd.print("\x02 to lock");
while (keypad.getKey() != 'l');
lcd.clear();
}
else {
break;
}
}
}
int l = 0;
while (l < 10) {
lcd.setCursor(3, 1);
for (int i = 0; i < l; i++) {
lcd.print("=");
}
for (int i = l; i < 10; i++) {
lcd.print(".");
}
delay(100);
l++;
}
lock.write(90);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("\x02 locked \x02");
while (true) {
int input = 0;
while (input / 10000 == 0) {
lcd.setCursor(5, 1);
int code2 = input, len = 0;
while (code2 > 0) {
lcd.print("*");
code2 /= 10;
len++;
}
for (int i = 0; i < (5 - len); i++) {
lcd.print("_");
}
char key;
while (true) {
do {
key = keypad.getKey();
} while (key == NO_KEY);
bool flag = false;
for (int i = '0'; i <= '9'; i++) {
if (key == i) {
flag = true;
break;
}
}
if (flag) break;
}
input *= 10;
input += key - '0';
}
l = 0;
while (l < 10) {
lcd.setCursor(3, 1);
for (int i = 0; i < l; i++) {
lcd.print("=");
}
for (int i = l; i < 10; i++) {
lcd.print(".");
}
delay(100);
l++;
}
if (input == code) {
lcd.clear();
break;
}
else {
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("code wrong!");
l = 0;
while (l < 10) {
lcd.setCursor(3, 1);
for (int i = 0; i < l; i++) {
lcd.print("=");
}
for (int i = l; i < 10; i++) {
lcd.print(".");
}
delay(1000);
l++;
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("\x02 locked \x02");
}
}
}