Step 4: Program!
You can easily upload my code to your arduino and never look back but whats the fun in that? Let me explain the basic idea of how it works.
First of all, I did not want any external buttons/switches/etc and I did not want to reprogram the arduino every time I wanted to add a new card. Therefore I wanted to use only RFID to control the operation of the circuit as well as control over the door lock.
The program turns on the Blue LED to indicate it is ready to read a new card. When the card is read it decides if it is a valid card or not by comparing what it read in to a list of valid cards. If the user is valid, the arduino turns OFF the Blue LED and turns on the Green LED for 5 seconds. It also turns on another output high for 5 seconds. This output is connected to the TIP31A transistor and allows the tiny arduino to control a much larger 12v 300mA door lock without being damaged. After 5 seconds the door lock re-locks and the LED turns back to blue to wait for another card to be read. If the card is invalid then the LED changes to RED for a few seconds and back to Blue to wait for another card.
It is important that the door lock still work even if the arduino loses power overnight or is reset. Therefore all valid card ID's are stored in EEPROM memory. The ATMega168 has 512 Bytes of EEPROM memory. Each RFID card has a 5 Hex Byte serial number and a 1 Hex Byte Check sum that we can use to verify there were no errors in the transmission between the ID-20 and the arduino.
Valid cards are stored in the EEPROM by using the first Byte as a counter. For example, if there are 3 valid cards stored the first Byte in the EEPROM would be 3. EEPROM.read(0); = 3. Knowing this, and the fact that each ID is 5 Bytes long we know that 1-5 is card one, 6-10 is card 2 and 11-15 is card 3. We can make a loop that looks through the EEPROM 5 bytes at a time and tries to find the card that was read in by the reader.
But how can we add new cards to the EEPROM after the circuit is installed?? I have read in one of the RFID cards I have and hard coded it to be the Master RFID card. So even if the entire EEPROM is wiped the master card will still function. Whenever a card is read, it checks first to see if it is the Master card, if not, then it continues to see if it is a valid card or not. If the card is the master card we have the arduino go into a "programming mode" where it flashes RGB and waits for another valid tag to be read. The next tag that is read is added to the next free spot in the EEPROM and the counter is incremented 1 if the card does not already exist in the EEPROM memory. The reader then returns to normal mode and waits for a new card to be read.
Currently I have not programmed a way to delete a card as the reasons for deleting a card would most likely be it was lost or stolen. As this would most likely be used with 1-10 people the easiest thing to do would be to hard program a Master Erase card that will wipe all cards from the EEPROM then re add them all, which only takes a few seconds. I have added code to wipe the EEPROM but I have not implemented this feature yet. .
The code is attached in a text file along with a copy of the parts list.