0% found this document useful (0 votes)
4 views

Document4

The document contains an Arduino code for a security system using a 4x4 keypad, a servo motor, and an LCD display. The system allows users to enter a password ('123') to open the servo, which remains open until closed manually or after 5 seconds of inactivity. It includes features for displaying messages on the LCD and debugging information via Serial communication.

Uploaded by

you00737axa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Document4

The document contains an Arduino code for a security system using a 4x4 keypad, a servo motor, and an LCD display. The system allows users to enter a password ('123') to open the servo, which remains open until closed manually or after 5 seconds of inactivity. It includes features for displaying messages on the LCD and debugging information via Serial communication.

Uploaded by

you00737axa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <Keypad.

h>

#include <Servo.h>

#include <Wire.h>

#include <LiquidCrystal_I2C.h>

// Pin configuration for the 4x4 keypad

const byte ROW_NUM = 4; // Four rows

const byte COLUMN_NUM = 4; // Four columns

char keys[ROW_NUM][COLUMN_NUM] = {

{'1','2','3','A'},

{'4','5','6','B'},

{'7','8','9','C'},

{'*','0','#','D'}

};

// Pin assignments for the keypad

byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; // Connect to the row pinouts of


the keypad

byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; // Connect to the column


pinouts of the keypad

// Create keypad object

Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column,


ROW_NUM, COLUMN_NUM);

// Create Servo object

Servo myServo;
// Create LCD object (Address 0x27, 16x2 LCD display)

LiquidCrystal_I2C lcd(0x27, 16, 2);

// Variables for servo control

bool servoOpen = false; // Track if the servo is open or closed

char sequence[3]; // Store the last 3 keys pressed

byte seqIndex = 0; // Index to track sequence position

// Timer for inactivity (e.g., 5 seconds of inactivity before closing the servo)

unsigned long lastKeyPressTime = 0; // Track the last key press time

const unsigned long INACTIVITY_TIMEOUT = 5000; // 5 seconds

// Password to open the servo (as "123")

const char password[] = "123";

byte passwordLength = 3; // Length of the password

void setup() {

// Attach the servo to pin 9

myServo.attach(9);

// Initialize the servo to the closed position

myServo.write(0); // 0 degrees is the closed position

// Start Serial communication for debugging

Serial.begin(9600);

// Initialize LCD
lcd.begin(16, 2); // Initialize the LCD

lcd.setBacklight(1); // Turn on the backlight

// Show initial message

lcd.clear();

lcd.print("Enter the password");

void loop() {

// Read the pressed key

char key = keypad.getKey();

// Check if a key was pressed

if (key) {

Serial.println(key); // For debugging purposes, print the key pressed

// Update the last key press time

lastKeyPressTime = millis();

// If we haven't yet reached the length of the password sequence

if (seqIndex < passwordLength) {

sequence[seqIndex] = key; // Store the key in the sequence

seqIndex++; // Increment the sequence index

// Check if the sequence has reached the password length (3 digits)

if (seqIndex == passwordLength) {
// Compare the entered sequence with the password

if (strncmp(sequence, password, passwordLength) == 0) {

// Correct password entered, open the servo

if (!servoOpen) {

myServo.write(90); // 90 degrees is the open position

servoOpen = true;

lcd.clear();

lcd.print("Access Granted");

Serial.println("Correct password! Servo opened.");

} else {

// Incorrect password, reset the sequence

seqIndex = 0; // Reset the sequence index

lcd.clear();

lcd.print("Access Declined");

Serial.println("Incorrect password, sequence reset.");

// If key '6' is pressed, close the servo

if (key == '6' && servoOpen) {

myServo.write(0); // Close the servo

servoOpen = false;

lcd.clear();

lcd.print("Servo Closed");

Serial.println("Servo closed by pressing 6");


}

// Check if the servo should be closed due to inactivity

if (servoOpen && (millis() - lastKeyPressTime > INACTIVITY_TIMEOUT)) {

// Do nothing, inactivity timer has been replaced by manual '6' press

You might also like