Hello. I bought this keypad from Amazon and it appears to not have any matrixing. There are 14 pins. The first pin is COM (ground), the second is "/", and the other 12 are for each individual button. Pins 3-6 are the first column (*, 7, 4, 1), pins 7-10 are the second column (0, 8, 5, 2), and pins 11-14 are the third column (#, 9, 6, 3). I have the button pins all connected in a line from I/O pins 2-13 on an Arduino UNO.
#define PIN_NUM1 2
#define PIN_NUM2 3
#define PIN_NUM3 4
#define PIN_NUM4 5
#define PIN_NUM5 6
#define PIN_NUM6 7
#define PIN_NUM7 8
#define PIN_NUM8 9
#define PIN_NUM9 10
#define PIN_STAR 11
#define PIN_NUM0 12
#define PIN_HASH 13
unsigned int pins[12] = {PIN_NUM1, PIN_NUM2, PIN_NUM3, PIN_NUM4, PIN_NUM5, PIN_NUM6, PIN_NUM7, PIN_NUM8, PIN_NUM9, PIN_STAR, PIN_NUM0, PIN_HASH};
unsigned char buttons[12] = {'*', '7', '4', '1', '0', '8', '5', '2', '#', '9', '6', '3'};
void setup()
{
// Set keypad pins
pinMode(PIN_NUM1, INPUT);
pinMode(PIN_NUM2, INPUT);
pinMode(PIN_NUM3, INPUT);
pinMode(PIN_NUM4, INPUT);
pinMode(PIN_NUM5, INPUT);
pinMode(PIN_NUM6, INPUT);
pinMode(PIN_NUM7, INPUT);
pinMode(PIN_NUM8, INPUT);
pinMode(PIN_NUM9, INPUT);
pinMode(PIN_STAR, INPUT);
pinMode(PIN_NUM0, INPUT);
pinMode(PIN_HASH, INPUT);
Serial.begin(9600);
}
void loop()
{
char key = getKey();
if (key) // Check for a valid key.
{
Serial.println(key);
}
}
char getKey()
{
for (int i = 0; i < 12; i++)
{
if (digitalRead(pins[i]) == LOW)
{
return buttons[i];
}
}
}
This is my code so far that I am testing. Whenever I compile, it just spams random-ish characters, usually astericks (*). I am trying to just make a functional keypad. Is there any way for me to make this functional or better?