Friday, May 17, 2013

Digital lock using arduino uno

HEY GUYS THIS IS THE CODE FOR DIGITAL LOCK USING ARDUINO UNO

PLEASE GO THROUGH IT

/*Circuit connections for LCD Interfacing:
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)
 *Circuit connections for Keypad Interfacing:
 */
#include <Keypad.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
char* secretCode = "2218";//character pointer secretCode stores the starting address of the string "2218"
int p=0;
const byte rows = 4;//four rows
const byte cols = 3;//three columns
char keys[rows][cols] =
{
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[rows] = {9,8,7,6};//connect to the row pinouts of the keypad
byte colPins[cols] = {12,11,10};//connect to the column pinouts of the keypad
Keypad nirma = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);
int redPin = 4;
int greenPin =2;
void setup()
{
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  lcd.begin(16,2);//set up the LCD's number of columns and rows
  lcd.print("Enter password ");
  //setLocked(true);
}

void loop()
{
  lcd.setCursor(0, 1);
  char input = nirma.getKey();//returns the key that is pressed
  for(int i=3;i>0;i--)//Loop controls no. of attempts(3)
  {
    for(int k=1;k<5;k++)//Loop controls no. of keypad presses(4)
    {
      if (input == '*' || input == '#')
      {
        p = 0;
        setLocked(true);
        lcd.println("Invalid numerals");//Red LED is on untill all 4 no.s have been pressed
      }
      else if(input == secretCode[p])
      {
        lcd.print("*");
        p++;
        if(p == 4)
        {
          setLocked(false);//Green LED glows when all 4 no.s have been entered
          lcd.println("Correct Password");
        }
      }
      else
      {
        lcd.print("*");
        p++;
        if(p == 4)
        {
          setLocked(true);
          lcd.println("I am locked.");
          lcd.println(i-1); lcd.print("trials left");
          if(i==1)
          {
            lcd.println("I am blocked.");
          }
        }
      }
    }
    delay(100);
  }
}
void setLocked(int locked)//user-defined function setlocked
{
  if(locked)
  {
    digitalWrite(redPin, HIGH);
    digitalWrite(greenPin, LOW);
  }
  else
  {
    digitalWrite(redPin, LOW);
    digitalWrite(greenPin, HIGH);
  }
}

2 comments:

  1. Where should I type the secret code ?
    Please Reply

    ReplyDelete
  2. hello Karthik

    sorry for late reply...
    Actually secret code is password of your lock....Here in My case it is 2218...you can put anything...it is 4 digit number like atm pin...

    ReplyDelete