Site icon DIY Usthad

Arduino Secret Pattern Lock

Arduino pattern lock

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.

Hardware Used

Components AliExpressBanggoodAmazon.comAmazon.inWin Source
Micro push-button
Arduino Nano
TIP120 transistor
Resistors 2.3k,10k(kit)
1N4007 Diode
Solenoid Lock

Watch the video

Theory

Circuit

Connect the Circuit as shown,

Arduino Code


//https://diyusthad.com

#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;
}


How to change the LOCK CODE?

int pcode[4] = {1, 2, 2, 1}; 

Enter your code “1” for short press and “2” for long press in the above-mentioned line.

For example, if you want to set your code as “long short short short” then change the line to this ????????

int pcode[4] = {2, 1, 1, 1}; 
Exit mobile version