#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include <Keypad.h>
int lcdColumns = 16;
int lcdRows = 2;
Servo ServoMotor;
char* password = "427"; // change the password here, just pick any 3 numbers
int position = 0;
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' }
};
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);
uint8_t colPins[COLS] = { 27, 26, 25, 33 }; // Pins connected to C1, C2, C3, C4
uint8_t rowPins[ROWS] = { 5, 17, 12, 14 }; // Pins connected to R1, R2, R3, R4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
int Coun1 = 0;
int Coun2 = 0;
void setup() {
Serial.begin(9600);
// initialize LCD
lcd.init();
// turn on LCD backlight
lcd.backlight();
//pinMode(RedpinLock, OUTPUT);
pinMode(GreenpinUnlock, OUTPUT);
ServoMotor.attach(11);
LockedPosition(true);
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
pinMode(18, INPUT_PULLUP);
pinMode(19, INPUT_PULLUP);
pinMode(12, OUTPUT);
pinMode(14, OUTPUT);
delay(100);
digitalWrite(12, HIGH);
digitalWrite(14, HIGH);
delay(100);
}
void loop() {
char key = keypad.getKey();
lcd.setCursor(0, 1);
{
pinMode(RedpinLock, OUTPUT);
pinMode(GreenpinUnlock, OUTPUT);
ServoMotor.attach(11);
LockedPosition(true);
}
{
char key = keypad.getKey();
if (key == '*' || key == '#')
{
position = 0;
LockedPosition(true);
}
if (key == password[position])
{
position ++;
}
if (position == 3)
{
LockedPosition(false);
}
delay(100);
}
void LockedPosition(int locked)
{
if (locked)
{
digitalWrite(RedpinLock, HIGH);
digitalWrite(GreenpinUnlock, LOW);
ServoMotor.write(11);
}
else
{
digitalWrite(RedpinLock, LOW);
digitalWrite(GreenpinUnlock, HIGH);
ServoMotor.write(90);
}
}
if (key != NO_KEY) {
lcd.print(key);
}
// put your main code here, to run repeatedly:
// this speeds up the simulation
int sign_in = digitalRead(18);
int sign_in_2 = digitalRead(19);
if((sign_in == 1) || (sign_in_2 ==1)){
Coun1+= sign_in;
Coun2+= sign_in_2;
lcd.print(Coun1);
Serial.print(" Coun1 :");
Serial.println(Coun1);
Serial.print(" Coun2 :");
Serial.println(Coun2);
digitalWrite(12, HIGH);
digitalWrite(14, HIGH);
delay(5000);
digitalWrite(12, LOW);
digitalWrite(14, LOW);
delay(5000);
}
else {
digitalWrite(12, LOW);
digitalWrite(14, LOW);
}
}