How to Specify User-Defined Functions for Your Keypad Entry System Arduino Project - dummies

How to Specify User-Defined Functions for Your Keypad Entry System Arduino Project

By Brock Craft

The final part of the code for your keypad entry system Arduino project contains four functions that handle identifying the key pressed, sending commands to the Max chip, clearing the display, and activating the door mechanism.

The clever part of this code is how the keypress is determined. It uses code by Michael Margolis in his indispensable reference book, Arduino Cookbook, published by O’Reilly Media. The digital pins for the rows were pulled up HIGH during setup(). This means that reading those pins will always evaluate to HIGH, unless something else is pulling them low — like a keypress connecting them to a LOW column pin.

The getKey function goes through each of the digital pins connected to the column pins of your keypad and sets the pin for LOW. It then checks to see if any row pins are now also LOW. The pull-up resistors you used in setup() keep the row pins HIGH — unless a key has been pressed, connecting it to a LOW column pin!

The while statement only operates while a condition exists, in this case when one of the row pins has become LOW due to a keypress. At this moment, the keyMap is consulted to find the corresponding number for that row and column, and this is stored in the variable key.

Next, you do a little housekeeping, setting all the column pins back to HIGH, for the next time around. Finally, the value for key is returned to the part of the program that requested it. Whew!

The only thing remaining is to unlock the door to your fortress/castle/laboratory. If you haven’t already done so, add the last part of your code at the end. In the unlock() function, the four sendCommand() instructions print the message “HI” to the display. It’s nice to receive a warm welcome — if only a brief one. Then the action happens.

The strikePin is written HIGH, which will provide power to the transistor controlling the relay. When it activates, power is allowed to pass to your unlocking mechanism for the duration specified by doorOpenTime. Then you set strikePin to LOW to keep out any nefarious intruders, spies, and miscreants.

If you haven’t already done so, upload the code and cross your fingers. When you press a key, you should see its number displayed in your LED module. Open the serial monitor in the Arduino IDE and observe what happens when you press a key. If everything is connected correctly, you should see the keys identified and the codeBuffer[] gradually being filled with the values you have entered.

If you don’t see the digits lighting up on your LED module, check your connections again. If you don’t see the correct values on your serial monitor, make certain that there is not an error in your code and that the values for rowPins and colPins are correct. After you’ve confirmed this is all working, you can move on, to add the relay and door opening module.

If it’s not working, you won’t be able to test the relay and door mechanism.

To prepare your Arduino for its working life as a security guard, set DEBUG to false and upload the code one last time. Remove the USB connection from your Arduino because you won’t need it anymore. Power will be supplied from your power transformer.