Keypad Controlled Lock Using Arduino with Keypad library

In this section we are making a password control lock (Keypad controlled lock using arduino with keypad library), we first define our password here it is of 6 characters long but can be change according to our convenience.

In this we are using serial monitor for displaying result/message for matched or not matched conditions, However, you can use Liquid Crystal LCD 16X2. To operate the lock, the user must press * and then the secret number, followed by #.

If you earlier didn’t learn about Keypad I recommend you to first learn about it.

KEYPAD CONTROLLED LOCK USING ARDUINO WITH KEYPAD LIBRARY

PROGRAMMING CODE

// including keypad library
#include<Keypad.h>

const byte ROWS = 4;   // set 4 rows
const byte COLS = 3;  // set 3 columns, if you have 4x4 keypad change it to 4

// defining keys position in 2D (two dimension) array
char keys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};

// Defining Arduino pins to be connected to keypad rows & column
byte rowPins[ROWS] = {5, 4, 3, 2}; // r1,r2,r3,r4
byte colPins[COLS] = {8, 7, 6}; // c1, c2, c3

/* creating object variable "kypd" (can be any name like k, a etc. ) of Keypad library, to use inbuilt functions of library
*/
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

// our password stored in character array PIN
// (1)
char PIN[6] = {'1', '2', '3', '4', '5', '6'};

// character array for attempt & comparison initialize with 0
char attempt[6] = {0, 0, 0, 0, 0, 0};

int count = 0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  // (6)
  readKeypad();
}

void readKeypad() {
  /* keypad is used for accessing function/method getKey() which returns key value & stored in 'key' a char type variable
  */
  char key = keypad.getKey();
  /* if has been pressed enter in 'if block'
    /* switch has three condition
     if * is pressed then set count 0 for reading password
     if # is pressed then verify for the password
     other time record key in attempt variable and increase index (count) value
  */
  if (key) {
    Serial.println(key);

    // (5)
    switch (key) {
      case '*': count = 0; Serial.println("Reset Done! Enter Again"); break;
      case '#': delay(100); // removes the possibility of switch bounce condition
        check(); break;
      default: attempt[count] = key; count++;
    }
  }
}

// call this function and perform task you desired, if the PIN entered is correct
void correct() {
  Serial.println("Correct PIN entered...");
  count = 0;
}

// call this function and perform task you desired, if the PIN entered is not correct
void incorrect () {
  Serial.println("Incorrect PIN entered!");
  count = 0;
}

// (2)
void check() {
  int n = 0;
  int j;
  for ( j = 0; j < 6 ; j++ ) {
    if (attempt[j] == PIN[j]) {
      n++;
    }
  }
  if (n == 6) {
    // (3)
    correct();
  } else {
    // (4)
    incorrect();
  }
  for (int i = 0; i < 6; i++) // remove previously entered code attempt from {
    attempt[i] = 0;
}

CODE EXPLANATION – KEYPAD CONTROLLED LOCK USING ARDUINO WITH KEYPAD LIBRARY

How It Works

  1. After the usual setup routines (as described in Listing 9-1), the sketch continually “listens” to the keypad by running the function readKeypad() at (6)
  2. After a key is pressed, the value of the key is examined using a switch-case statement at (5)
  3. The values of the keys pressed on the keypad are stored in the array attempt[], and when the user presses #, the function check() will called
  4. At (2), the values of keys pressed are compared against the PIN stored in the array PIN[] at (1), which holds the secret number.
    If the correct sequence is entered, the function correct() at (3) is called, where you can add your own code to execute; but if the incorrect sequence is entered, the function incorrect() is called at (4)
  5. Finally, once the user’s entry has been checked, it is removed from memory and the code is ready for the next test.

Testing the Sketch

After you’ve uploaded the sketch to the Arduino, open the Serial Monitor window, press the asterisk key (*) on the numeric keypad, type the secret number, and then press the pound sign key (#) when you’ve finished. Try entering both correct and incorrect numbers. Your results should be similar to the output shown in Figure

Figure 9-4: Results from entering correct and incorrect PINs,

This example served as a perfect foundation for your own PIN-activated devices, such as locks, alarms, or anything else you can imagine.

Just be sure to replace the code in correct() and incorrect() function (like turn on or off LED or rotation of motor) with the required code to run when a correct or incorrect sequence is entered.

Looking Ahead Once again, you have learned another way to gather input for your Arduino. You’ve also gained the foundational knowledge to create a useful method of controlling a sketch using a numeric keypad, as well as the foundations for a combination lock to access anything that your Arduino can control.

You’ve also learned the very useful switch-case function. Moving on to the next chapter you’ll learn about another form of input: the touchscreen.

Total Visits: 645
0 0 vote
Article Rating
Subscribe
Notify of
guest
43 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
buy watch
6 months ago

870730 744454Just wanna comment which you have a very nice internet internet site , I enjoy the style it really stands out. 27542

social
6 months ago

736342 451711I recognize there exists a lot of spam on this weblog. Do you want support cleansing them up? I may assist amongst courses! 196689

wall and floor stickers
6 months ago

460656 824244The vacation trades offered are evaluated a variety of within the chosen and merely good value all about the world. Those hostels are normally based towards households which you will discover accented via charming shores promoting crystal-clear fishing holes, concurrent of ones Ocean. Hotels Discounts 889699

Jasmine
6 months ago

943671 4346really nice post, i definitely genuinely like this remarkable website, maintain on it 578773

car door decals graphics
6 months ago

733722 889495I observe there is lots of spam on this weblog. Do you want assist cleaning them up? I may well assist among classes! 993201

tailgate mural
6 months ago

436377 672696Awesome inkling Grace! ego was luxurious youd bring about this about your biz bump into upstanding lineage. We reason you! 232241

location voiture Guadeloupe et martinique
6 months ago

281113 410319Excellent paintings! This really is the kind of information that really should be shared around the web. Disgrace on Google for now not positioning this publish upper! Come on more than and speak over with my web site . Thanks =) 31306

ww88
6 months ago

635608 667822Some times its a pain inside the ass to read what people wrote but this internet web site is very user friendly ! . 855704

cialisis
6 months ago

560631 100366hi!,I like your writing so significantly! share we communicate a lot more about your article on AOL? I call for an expert on this area to solve my problem. Possibly thats you! Searching forward to see you. 249066

mejaqq pkv
6 months ago

249341 891078This is a nice weblog i ought to say, usually i don????t post comments on other people???? blogs but would like to say that this post truly forced me to do so! 203810

qiuqiu99
6 months ago

868831 899945The place else could anybody get that kind of info in such a perfect method of writing? 981477

pasar poker
6 months ago

96065 591086I recognize there is certainly a great deal of spam on this blog. Do you want help cleansing them up? I could help in between classes! 961764

duratrans nyc
6 months ago

124533 677670Just wanna input on couple of common things, The site design is perfect, the articles is quite great : D. 107911

fowling game set for sale
6 months ago

822194 214190I as properly conceive so , perfectly indited post! . 689964

Emersyn
6 months ago

58714 701084Hello! I simply would like to give a huge thumbs up for the wonderful info youve here on this post. I may well be coming back to your weblog for a lot more soon. 954990

http://www.rajaqq1.club/
6 months ago

778738 517021Yay google is my king helped me to locate this excellent web web site ! . 108029

wayangpoker
6 months ago

749652 387437This is some fantastic data. I expect additional facts like this was distributed across the internet today. 126646

vipbandarq
6 months ago

267908 490121You got a quite excellent internet site, Gladiola I discovered it by way of yahoo. 475212

rajaqq
5 months ago

946403 306488actually great post, i undoubtedly really like this site, maintain on it 336824

martial arts near Harrisburg cmawest.com
5 months ago

299207 25548Yeah bookmaking this wasnt a bad conclusion fantastic post! . 425475

Translate »
43
0
Would love your thoughts, please comment.x
()
x