Tuesday, 3 March 2015

Matrix Keypad Fun

 I was shopping online at http://proto-pic.co.uk/ and I came across a cheap 12 button keypad link here (http://proto-pic.co.uk/keypad-12-button/ ). At £3.10 I thought it look like a fun project.


The keypad is a Matrix keypad so 12 buttons are control by 7 pins. This made it more tricky to wire up but with the help of other tutorials like this one(http://bildr.org/2011/05/arduino-keypad/) I got it to work. I tried wiring it up as described but the matrix didn't work, it reported I was pushing more than 1 button at a time.Though a process of trial and error I found the problem,  tutorials say you have to power 3 or 4 pins but on mine I just had to power pin 1 on the keypad. Once I had it working I rewired it simpler so that pin 1 on the keypad went to Digital pin 2 on the arduino and pin 2 went to Digital pin 3 and 3-4, 4-5, 5-6 and so on. I altered the code to match.



Diagram of wiring





I plugged mine into a breadboard to make it easy to wire up the power. The 5v has to go through a resistor to make it safe for the digital pins. I used a 10k but that may be overkill. Yours may be different to wire up, just remember to change the pins on the code to make it work .




Code



The code I based it on was the  <Keypad.h> library found here (http://playground.arduino.cc/Code/Keypad) It to care of the hard work so thanks should go to its creators.

I have highlight code references in blue to make it easier to read. 

Now using Gist to host my code.

First a simple one. 1 button activator




This code reads the key being pressed with the command  keypad.getKey() and store it as a char variable called (key). Remember Char's are stored using apostrophes '2' unlike Strings that use quotation marks like "fire". So to trigger stuff with one button you just have to compare two Chars as in the example above.

Passwords Code

 



I made this code because I wanted more than 1 password and the password library only allowed 1 so I wrote my own code. I have adding lots of comments to explain.The code allows passwords up to 5 buttons long but it can be easy extended by adding more else if clauses .

It saves the key to a string, the first key is saved as CHARONE. The Count then adds 1 so that the 2nd key pressed is saved as CHARTWO and so on. The strings are combined by the command test = String(CHARONE + CHARTWO);. To test the password you just have to compare 2 strings, test and the password you want to compare. Using this with and else if statement you can have as many password as you like.

The special key of  '*' is used to check if the entered keys match the password. It clears the strings ready for the next attempt

The special key of '#'  is used to clear the password attempt using cleartest();. This clears the the strings. Using it as a separate command keeps the code tidy.

Other Projects


This is just the start of what you can do with a keypad. Other uses that I will post tutorials include Texting interface using the keypad, like old phones before touch screens. A mastermind game using Processing.

But they are not quite ready yet.

Any Question or comments are welcome in the box below