#include <Keypad.h> // Include the Keypad library
#include <ESP32Servo.h>
Servo myservo;
String enteredCode = "";
int wrong=0;
// Define the pins for the keypad and door lock
const byte ROWS = 4; // Number of rows on the keypad
const byte COLS = 3; // Number of columns on the keypad
char keys[ROWS][COLS] = { // Define the keys on the keypad
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {23,22, 3,21 }; // Pins connected to R1, R2, R3, R4 // Connect the keypad column pins to these pins on the ESP32
byte colPins[COLS] = { 19, 18,5 }; // Pins connected to C1, C2, C3, C4
const int lockPin = 2; // Connect the door lock to this pin on the ESP32
const String accessCode = "1234"; // Set the access code to a 6-digit string
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); // Create a new Keypad instance
void setup() {
pinMode(lockPin, OUTPUT); // Set the lock pin to output mode
pinMode(17,OUTPUT);
pinMode(32,INPUT);
myservo.attach(4);
Serial.begin(9600); // Initialize the serial communication with the ESP32
}
void loop() {
delay(100);
char key = keypad.getKey(); // Read the input from the keypad
if (key != NO_KEY) { // If a key is pressed
tone(17, 1200);
delay(40);
noTone(17);
if (key == '#' || enteredCode.length() >= 6) { // If the user has finished entering the code
if (enteredCode == accessCode) { // If the code entered matches the access code
Serial.println("Access granted"); // Print a message to the serial monitor
tone(17, 3000);
delay(500);
noTone(17);
digitalWrite(lockPin, HIGH); // Unlock the door
myservo.write(90);
delay(5000);
digitalWrite(lockPin, LOW);
myservo.write(0);
}
else if (enteredCode == accessCode+"*") { // If the code entered matches the access code
Serial.println("Access granted"); // Print a message to the serial monitor
tone(17, 3000);
delay(500);
noTone(17);
digitalWrite(lockPin, HIGH); // Unlock the door
myservo.write(90);
}
else if (enteredCode=="*"){
Serial.println("door is locked");
digitalWrite(lockPin, LOW);
myservo.write(0);
}
else { // If the code entered does not match the access code
Serial.println("Access denied"); // Print a message to the serial monitor
wrong=wrong+1;
tone(17, 2000);
delay(500);
noTone(17);
}
enteredCode = "";}// Reset the entered code
else { // If the user is still entering the code
enteredCode += key;
Serial.println(enteredCode); // Add the entered key to the entered code string
}
}
}
/*
#include <Keypad.h> // Include the Keypad library
String enteredCode = "";
int wrong=0;
// Define the pins for the keypad and door lock
const byte ROWS = 4; // Number of rows on the keypad
const byte COLS = 3; // Number of columns on the keypad
char keys[ROWS][COLS] = { // Define the keys on the keypad
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {23,22, 3,21 }; // Pins connected to R1, R2, R3, R4 // Connect the keypad column pins to these pins on the ESP32
byte colPins[COLS] = { 19, 18,5 }; // Pins connected to C1, C2, C3, C4
const int lockPin = 2; // Connect the door lock to this pin on the ESP32
const String accessCode = "1234"; // Set the access code to a 6-digit string
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); // Create a new Keypad instance
void setup() {
pinMode(lockPin, OUTPUT); // Set the lock pin to output mode
pinMode(17,OUTPUT);
pinMode(32,INPUT);
Serial.begin(9600); // Initialize the serial communication with the ESP32
}
void loop() {
if(digitalRead(32)==HIGH){
wrong=0;
}
if(wrong<3){
delay(100);
char key = keypad.getKey(); // Read the input from the keypad
if (key != NO_KEY) { // If a key is pressed
tone(17, 1200);
delay(40);
noTone(17);
if (key == '#' || enteredCode.length() >= 6) { // If the user has finished entering the code
if (enteredCode == accessCode) { // If the code entered matches the access code
Serial.println("Access granted"); // Print a message to the serial monitor
tone(17, 3000);
delay(500);
noTone(17);
digitalWrite(lockPin, HIGH); // Unlock the door
}
else if (enteredCode=="*"){
Serial.println("door is locked");
digitalWrite(lockPin, LOW);
}
else { // If the code entered does not match the access code
Serial.println("Access denied"); // Print a message to the serial monitor
wrong=wrong+1;
tone(17, 2000);
delay(500);
noTone(17);
}
enteredCode = "";}// Reset the entered code
else { // If the user is still entering the code
enteredCode += key;
Serial.println(enteredCode); // Add the entered key to the entered code string
}
}
}
else{
Serial.println("door has permanet locked");
}
}
*/