Hackster is hosting Hackster Holidays, Ep. 4: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Wednesday!Stream Hackster Holidays, Ep. 4 on Wednesday!
Najad
Published © GPL3+

Pattern Lock!

A pattern lock using Arduino.

BeginnerFull instructions provided4,551

Things used in this project

Hardware components

UTSOURCE Electronic Parts
UTSOURCE Electronic Parts
×1
Arduino Nano
×1
UTSOURCE TIP120
×1
Linear Solenoid, 12 VDC
Linear Solenoid, 12 VDC
×1
Resistor 10k ohm
Resistor 10k ohm
×3
LED (generic)
LED (generic)
×2
1N4007 – High Voltage, High Current Rated Diode
1N4007 – High Voltage, High Current Rated Diode
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Schematic

Code

PatternLock.ino

C/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;
}

Credits

Najad

Najad

30 projects • 97 followers
Just crazy stuff​

Comments