/********* Pleasedontcode.com **********
Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- Terms and Conditions:
You have a non-exclusive, revocable, worldwide, royalty-free license
for personal and commercial use. Attribution is optional; modifications
are allowed, but you're responsible for code maintenance. We're not
liable for any loss or damage. For full terms,
please visit pleasedontcode.com/termsandconditions.
- Project: **Keypad Control**
- Source Code NOT compiled for: Arduino Nano ESP32
- Source Code created on: 2024-10-05 08:31:11
********* Pleasedontcode.com **********/
/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
/* The servo motor installed in the door will lock or */
/* unlock based on the correct PIN code received by */
/* the button. If the door is unlocked and the PIN is */
/* correct, it will lock. If the door is locked and */
/* the PIN is correct, it will unlock. */
/****** SYSTEM REQUIREMENT 2 *****/
/* The webserver provides a web page about lock or */
/* unlock state of the door. */
/****** END SYSTEM REQUIREMENTS *****/
/********* User code review feedback **********
#### Feedback 1 ####
- put a physical button to enable introduction of pin in keypad
********* User code review feedback **********/
/****** DEFINITION OF LIBRARIES *****/
#include // Library to control servo motors
#include // Library to handle keypad input
#include // Library for WiFi connectivity
#include // Library to create a web server
/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void handleRoot(void);
void checkKeypad(void);
/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t keypad_Keypad3x4_R1_PIN_D4 = 4;
const uint8_t keypad_Keypad3x4_R2_PIN_D5 = 5;
const uint8_t keypad_Keypad3x4_R3_PIN_D6 = 6;
const uint8_t keypad_Keypad3x4_R4_PIN_D7 = 7;
const uint8_t buttonPin = 2; // Physical button pin
/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t keypad_Keypad3x4_C1_PIN_D8 = 8;
const uint8_t keypad_Keypad3x4_C2_PIN_D9 = 9;
const uint8_t keypad_Keypad3x4_C3_PIN_D10 = 10;
/***** DEFINITION OF KEYPAD VARIABLES *****/
const uint8_t KEYPAD3X4_ROWS = 4; // four rows
const uint8_t KEYPAD3X4_COLS = 3; // three columns
char Keys_Keypad3x4[KEYPAD3X4_ROWS][KEYPAD3X4_COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'},
};
// Create keypad object
Keypad keypad = Keypad(makeKeymap(Keys_Keypad3x4),
new byte[KEYPAD3X4_ROWS]{keypad_Keypad3x4_R1_PIN_D4, keypad_Keypad3x4_R2_PIN_D5, keypad_Keypad3x4_R3_PIN_D6, keypad_Keypad3x4_R4_PIN_D7},
new byte[KEYPAD3X4_COLS]{keypad_Keypad3x4_C1_PIN_D8, keypad_Keypad3x4_C2_PIN_D9, keypad_Keypad3x4_C3_PIN_D10},
KEYPAD3X4_ROWS, KEYPAD3X4_COLS);
// Create servo object
Servo doorServo;
// WiFi and web server setup
const char* ssid = "your_SSID"; // Replace with your WiFi SSID
const char* password = "your_PASSWORD"; // Replace with your WiFi password
WebServer server(80);
bool doorLocked = true; // Initial state of the door
const char* correctPin = "1234"; // Correct PIN code
bool pinEntryEnabled = false; // Flag to check if PIN entry is enabled
void setup(void) {
// Initialize pins
pinMode(keypad_Keypad3x4_R1_PIN_D4, INPUT_PULLUP);
pinMode(keypad_Keypad3x4_R2_PIN_D5, INPUT_PULLUP);
pinMode(keypad_Keypad3x4_R3_PIN_D6, INPUT_PULLUP);
pinMode(keypad_Keypad3x4_R4_PIN_D7, INPUT_PULLUP);
pinMode(keypad_Keypad3x4_C1_PIN_D8, OUTPUT);
pinMode(keypad_Keypad3x4_C2_PIN_D9, OUTPUT);
pinMode(keypad_Keypad3x4_C3_PIN_D10, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP); // Initialize button pin
// Attach servo to pin
doorServo.attach(3); // Attach servo to pin 3
// Start WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
}
// Start web server
server.on("/", handleRoot);
server.begin();
}
void loop(void) {
server.handleClient(); // Handle web server requests
checkKeypad(); // Check for keypad input
// Check if the button is pressed to enable PIN entry
if (digitalRead(buttonPin) == LOW) {
pinEntryEnabled = true; // Enable PIN entry when button is pressed
delay(500); // Debounce delay
}
}
void handleRoot(void) {
String state = doorLocked ? "Locked" : "Unlocked";
String message = "Door is " + state + "
";
server.send(200, "text/html", message);
}
void checkKeypad(void) {
char key = keypad.getKey();
static String inputPin = "";
if (pinEntryEnabled) { // Only check keypad if PIN entry is enabled
if (key) {
if (key == '#') {
// Check if the entered PIN is correct
if (inputPin == correctPin) {
doorLocked = !doorLocked; // Toggle door state
doorServo.write(doorLocked ? 0 : 90); // Lock or unlock the door
}
inputPin = ""; // Reset input
} else if (key == '*') {
inputPin = ""; // Clear input
} else {
inputPin += key; // Append key to input
}
}
}
}
/* END CODE */