Tuesday, March 22, 2016

Password lock with only one button switch using Arduino

Electronic locks with password always had a keyboard for inserting the code, but following lock used only one button switch.
The code must inserts by pressing the button switch 5 times but with certain delay between each pressing, as you can see in programming code i sets this delay time 1 second delay between four first codes and 5 seconds for two last codes, you can change this code by changing that delay times in programming code.
After inserting the correct code the load that connected to pin10 runs for 3 seconds, you can change this delay time in code.
If an incorrect code inserts, the red LED turns ON also for 3 seconds.
Last important note is the "tolerance" stage in programming code, i sets it on 0.4, that means the delay time between each two pressed buttons acceptable 40% more or lower than the delay time that you write it in the programming code, so 600 to 1400 milliseconds can accepted rather than 1000 milliseconds, or 3000 to 7000 milliseconds can accepted rather than 5000 milliseconds.(the "tolerance" is very important because you never can press the button exactly 5000 milliseconds or exactly 1000 milliseconds).

Password lock with only one button switch using Arduino

Password lock with only one button switch using Arduino
Password lock with only one button switch using Arduino
Password lock with only one button switch using Arduino

Password lock with only one button switch using Arduino




int count=0;
unsigned long a[]={0,0,0,0,0};
unsigned long b[]={0,0,0,0,0};
unsigned long del[]={1000,1000,1000,5000};

float tolerance=.4;

void setup()
{
  pinMode(2, INPUT);
  pinMode(10, OUTPUT);
  pinMode(13, OUTPUT);
}

void dataCapture()
{
  a[count]=millis();
  delay(500); // for button debounce, your value may differ
  count++;
}

void checkCombination()
{
    int compare=0;
    count=0;
    // calcualate delays between keypresses
    b[0]=a[1]-a[0];
    b[1]=a[2]-a[1];
    b[2]=a[3]-a[2];
    b[3]=a[4]-a[3];

    // compare the button delay values
    if (b[0]<=(del[0]*(1+tolerance)) && b[0]>=(del[0]*(1-tolerance))) { compare++; }
    if (b[1]<=(del[1]*(1+tolerance)) && b[1]>=(del[1]*(1-tolerance))) { compare++; }   
    if (b[2]<=(del[2]*(1+tolerance)) && b[2]>=(del[2]*(1-tolerance))) { compare++; }
    if (b[3]<=(del[3]*(1+tolerance)) && b[3]>=(del[3]*(1-tolerance))) { compare++; }
    if (compare==4) { digitalWrite(10,HIGH); }
    delay(3000);
    digitalWrite(10,LOW);
    if (compare!=4) { digitalWrite(13,HIGH); }
    delay(3000);
    digitalWrite(13,LOW);
   
   
}

void loop()
{
  if (digitalRead(2)==HIGH)
  {
    dataCapture();
  }
  if (count>4)
  {
    checkCombination();
  }
}