Skip to main content

Password Door Lock Using Tinkercad

 #include <Servo.h>

#include <LiquidCrystal_I2C.h>

#include <Keypad.h>

 

#ifndef PASSWORD_H

#define PASSWORD_H


 

#define MAX_PASSWORD_LENGTH (20)


#define STRING_TERMINATOR '\0'


class Password {

public:

/**

* @brief Construct object in memory, set all variables

* @param pass 

*/

Password(char* pass);

/**

* @brief Set the password

* @param pass 

*/

void set(char* pass);

/**

* @brief Evaluate a string, is it equal to the password?

* @param pass 

* @return true 

* @return false 

*/

bool is(char* pass);

/**

* @brief Append a char to the guessed password

* @param character 

* @return true 

* @return false 

*/

bool append(char character);

/**

* @brief Reset the guessed password, one can guess again

*/

void reset();

/**

* @brief Is the current guessed password equal to the target password?

* @return true 

* @return false 

*/

bool evaluate();

//char* getPassword();

//char* getGuess();

//operators

/**

* @brief password using operator =

*/

Password &operator=(char* pass);

/**

* @brief Test password using ==

*/

bool operator==(char* pass);

/**

* @brief Test password using != 

*/

bool operator!=(char* pass);

/**

* @brief Append to currently guessed password using operator <<

*/

Password &operator<<(char character);

private:

char* target;

char guess[ MAX_PASSWORD_LENGTH ];

byte currentIndex;

};


#endif



Password::Password(char* pass) {

  set(pass);

  reset();

}


void Password::set(char* pass) { target = pass; }


bool Password::is(char* pass) {

  byte i = 0;

  while (*pass && i < MAX_PASSWORD_LENGTH) {

    guess[i] = pass[i];

    i++;

  }

  return evaluate();

}


bool Password::append(char character) {

  if (currentIndex + 1 == MAX_PASSWORD_LENGTH) {

    return false;

  } else {

    guess[currentIndex++] = character;

    guess[currentIndex] = STRING_TERMINATOR;  // ensure a valid c string

  }

  return true;

}


void Password::reset() {

  currentIndex = 0;

  guess[currentIndex] = STRING_TERMINATOR;

}


bool Password::evaluate() {

  char pass = target[0];

  char guessed = guess[0];

  for (byte i = 1; i < MAX_PASSWORD_LENGTH; ++i) {

    // check if guessed char is equal to the password char

    if ((STRING_TERMINATOR == pass) && (STRING_TERMINATOR == guessed)) {

      return true;  // both strings ended and all previous characters are equal

    } else if ((pass != guessed) || (STRING_TERMINATOR == pass) ||

               (STRING_TERMINATOR == guessed)) {

      return false;  // difference OR end of string has been reached

    }


    // read next char

    pass = target[i];

    guessed = guess[i];

  }

  return false;  // a 'true' condition has not been met

}


Password& Password::operator=(char* pass) {

  set(pass);

  return *this;

}


bool Password::operator==(char* pass) { return is(pass); }


bool Password::operator!=(char* pass) { return !is(pass); }


Password& Password::operator<<(char character) {

  append(character);

  return *this;

}


 

 


#define buzzer 11


Servo servo;

LiquidCrystal_I2C lcd(0x27, 16, 2);


String newPasswordString; //hold the new password

char newPassword[6]; //charater string of newPasswordString

byte a = 5;

bool value = true;


Password password = Password("0123"); //Enter your password


byte maxPasswordLength = 6;

byte currentPasswordLength = 0;

const byte ROWS = 4; // Four rows

const byte COLS = 4; // Four columns



char keys[ROWS][COLS] = {

  {'D', 'C', 'B', 'A'},

  {'#', '9', '6', '3'},

  {'0', '8', '5', '2'},

  {'*', '7', '4', '1'},

};



byte rowPins[ROWS] = {2, 3, 4, 5};

byte colPins[COLS] = {6, 7, 8, 9};


Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );



void setup() {

  

  Serial.begin(9600);

  pinMode(buzzer, OUTPUT);

  servo.attach(10);

  servo.write(50);

  lcd.init();

  lcd.backlight();

  lcd.setCursor(3, 0);

  lcd.print("WELCOME TO");

  lcd.setCursor(0, 1);

  lcd.print("DOOR LOCK SYSTEM");

  delay(3000);

  lcd.clear();

}


void loop() {

  lcd.setCursor(1, 0);

  lcd.print("ENTER PASSWORD");


  char key = keypad.getKey();

  if (key != NO_KEY) {

    delay(60);

    if (key == 'C') {

      resetPassword();

    } else if (key == 'D') {

      if (value == true) {

        doorlocked();

        value = false;

      } else if (value == false) {

        dooropen();

        value = true;

      }

    } else {

      processNumberKey(key);

    }

  }

}


void processNumberKey(char key) {

  lcd.setCursor(a, 1);

  lcd.print("*");

  a++;

  if (a == 11) {

    a = 5;

  }

  currentPasswordLength++;

  password.append(key);


  if (currentPasswordLength == maxPasswordLength) {

    doorlocked();

    dooropen();


  }

}


void dooropen() {

  if (password.evaluate()) {

    digitalWrite(buzzer, HIGH);

    delay(300);

    digitalWrite(buzzer, LOW);

    servo.write(50);

    delay(100);

    lcd.setCursor(0, 0);

    lcd.print("CORRECT PASSWORD");

    lcd.setCursor(0, 1);

    lcd.print("DOOR OPENED");

    delay(2000);

    lcd.clear();

    a = 5;

  } else {

    digitalWrite(buzzer, HIGH);

    delay(200);

    digitalWrite(buzzer, LOW);

    delay(200);

    digitalWrite(buzzer, HIGH);

    delay(200);

    digitalWrite(buzzer, LOW);

    delay(200);

    digitalWrite(buzzer, HIGH);

    delay(200);

    digitalWrite(buzzer, LOW);

    delay(200);

    lcd.setCursor(0, 0);

    lcd.print("WRONG PASSWORD!");

    lcd.setCursor(0, 1);

    lcd.print("PLEASE TRY AGAIN");

    delay(2000);

    lcd.clear();

    a = 5;

  }

  resetPassword();

}


void resetPassword() {

  password.reset();

  currentPasswordLength = 0;

  lcd.clear();

  a = 5;

}


void doorlocked() {

  if (password.evaluate()) {

    digitalWrite(buzzer, HIGH);

    delay(300);

    digitalWrite(buzzer, LOW);

    servo.write(110);

    delay(100);

    lcd.setCursor(0, 0);

    lcd.print("CORRECT PASSWORD");

    lcd.setCursor(2, 1);

    lcd.print("DOOR LOCKED");

    delay(2000);

    lcd.clear();

    a = 5;

  } else {

    digitalWrite(buzzer, HIGH);

    delay(200);

    digitalWrite(buzzer, LOW);

    delay(200);

    digitalWrite(buzzer, HIGH);

    delay(200);

    digitalWrite(buzzer, LOW);

    delay(200);

    digitalWrite(buzzer, HIGH);

    delay(200);

    digitalWrite(buzzer, LOW);

    delay(200);

    digitalWrite(buzzer, HIGH);

    delay(200);

    digitalWrite(buzzer, LOW);

    delay(200);

    lcd.setCursor(0, 0);

    lcd.print("WRONG PASSWORD!");

    lcd.setCursor(0, 1);

    lcd.print("PLEASE TRY AGAIN");

    delay(2000);

    lcd.clear();

    a = 5;

  }

  resetPassword();

}

Comments

Post a Comment

Popular posts from this blog

Solar Tracking System

Dual-Axis Solar Tracking System | Arduino SimpleCircuits Arduino Solar DIY Renewable Energy Project Dual-Axis Solar Tracking System Build a high-performance solar tracker that follows the sun in real-time. Four LDR sensors, an Arduino UNO, and two servo motors — boosting energy capture by up to 40% versus fixed mounts. 40% More Efficient 4 LDR Sensors 2 Servo Axes <300mA Power Draw Finished Build System Overview How the System Works The tracker reads the sky through four LDR sensors placed around the panel, computes intensity deltas, and drives two SG90 servos in a real-time closed loop — keeping the panel perpendicular to sunlight from sunrise to sunset. ...

Arduino Code Car Parking System

 // Created by Simple Circuits  #include <Wire.h>  #include <LiquidCrystal_I2C.h> LiquidCrystal_I2C lcd(0x27,16,2);    #include <Servo.h>  Servo myservo; int IR1 = 2; int IR2 = 3; int Slot = 4;           //Total number of parking Slots int flag1 = 0; int flag2 = 0; void setup() {   Serial.begin(9600);      lcd.init(); //initialize the lcd     lcd.backlight(); //open the backlight     pinMode(IR1, INPUT); pinMode(IR2, INPUT);    myservo.attach(4); myservo.write(100); lcd.setCursor (0,0); lcd.print("     ARDUINO    "); lcd.setCursor (0,1); lcd.print(" PARKING SYSTEM "); delay (2000); lcd.clear();   } void loop(){  if(digitalRead (IR1) == LOW && flag1==0){ if(Slot>0){flag1=1; if(flag2==0){myservo.write(0); Slot = Slot-1;} }else{ lcd.setCursor (0,0); lcd.print("    SORRY :(    ");   lc...

Arduino Code

 //define Pins #include <Servo.h> Servo servo; int trigPin = 11; int echoPin = 12; // defines variables long duration; int distance; void setup()  {   servo.attach(13);   servo.write(180);  delay(2000);    // Sets the trigPin as an Output pinMode(trigPin, OUTPUT); // Sets the echoPin as an Input  pinMode(echoPin, INPUT); } void loop()  { // Clears the trigPin digitalWrite(trigPin, LOW); delayMicroseconds(2); // Sets the trigPin on HIGH state for 10 micro seconds digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Reads the echoPin, returns the sound wave travel time in microseconds duration = pulseIn(echoPin, HIGH); // Calculating the distance distance= duration*0.034/2; // Prints the distance on the Serial Monitor Serial.print("Distance: "); Serial.println(distance); if ( distance <= 25   ) // Change Distance according to Ultrasonic Sensor Placement  { servo.write(180); delay(3000); ...