Keypad Programming

?#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
{'1','4','7','*'},
{'2','5','8','0'},
{'3','6','9','#'},
{'A','B','C','D'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7,6}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(){
Serial.begin(9600);
}

void loop(){
char key = keypad.getKey();

if (key != NO_KEY){
Serial.println(key);
}
}

I have to initialised my keypad like this ><. Why ? Kinda weird. thought follow the orientation as in the keypad ? my 2 in the real keypad when I pressed, it gave me 4 in the serial monitor. Keypad problem ?

Thank you

This is the keypad I bought : http://www.ebay.com/itm/280780177082?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1497.l2649#ht_2936wt_1139

Connected as shown.

Your issue is an easy one to fix. The following code is where you tell the microcontroller what you want each button to mean using a data structure called an array.

char keys[ROWS][COLS] = {
{'1','4','7','*'},
{'2','5','8','0'},
{'3','6','9','#'},
{'A','B','C','D'}
};

Normally this part of the code should resemble the kepad being used. In that case you'll want change the characters in curly brackets so they are arranged the same way as they appear on your keypad.

Ya, I thought in the bracket the character should be the same as in the keypad itself ? Which means it should be declare like :

{'1','2','3','A'}, {'4','5','6','B'}, {'7','8','9','C'}, {'#','0','*,'D''} ??

Is it keypad problem ?

Vincent19: {'1','2','3','A'}, {'4','5','6','B'}, {'7','8','9','C'}, {'#','0','*,'D''} ??

As long as you make sure each character only has single quotes around it (unlike the * and D), this should correctly output the character values that are printed on each key.

Vincent19: Is it keypad problem ?

No, it just means the example you used was based on a keypad with a different labelling on the keys. You really can assign any character value you want to specific keys, but keeping it the similar to the existing key labels tends to be less confusing.

Yayaya. Okay get it then. No big deal.

Thank you :slight_smile:

http://arduino.cc/playground/Code/Keypad?action=sourceblock&num=1

For this coding, what does the code key!=NO KEY means ?

Thank you.

How am I able to make the display to print on a LCD.I want to do a simple calculator

Vincent19: http://arduino.cc/playground/Code/Keypad?action=sourceblock&num=1

For this coding, what does the code key!=NO KEY means ?

Thank you.

How am I able to make the display to print on a LCD.I want to do a simple calculator

if( variable != value ) means if the variable is not assigned the value. So NO_KEY (with underscore!!) represents a value that gets assigned to the variable if no key is pressed. because you only want to send the value of the key when a key is really pressed, you first check IF there has been a keypress.

so it says: if there has a key been pressed, print the value of the key being pressed.

so if it say no key is pressed, so it print nothing?

Vincent19: so if it say no key is pressed, so it print nothing?

Yes, NO_KEY is being used as a flag. In this case it's a flag to see if any of the keys have been pressed recently. This allows the example program to check if a key has been pressed and behave differently based on the results.