#include <TM1637Display.h>
#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
// Define TM1637 display pins
const int CLK = 2;
const int DIO = 3;
// Define servo pins
const int SERVO1 =5;
const int SERVO2 =6;
// Define Keypad
const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPins[ROWS] = {9, 8, 7, 4}; // Connect to row pinouts of the keypad
byte colPins[COLS] = {13, 12, 11}; // Connect to column pinouts of the keypad
//The reason byte is used instead of int is mainly for memory efficiency and data optimization.
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Define the correct code
const int correctCode[4] = {1, 2, 3, 4}; // Change to your desired code
int enteredCode[4] = {0, 0, 0, 0};
int codeIndex = 0;
// Create TM1637 display object
TM1637Display display(CLK, DIO);
// Create Servo objects
Servo servo1;
Servo servo2;
// Initialize LCD
LiquidCrystal_I2C lcd(0x27, 16, 2); // Adjust address if needed
// Flag to track door state
bool doorOpened = false;
void setup() {
display.setBrightness(7); // Set maximum brightness
//Serial.begin(9600);
// Attach servos
servo1.attach(SERVO1);
servo2.attach(SERVO2);
// Initialize LCD
lcd.begin(16, 2);
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Enter Code:");
// Ensure servos start in the closed position
closeDoors();
}
void loop() {
char key = keypad.getKey();
if (key) {
//Serial.print("Key pressed: ");
// Serial.println(key);
if (key >= '0' && key <= '9') {
enteredCode[codeIndex] = key - '0';
codeIndex++;
}
// Converts the character into an integer
// key - '0' converts the character into
//its corresponding integer value.
// In ASCII, '0' is 48, '1' is 49, '2' is 50, etc.
// Subtracting '0' from any digit character gives the numeric value:
// '3' - '0' → 51 - 48 = 3
// '7' - '0' → 55 - 48 = 7
// Display entered digits
int displayNumber = enteredCode[0] * 1000 + enteredCode[1] * 100 + enteredCode[2] * 10 + enteredCode[3];
display.showNumberDec(displayNumber);
lcd.setCursor(0, 1);
lcd.print(displayNumber);
lcd.print(" "); // Clear extra digits if necessary
// Check code when 4 digits are entered
if (codeIndex == 4) {
if (enteredCode[0] == correctCode[0] && enteredCode[1] == correctCode[1] &&
enteredCode[2] == correctCode[2] && enteredCode[3] == correctCode[3]) {
// Serial.println("Code correct! Door opening...");
lcd.setCursor(0, 0);
lcd.print("Valid Code! ");
lcd.setCursor(0, 1);
lcd.print("Opening... ");
openDoors();
lcd.setCursor(0, 0);
lcd.print("Closing in 5s ");
lcd.setCursor(0, 1);
lcd.print("Please wait... ");
delay(5000);
closeDoors();
} else {
// Serial.println("Wrong code!");
lcd.setCursor(0, 0);
lcd.print("Wrong Code! ");
lcd.setCursor(0, 1);
lcd.print("Try again ");
delay(1000);
}
resetCode();
display.showNumberDec(0, true);
resetLCD();
}
}
}
void closeDoors(){
servo1.write(180);
servo2.write(0);
}
void openDoors(){
servo1.write(90);
servo2.write(90);
}
void resetCode(){
for (int i = 0; i < 4; i++) {
enteredCode[i] = 0;
}
codeIndex = 0;
}
void resetLCD(){
lcd.setCursor(0, 0);
lcd.print("Enter Code: ");
lcd.setCursor(0, 1);
lcd.print(" "); // Clear completely
lcd.setCursor(0, 1);
lcd.print("0000");
}