Table of Contents
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 changed 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
- 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)
- After a key is pressed, the value of the key is examined using a switch-case statement at (5)
- 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
- 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) - 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 the 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.
870730 744454Just wanna comment which you have a very nice internet internet site , I enjoy the style it really stands out. 27542
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
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
943671 4346really nice post, i definitely genuinely like this remarkable website, maintain on it 578773
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
436377 672696Awesome inkling Grace! ego was luxurious youd bring about this about your biz bump into upstanding lineage. We reason you! 232241
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
635608 667822Some times its a pain inside the ass to read what people wrote but this internet web site is very user friendly ! . 855704
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
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