Project tutorial

Pattern Lock! © GPL3+

A pattern lock using Arduino.

  • 998 views
  • 0 comments
  • 10 respects

Components and supplies

Apps and online services

About this project

Introduction

In this project we are making a pattern lock using Arduino and solenoid lock.

The lock will only get opened when the correct pattern is entered. the pattern is a combination of long and short press of the pattern switch.

  • Avoid the relay in the circuit, I have not used them

Demo

Theory

  • The code will constantly monitor the enter switch and patter switch state and store it.
  • Whenever I press the pattern button,
  • The time from millis() function will be stored in two variables, one for press time and one for release time.
  • Then the time difference between the release and press will be calculated and stored.
  • Then using a if condition the program will check whether the time difference is less-than or greater-than the predefined time for short and long press.
  • Then the code will check whether there is any space for the last detected press and if will be stored to the nearly available NULL space in the pattern lock array until all the array is filled.
  • Now if we press the enter button the program will compare the default pattern and the patter which you just entered.
  • If the pattern is true then the transistor will turn on and all the values stored to the pattern array will be erased.
  • If the pattern is false then the transistor will remain off and all the values stored to the pattern array will be erased.
  • Also, I have added some LEDs for indication.

Circuit

Connect the Circuit as shown,

  • Digital pin 2 and 3 for enter and pattern button.
  • Digital pin 5 and 6 for red and green LEDs.
  • Digital pin 12 to transistor base.
  • Transistor emitter to GND.
  • Transistor collector to solenoid ground.
  • 12v to solenoid VCC.
  • Don't forget to connect ground of 12 to ground of Arduino.
  • Diode in between vcc and ground of solenoid.
  • and don't forget all the resistors.

Video

Code

PatternLock.inoC/C++
//program by https://www.diyusthad.com
//https://wwww.youtube.com/c/diyusthad

#define pSw 2 //pattern switch pin
#define enter 3 //enter switch pin
#define redLED 5 //red led pin
#define greenLED 6 //green led pin
#define lock 12 //transistor pin
int pattern[4] = {NULL, NULL, NULL, NULL};
int pcode[4] = {1, 2, 2, 1}; //Enter your code 1 for short press and 2 for long press
double presstime = 0, releasetime = 0; //for saving press and release time
double timediff; //for storing time diffrence
bool flag1 = 1, flag2 = 0; //flag values 
bool enterSwStatus; //for storing enter switch status 
bool patternSwStatus; // for storing patern switch status 

void setup() {

  pinMode(pSw, INPUT);
  pinMode(enter, INPUT);
  pinMode(redLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  pinMode(lock, OUTPUT); digitalWrite(lock, LOW);
  Serial.begin(115200);

  digitalWrite(redLED, LOW);
  delay(100);
  digitalWrite(redLED, HIGH);
  digitalWrite(greenLED, LOW);
  delay(100);
  digitalWrite(greenLED, HIGH);

}

void loop() {

  if (digitalRead(pSw) == 1 && flag2 == 0)
  {
    presstime = millis();
    flag1 = 0;
    flag2 = 1;
    delay(10);
  }
  if (digitalRead(pSw) == 0 && flag1 == 0)
  {
    releasetime = millis();
    flag1 = 1;
    delay(10);
    timediff = releasetime - presstime;

    if (timediff <= 250)
    {
      Serial.println("short");
      digitalWrite(redLED, LOW);
      delay(10);
      digitalWrite(redLED, HIGH);
      flag2 = 0;
      flag1 = 1;
      while (true)
      {
        if (pattern[0] == 0)
        {
          pattern[0] = 1;
          break;
        }
        if (pattern[1] == 0)
        {
          pattern[1] = 1;
          break;
        }
        if (pattern[2] == 0)
        {
          pattern[2] = 1;
          break;
        }
        if (pattern[3] == 0)
        {
          pattern[3] = 1;
          break;
        }
        break;
      }

    }
    else if (timediff >= 250)
    {
      Serial.println("long");
      digitalWrite(redLED, LOW);
      delay(10);
      digitalWrite(redLED, HIGH);
      flag2 = 0;
      flag1 = 1;
      while (true)
      {
        if (pattern[0] == 0)
        {
          pattern[0] = 2;
          break;
        }
        if (pattern[1] == 0)
        {
          pattern[1] = 2;
          break;
        }
        if (pattern[2] == 0)
        {
          pattern[2] = 2;
          break;
        }
        if (pattern[3] == 0)
        {
          pattern[3] = 2;
          break;
        }
        break;
      }
    }
    patternSwStatus = 1;
  }


  if (digitalRead(enter) > 0 && enterSwStatus == 0)
  {
    enterSwStatus = 1;
    for (int i = 0; i < 4; i++)
    {
      Serial.print(pattern[i]);
    }
    Serial.println("");
    if (unlock() == false)
    {
      Serial.println("wrong");
      digitalWrite(redLED, LOW);
      delay(200);
      digitalWrite(redLED, HIGH);
    }
    else
    {
      Serial.println("access granted");
      digitalWrite(greenLED, LOW);
      digitalWrite(lock, HIGH);
      delay(10000);
      digitalWrite(greenLED, HIGH);
      digitalWrite(lock, LOW);
    }
    patternSwStatus = 0;
    pattern[0] = NULL;
    pattern[1] = NULL;
    pattern[2] = NULL;
    pattern[3] = NULL;
  }
  if (digitalRead(enter) == 0)
    enterSwStatus = 0;

}
int unlock()
{
  if (pcode[0] != pattern [0])
    return false;
  else if (pcode[1] != pattern [1])
    return false;
  else if (pcode[2] != pattern [2])
    return false;
  else if (pcode[3] != pattern [3])
    return false;
  else
    return true;
}

Schematics

Schematic
Final circuit e0me88oaiy

Comments

Similar projects you might like

Rampiot - Cool Smart Lock

Project tutorial by Robinson Mesino

  • 4,528 views
  • 2 comments
  • 29 respects

Arduino Keyless Door Lock System with Keypad and LCD

Project tutorial by DIY Hacking

  • 19,763 views
  • 16 comments
  • 49 respects

Password Based Door Lock

Project tutorial by Rishabh

  • 14,890 views
  • 6 comments
  • 29 respects

Gesture-Controlled Lock

Project tutorial by Arduino “having11” Guy

  • 9,860 views
  • 8 comments
  • 37 respects

Ultrasonic Lock

by IoannisD

  • 3,872 views
  • 2 comments
  • 6 respects

Smart Door Lock Using WiFi Login Page by Arduino & ESP8266

Project tutorial by ElectroPeak

  • 3,474 views
  • 1 comment
  • 25 respects
Add projectSign up / Login