/* Arduino Projects for Dummies * by Brock Craft * * Chapter 8: Building a Keypad Entry System + RFID * 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 05.05.2013 * Adapted from keypad reading technique by Michael Margolis */ #include 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 const int RFIDResetPin = 12; // This pin tells the reader to read again // Make a list of your RFID tags here. // If you don't know them, leave at least one in place as starter // a starter ID and delete it later. Otherwise the code will break. // (You always have to have something to compare the read tags to.) char tag1[13] = "4B00DDBF9FB6"; // Your scanned tag ID goes here char tag2[13] = "010203AABBCC"; // these are example Iag IDs only char tag3[13] = "010203AABBDD"; // these are example Iag IDs only // etc. for more tags 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(); // Set up the RFID reader pinMode(RFIDResetPin, OUTPUT); // Tells the reader to start again digitalWrite(RFIDResetPin, HIGH); // Make it ready to read Serial.println("Ready."); // Advise the consoe we are ready. } 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; } } // NOW SEE IF THE RFID READER HAS DETECTED A TAG Serial.println("Looking for a tag..."); // Say what we are currently doing char tagString[13]; // Create an array to hold the tag we are reading int index = 0; // A utility counter to track where we are in tagstring[] boolean reading = false; // Stores whether we have a reading while(Serial.available()){ // If there is a serial connection... int readByte = Serial.read(); // Read next available byte if(readByte == 2) reading = true; // 2 indicates the begining ofa tag if(readByte == 3) reading = false; // 3 indicates the end of tag // If there is a reading and it's not the beginning or end, store it if(reading && readByte != 2 && readByte != 10 && readByte != 13){ tagString[index] = readByte; // Store the tag at the index point index ++; // increment where we are in the storage array } } checkTag(tagString); // Check if it is a match clearTag(tagString); // Clear the char array of all values resetReader(); // Reset the RFID reader } // 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) } void checkTag(char tag[]){ // Compare this tag to the stored tags if(strlen(tag) == 0) return; // If there's nothing here, do nothing if(compareTag(tag, tag1)){ // If read tag matches tag1, do this unlock(); } else if(compareTag(tag, tag2)){ // If read tag matches tag2, do this unlock(); } else if(compareTag(tag, tag3)){ // If read tag matches tag3, do this unlock(); } else { // If it doesn't match the list of tags... Serial.println("New tag found: "); Serial.println(tag); // Print the tag number of this new tag delay(5000); // Wait 5 seconds, so we can make note of it } } boolean compareTag(char one[], char two[]){ // Compare our two chars to see whether the tag // we just read is the same as the stored tag if(strlen(one) == 0) return false; // If there's nothing here, do nothing for(int i = 0; i < 12; i++){ if(one[i] != two[i]) return false; // If the two tags are are not the same, return no match } Serial.println("Valid tag found!"); return true; // Otherwise, return that there is a match! } void clearTag(char one[]){ // Clear the tag reading char array by filling it with ASCII 0 // If not null, it could indicate another tag read for(int i = 0; i < strlen(one); i++){ one[i] = 0; } } void resetReader(){ // Toggle the reset pin so the RFID reader will read again digitalWrite(RFIDResetPin, LOW); digitalWrite(RFIDResetPin, HIGH); delay(150); }