Knock Door - 090017

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

KNOCK DOOR

#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

Servo lockServo;
LiquidCrystal_I2C lcd(0x27, 16, 2); // Address 0x27, 16 columns, 2 rows

const int soundSensorPin = A0;


int soundThreshold = 400;

const int knockThreshold = 400; // Adjust this threshold based on your environment
int knockCount = 0;
unsigned long lastKnockTime = 0;
const int knockTimeout = 300; // Timeout between knocks in milliseconds

bool isLocked = true;


unsigned long unlockTime = 0;
const unsigned long lockDuration = 3000; // 10 seconds in milliseconds

void setup() {
lockServo.attach(9);
lcd.init();
lcd.backlight();
lcd.setCursor(3,0);
lcd.print("Knock Me!");
lcd.setCursor(3,1);
lcd.print("To Enter");
}

void loop() {
int soundValue = analogRead(soundSensorPin);

if (soundValue > soundThreshold) {


if (millis() - lastKnockTime > knockTimeout) {
knockCount = 0; // Reset knock count if timeout passed
lcd.clear();
lcd.print("Knock Me!");
}
lastKnockTime = millis();

if (soundValue > knockThreshold) {


knockCount++;
if (knockCount >= 5 ) {
if (isLocked) {
unlockDoor();
} else {
lockDoor();
}
delay(500); // Debounce delay
}
}

if (knockCount < 5) {
lcd.clear();
lcd.setCursor(3,0);
lcd.print("Knock Me!");
lcd.setCursor(3,1);
lcd.print("To Enter");

} else if (knockCount >= 5) {


lcd.clear();
lcd.setCursor(2,0);
lcd.print("Welcome Home!");
lcd.setCursor(6,1);
lcd.print("Pogi");
delay(1000); // Display the message for 3 seconds
lcd.clear();
lcd.print("Knock Me!");
lockDoor(); // Lock the door
delay(100); // Wait for a second before resetting knock count
knockCount = 0; // Reset knock count
}
}

if (!isLocked && millis() - unlockTime >= lockDuration) {


lockDoor(); // Lock the door automatically after lockDuration
}
}

void lockDoor() {
lockServo.write(0);
lcd.clear();
lcd.setCursor(3,0);
lcd.print("Knock Me!");
lcd.setCursor(3,1);
lcd.print("To Enter");
isLocked = true;
}

void unlockDoor() {
lockServo.write(90);
lcd.clear();
lcd.setCursor(2,0);
lcd.print("Welcome Home!");
lcd.setCursor(6,1);
lcd.print("Pogi");
isLocked = false;
unlockTime = millis(); // Record the time when the door was unlocked
}

You might also like