/* Arduino Projects for Dummies * by Brock Craft * * Chapter 8: Building a Keypad Entry System * A system that uses a numeric keypad to actuate a solenoid * Code is dsiplayed on a LED 7-segment common cathode display * with a MAX72xx display driver IC * * v0.1 30.04.2013 * Adapted from keypad reading technique by Michael Margolis */ 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]; // A buffer to store the code that is being entered boolean DEBUG=true; // Set thus to true to pring out status messages to the serial port int keypressCount=0; // A variable to count how many times a key has been pressed // The keyMap defines the character returned when the corresponding key is pressed const char keyMap[numRows][numCols] = { { '1', '2', '3' }, { '4', '5', '6' }, { '7', '8', '9' }, { '*', '0', '#' } }; // This array defines the Arduino Digital pins used for rows and columns // for the keypad from Sparkfun (US) and Rapid (UK) const int rowPins[numRows] = { 7, 2, 3, 5 }; // Digital Pins for Keypad Rows 0 through 3 const int colPins[numCols] = { 6, 8 ,4 }; // Digital Pins for Keypad Columns 0 through 2 void setup() { if (DEBUG){Serial.begin(9600);} // If in DEBUG mode, start the serial port // Prepare the MAX72xx to display 7-segment data (see datasheet) // Format: sendCommand(command, value) SPI.begin(); // initialize SPI interface to talk to the MAX72xx pinMode(slaveSelect, OUTPUT); // Set the SPI slave pin for output sendCommand(12,1); // Set to normal mode (default is shutdown mode); sendCommand(15,0); // Display test off sendCommand(10,8); // Set to medium brightness (range is 0-15) sendCommand(11, numberOfDigits); // 7221 digit scan limit command sendCommand(9,255); // Set "Decode mode" true. Provides digits: 0-9, H, E, L, P, -, blank // Prepare the I/O pins for the keypad and the relay pinMode(strikePin,OUTPUT); for (int row = 0; row < numRows; row++) { pinMode(rowPins[row],INPUT); // Set row pins as input digitalWrite(rowPins[row],HIGH); // Turn on pull-up resistors on the processor } for (int column = 0; column < numCols; column++) { pinMode(colPins[column],OUTPUT); // Set column pins as outputs for writing digitalWrite(colPins[column],HIGH); // Make all columns inactive } // Clear the display (helps when debugging) clearDisplay(); } void loop() { char key = getKey(); // See if a key has been pressed codeBuffer[keypressCount]=key; // Add the key just pressed to the code Buffer if( key != 0) { // if the character is not 0 then sendCommand(keypressCount+1, key); // Sends the character to the display // it's a valid key press if (DEBUG){ // Used to print values for testing and debugging Serial.print("Digit "); Serial.println(keypressCount); Serial.print("Got key: "); Serial.println(key); Serial.print("Buffer: "); Serial.print(codeBuffer[0]); // Print the contents of the code buffer Serial.print(codeBuffer[1]); Serial.print(codeBuffer[2]); Serial.println(codeBuffer[3]); Serial.print("Code: "); // Print the contents of the code array Serial.print(code[0]); Serial.print(code[1]); Serial.print(code[2]); Serial.println(code[3]); } keypressCount++; // Incrmement the number of keys that have been pressed if (keypressCount==4){ // If four keys have been pressed, test whether code matches delay(500); // wait a moment so that the forth digit can be seen on the display if (memcmp(codeBuffer, code,4)==0) { // Compare to see if there is a match if(DEBUG){Serial.println("MATCH!");} // If in DEBUG mode, report this unlock(); // If there is a match, unlock } clearDisplay(); // Now clear the display memset(codeBuffer, 0, 4); // Explicitly clear out the code buffer array keypressCount=0; } } } // Returns the key pressed, or 0 if no key is pressed char getKey() { char key = 0; // 0 indicates no key pressed for(int column = 0; column < numCols; column++) { digitalWrite(colPins[column],LOW); // Activate the current column for(int row = 0; row < numRows; row++) // Scan all rows for a key press { if(digitalRead(rowPins[row]) == LOW) // If a key is pressed... { delay(debounceTime); // Debounce while(digitalRead(rowPins[row]) == LOW) ; // Wait for key to be released key = keyMap[row][column]; // Store which key was pressed } } digitalWrite(colPins[column],HIGH); // De-activate the current column } return key; // Return the key that pressed (or 0 if none) } void sendCommand(int command, unsigned char value) { digitalWrite(slaveSelect,LOW); // Chip select is active when pin is LOW SPI.transfer(command); // Send the First byte of data to the MAX72xx SPI.transfer(value); // Send the Second byte of data digitalWrite(slaveSelect,HIGH); // Release the chip - signals end of data transfer } void clearDisplay(){ sendCommand(1, '_'); // Clears out all the digits. You can also use underscore 'o' sendCommand(2, '_'); sendCommand(3, '_'); sendCommand(4, '_'); } void unlock(){ if(DEBUG){Serial.println("Unlocking Door.");} // Display welcome message "HI" // Other characters available are: j,k,l,m,n,o = -,e,h,l,p,blank sendCommand(1, 'o'); // Blank sendCommand(2, 'l'); // Letter H sendCommand(3, '1'); // Letter I sendCommand(4, 'o'); // Blank digitalWrite(strikePin,HIGH); // Activate the relay (unlock the door strike) delay(doorOpenTime); // Gives you time to open up the door digitalWrite(strikePin,LOW); // Deactivate the relay (and lock door again) }