#include <Wire.h> // библиотека для протокола IIC
#include <LiquidCrystal_I2C.h> // подключаем библиотеку LCD IIC
#include <Servo.h>
#include <Keypad.h>
LiquidCrystal_I2C LCD(0x27, 16, 2); // присваиваем имя lcd для дисплея
bool sig; //переменная для сигнала кнопки
const byte ROWS = 4; // Количество строк на клавиатуре
const byte COLS = 4; // Количество столбцов на клавиатуре
char key;
int kolvo=0;
int error=0; //количество попыток ввода пароля
int T1=15; //время работы открытого сейфа в секундах
int T2=30; //время блокировки сейфа после неправильного пароля в секундах
char x[5]; //переменная для массива символов кнопок, нажатых на матричной клавиатуре
int z; //угол вращения сервомотора
int MIN=90; //начальное положение диапазона вращения сервомотора
int MAX=180;//конечное положение диапазона вращения сервомотора
char password[4]={'1','#','7','0'}; //пароль
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#',' '}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; // Пины для строк клавиатуры
byte colPins[COLS] = {5, 4, 3, 2}; // Пины для столбцов клавиатуры
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
Servo motor;
byte customChar1[] = {
B00000,
B00000,
B00000,
B00000,
B00011,
B00111,
B01100,
B01100
};
byte customChar2[] = {
B00000,
B00000,
B00000,
B00000,
B11000,
B11100,
B00110,
B00110
};
byte customChar3[] = {
B01100,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111
};
byte customChar4[] = {
B00110,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111
};
byte customChar5[] = {
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
B00000
};
void setup() {
LCD.init(); // инициализация LCD дисплея
LCD.backlight(); // включение подсветки дисплея
LCD.createChar(0, customChar1);
LCD.createChar(1, customChar2);
LCD.createChar(2, customChar3);
LCD.createChar(3, customChar4);
LCD.createChar(4, customChar5);
Serial.begin(9600);
motor.attach(10); // пин для сервомотора
motor.write(MIN); //начальная позиция сервомотора
LCD.setCursor(5, 0);
LCD.print("Hello,");
LCD.setCursor(1, 1);
LCD.print("I am smart safe");
delay(5000);
LCD.clear();
delay(1000);
}
void loop() {
label:
LCD.setCursor(0, 0);
LCD.print("Enter password");
LCD.setCursor(14,0);
LCD.write(0);
LCD.setCursor(15,0);
LCD.write(1);
LCD.setCursor(14,1);
LCD.write(2);
LCD.setCursor(15,1);
LCD.write(3);
key = keypad.getKey(); // Получаем символ, нажатый на клавиатуре
if(key){
if(key=='C') {
kolvo=0;
LCD.clear();
delay(500);
goto label;
}
if(key==' '){
kolvo=kolvo-2;
LCD.setCursor(kolvo, 1);
LCD.print(" ");
}
LCD.setCursor(kolvo, 1);
LCD.print(key);
delay(500);
LCD.setCursor(kolvo, 1);
LCD.print("*");
x[kolvo]=key;
kolvo=kolvo+1;
Serial.println(kolvo);
//////////////алгоритм для проверки пароля////////////////////////////////////////////////////////////////////////
if(kolvo==4){
if(x[0]==password[0] && x[1]==password[1] && x[2]==password[2] && x[3]==password[3] && x[4]==password[4])
//пароль правильный--------------------------------------------------------------------------------------------------------
{OPEN_ZAMOK();
goto label;}
//--------------------------------------------------------------------------------------------------------------------------
//пароль неправильный
else
{
CLOSE_ZAMOK();
goto label;
}
}
}
}
void OPEN_ZAMOK(){
LCD.clear();
delay(500);
LCD.setCursor(6, 0);
LCD.print("OPEN");
LCD.setCursor(14,0);
LCD.write(0);
LCD.setCursor(15,0);
LCD.write(4);
LCD.setCursor(14,1);
LCD.write(2);
LCD.setCursor(15,1);
LCD.write(3);
OPEN_SERVO();
TIMER_OPEN();
CLOSE_SERVO();
LCD.clear();
delay(500);
kolvo=0;
error=0;
}
void TIMER_OPEN(){
//цикл времени работы открытого сейфа
for(int t=T1; t>=0; t=t-1)
{
if (t>=10)
{LCD.setCursor(0,1);
}
else
{LCD.setCursor(0,1);
LCD.print("0");
LCD.setCursor(1,1);}
LCD.print(t);
delay(1000);
}
}
void TIMER_CLOSE(){
//цикл времени работы закрытого сейфа
for(int t=T2; t>=0; t=t-1)
{
if (t>=10)
{LCD.setCursor(0,1);
}
else
{LCD.setCursor(0,1);
LCD.print("0");
LCD.setCursor(1,1);}
LCD.print(t);
delay(1000);
}
}
void OPEN_SERVO() {
for(z=MIN; z<=MAX; z=z+1)
{motor.write(z);
delay(5);}
for(z=MAX; z>=MAX-10; z=z-1)
{motor.write(z);
delay(5);}
}
void CLOSE_SERVO() {
for(z=MAX-10; z>=MIN-10; z=z-1)
{motor.write(z);
delay(5);}
for(z=MIN-10; z<=MIN; z=z+1)
{motor.write(z);
delay(5);}
}
void CLOSE_ZAMOK(){
error=error+1;
LCD.clear();
delay(500);
LCD.setCursor(5, 0);
LCD.print("CLOSE");
LCD.setCursor(14,0);
LCD.write(0);
LCD.setCursor(15,0);
LCD.write(1);
LCD.setCursor(14,1);
LCD.write(2);
LCD.setCursor(15,1);
LCD.write(3);
if(error==1)
{LCD.setCursor(0,1);
LCD.print("2 attempt");
delay(2000);}
if(error==2)
{LCD.setCursor(0,1);
LCD.print("1 attempt");
delay(2000);}
if(error==3)
{LCD.setCursor(4,1);
LCD.print("BLOCKING");
TIMER_CLOSE();
error=0;}
LCD.clear();
delay(500);
kolvo=0;
}
void CHANGE_PASSWORD() {
}