3x4 keypad

Hi all,

I am working on a project with a hacked phone that has a 3x4 keypad. I was able to figure out the wiring and how it works with the keypad library. My question is that– currently I can get to display numbers, 1 to 9 on the serial monitor but what if I want to display 2 digit numbers? I am trying to have different numbers pressed (values between 1- 52) to pull up different processing sketches.

#include <Keypad.h>

//char* num[] = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"};

char* num01 = "01";
char* num02 = "02";
char* num03 = "03";
char* num04 = "04";
char* num05 = "05";
char* num06 = "06";
char* num07 = "07";
char* num08 = "08";
char* num09 = "09";
char* num10 = "10";

char* num11 = "11";
char* num12 = "12";

char* num21 = "21";
char* num22 = "22";

int count = 0;

const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// keymap
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 9, 8, 7, 6 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 10, 11, 12 }; 

// Create the Keypad
Keypad kpd = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS );

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

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

if (key == '*' || key == '#')
{
count = 0;
}

//if (key == num[count])
//{
//count ++;
//}
//if (count == 2)
//{
//Serial.println (num);
//count = 0;
//}

//////

if (key == num01[count])
{
count ++;
}
if (count == 2)
{
Serial.println ("01");  
count = 0;
}

//////

if (key == num02[count])
{
count ++;
}
if (count == 2)
{
Serial.println ("02");  
count = 0;
}

//////


if (key == num03[count])
{
count ++;
}
if (count == 2)
{
Serial.println ("03");  
count = 0;
}

//////


if (key == num04[count])
{
count ++;
}
if (count == 2)
{
Serial.println ("04");  
count = 0;
}

//////


if (key == num05[count])
{
count ++;
}
if (count == 2)
{
Serial.println ("05");  
count = 0;
}

//////


if (key == num06[count])
{
count ++;
}
if (count == 2)
{
Serial.println ("06");  
count = 0;
}

//////


if (key == num07[count])
{
count ++;
}
if (count == 2)
{
Serial.println ("07");  
count = 0;
}

//////

if (key == num08[count])
{
count ++;
}
if (count == 2)
{
Serial.println ("08");  
count = 0;
}

//////

if (key == num09[count])
{
count ++;
}
if (count == 2)
{
Serial.println ("09");  
count = 0;
}

//////

if (key == num10[count])
{
count ++;
}
if (count == 2)
{
Serial.println ("10");  
count = 0;
}

}

With the current code that I have, whenever I try to add anymore numbers, for example 10, 11, 21, etc the serial monitor would print out different numbers…

I am hoping it is just something simple.

cheers,

How are you going to define a group of key presses to get a number greater than 9? Are every 2 keys going to combine (so you have to input 1 as 01, 2 as 02 etc) , is there a timeout after which 1 character is sufficient or do you intend to press one of the special keys to indicate the end of digits?

Instead of trying to match combinations against long lists I suggest something like (this will require 2 keys every time eg 1 will have to be entered as 01).

int keys[2];

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

  if (key == '*' || key == '#')
  {
    count = 0;
  } else if (key>='0' && key <='9' ) {

    keys[count]=key-'0'; // simple convert of char to int
    count++;

    if (count >1 ) {

      serial.println(keys[0]*10+keys[1]);
      count=0;
    };
  };
};

UPDATED to properly handle case where no key is pressed.

Thanks for the reply rw950431.

The code you provided only outputs -528 in the serial monitor somehow...
but I was able to combine what I had with your suggestions and now it works!

int count = 0;
int keypresses[2];

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

  if (key == '*' || key == '#'){
    count = 0;
  }

  if (key) {
    keypresses[count] = key-'0';
    count++;
    if (count == 2) {
      Serial.println(keypresses[0]*10+keypresses[1]);
      count = 0;
    }
  }
}

Doh!.. I realized after I posted that getKey() was non-blocking and my code didn't handle the case where no key was pressed. Glad you figured it out. :slight_smile: