Advertisement
Online Test Banks
Score higher
See Online Test Banks
eLearning
Learning anything is easy
Browse Online Courses
Mobile Apps
Learning on the go
Explore Mobile Apps
Dummies Store
Shop for books and more
Start Shopping

How to Declare Variables to Code the Keypad for Your Keypad Entry System Arduino Project

Once you’ve got the keypad and display components in place on your breadboard, it’s time to load up the code to your Arduino. Take a moment to look at the code and then upload it to your board.

In the first section, before setup, you declare variables for the keypad and display.

const int numberOfDigits = 4; // The number of digits in the 7-segment display
const int numRows = 4;  // Number of rows in the keypad
const int numCols = 3;  // Number of columns in the keypad
const int debounceTime = 20; // Number of milliseconds for switch to become stable
const int doorOpenTime = 5000; // How long you want the door strike to remain open
const int strikePin = 9;  // The pin that actuates the relay for the door strike
const int slaveSelect = 10; // Pin used to enable the slave pin on the MAX72xx
char code[4]= {'1','2','3','4'}; // Set your code here
char codeBuffer[4]; // Stores the code that currently is being entered
boolean DEBUG=true; // Set to true to print status messages to the serial port
int keypressCount=0; // Counts how many times a key has been pressed
// The keyMap defines the character returned when its key is pressed
const char keyMap[numRows][numCols] = {
 { '1', '2', '3' },
 { '4', '5', '6' },
 { '7', '8', '9' },
 { '*', '0', '#' }
};
const int rowPins[numRows] = { 7, 4, 2, 5 }; // Keypad Rows 0 through 3
const int colPins[numCols] = { 6, 8 ,3 };  // Keypad Columns 0 through 2

The numberOfDigits variable sets your LED driver IC with the proper number of digits. numRows and NumCols store the number of rows and columns on your keypad, 4 and 3, respectively. You use these to look up which key is being pressed. The debounceTime ensures that the key being pressed is accurately detected. You set the amount of time the door will be held open with the doorOpenTime variable.

The strikePin variable defines which digital pin will be activated when the correct code is entered. This applies power to the relay, which closes to actuate your door lock mechanism. You might want to use a different name if you are using a different kind of mechanism, such as a magnetic door lock or a drop bolt lock.

slaveSelect defines a digital pin that you use to indicate you are sending data. The slave select (or “chip select”) pin (SS) is a command pin on your IC that connects the external pins to the internal circuitry.

In applications with several LED drivers that are all connected to your input wires, you want to be able to select whether or not it is enabled. The IC only responds to commands when its slave select mode pin is activated, which allows you to differentiate between this driver and other ones you might be using.

You are only using a single IC in this project, but you still need to let the chip know when you are going to send it data, and that’s what SS pin does.

Two char variables are used for the handling access code. The first one, code[], stores the access code digits. You can set this to be whatever you like. The second char, codeBuffer[], stores (or “buffers”) the keys that have been pressed. When these two match, presto! The Arduino activates strikePin. There’s no way to set the code from the keypad — you have to do it programmatically.

This program has a handy little debug feature built in. That’s what the next variable is for: a boolean called DEBUG. Recall that boolean variables can only be true or false. By setting this to true, you can use a conditional if statement to execute certain lines of code that won’t be executed if you set it to false.

For example, in this code you print a lot of variables on the serial port, to test that your keypad is working properly. But after your system is installed on your door, you don’t need to print anything. Rather than deleting all of those Serial.println() statements, you can simply go back to the DEBUG variable and set it to false.

The keypressCount stores how many digits have been pressed so far. When the fourth is pressed, the real action happens.

The char variable keyMap[][] is a two-dimensional array. A two-dimensional array is simply an array of arrays. Imagine a menu for an Italian restaurant. You might have a dozen menu items to choose from: breadsticks, soup, pizza, lasagna, salad, red wine, and so on.

You could store these as simply a single array, but you could also organize them by type: starters {breadsticks, soup}, main courses {pizza, lasagna, pasta}, drinks {water, red wine, white wine}. This way you can refer to items either individually or as collections. A single dimension array, as with your code[] array, stores elements (individual data items) like this:

Int anArray[] = { 0,1,2,3 };

A two-dimensional array stores elements like this:

Int anArray[][] = { {0,1,2,3}, {3,2,3,0}, {4,3,8,1}, {2,3,4,5} };

When you read from a two-dimensional array, the first value in the square brackets specifies which element you wish to read; the second value in brackets specifies the element within the element you specified. In working with arrays, the numbering system starts from zero. So, for example, in the two-dimensional array above, anArray[2][0] is holding the value 4.

The keyMap is simply used to store which digits are on the keypad. It’s literally a map of the keys.

When a key is pressed, you use the keyMap to identify which number is at that location, so you can compare it to your predetermined code.

The final two variables are arrays that store which of your Arduino’s digital pins the rows and columns of your keypad are connected to.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement
Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win an iPad Mini. Enter to win now!