Hey, I’m new to Arduino and I’m trying to use a 12 keys keypad to serial print the key pressed.
I used one of the examples that came in the keypad.zip (library), but it always gives me an error.
I’m using the HelloKeypad.pde example:
/* @file HelloKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Demonstrates the simplest use of the matrix Keypad library.
|| #
*/
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {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){
Serial.println(key);
}
}
When I click upload or verify it always gives me this error:
HelloKeypad:22: error: ‘Keypad’ does not name a type
HelloKeypad.cpp: In function ‘void loop()’:
HelloKeypad:29: error: ‘keypad’ was not declared in this scope
Any ideas?!
(I’m using an Arduino Mega 2560, if it matters)
Thanks in advance!
After uploading I noticed the keys weren't right so I change the code to match the keypad.
Yet several keys are not working... 2, 4, 6, 8 and 0 are not showing up on serial.write (they only show up when I press a few keys...)
I’ve already replaced the files and it uploads just fine!
I think rows and columns are well defined, 'cause every other key it’s in the right place.
Rather than serial printing the wrong number, it just doesn’t print anything…
Yet if I combine 0 + # it prints 6!
The keys that don’t work are 2, 4, 6, 8 and 0 (in different columns and rows)
I suggest you to use a multimeter to check if you got 4 row by 3 column or 3 row by 4 column instead. The look is deceiving. Your symptom tells me you might have treated a column pin as a row pin.
you might want to do as suggested and use a multimeter to map out the pins (shows how in the tutorial)
i mapped mine out and this was the combination i got although the keypad is. Not all keypads that seem to be the same wiring are the same wiring
zarobhr:
you might want to do as suggested and use a multimeter to map out the pins (shows how in the tutorial)
i mapped mine out and this was the combination i got although the keypad is. Not all keypads that seem to be the same wiring are the same wiring
Yhap, but the mapping it's not the problem...
It seems that when I pressed keys 2, 4, 6, 8 and 0 the Arduino just doesn't recognized them.
I'm not sure how the keypad works internally but I think it's based on the Voltage Divider Principle, with voltage drops between the keys that can tell wich of the key is being pressed.
Is it possible that the resistors drop less voltage than the library it's configured for?
No, the matrix keypad does not work with voltage dividing principle. It instead works with columns touching rows. With a regular matrix keypad, where 123 are one row one, and 147 are on column 1, pressing 1 connects row 1 with column 1. If your column 1 is at GND and you sense row 1, you find row 1 to be LOW. If there is no pushing, then column 1 and row 1 are not connected so then row 1 will not read LOW (internally pulled to HIGH) and read HIGH. So the keypad library scans all columns and rows to determine which keys are pressed. Did that help?
zarobhr:
you might want to do as suggested and use a multimeter to map out the pins (shows how in the tutorial)
i mapped mine out and this was the combination i got although the keypad is. Not all keypads that seem to be the same wiring are the same wiring
Yhap, but the mapping it's not the problem...
It seems that when I pressed keys 2, 4, 6, 8 and 0 the Arduino just doesn't recognized them.
I'm not sure how the keypad works internally but I think it's based on the Voltage Divider Principle, with voltage drops between the keys that can tell wich of the key is being pressed.
Is it possible that the resistors drop less voltage than the library it's configured for?
matrix keypads do not work on voltage divider principle (it shorts the wires/pins)
think of it like a switch, when you press a key only 1 pair of the seven wires will make contact and it should be nearly a complete short, just becuase the odd numbers work and even dont doesnt mean its not a mapping problem. its very likely that it is a mapping or a wiring problem