Combining two keypad strokes to one variable

Hey guys and girls,

I want to make a switch statement with two digit cases such as case'10', case '11' , and so on. My input is coming from a keypad, however im unsure of how i would 'concatenate' the two char inputs.
ie
press 1 = 2
press 2 = 5

when the char variable is then read by the program in the switch statement technically it should run case'25' .

I have done a lot of reading but am still unsure hence the post.

Assuming that the smallest press is 0 and the largest press is 9, how about 10*press1+press2 ?

Of course, the cases will be values like 10, 11, and 25. There is no '10', '11', or '25'.

vaj4088:
Assuming that the smallest press is 0 and the largest press is 9, how about 10*press1+press2 ?

Of course, the cases will be values like 10, 11, and 25. There is no '10', '11', or '25'.

Yes but doesnt that then store the variable as a integer rather then a character? so when buttons like # * are pressed they will return strange values

If you want to build a case statement that represents every possible combination of two consecutively entered characters, that is going to be a very big case statement.
If, for example, the keypad has 16 keys, that is 256 possible entries.

6v6gt:
If you want to build a case statement that represents every possible combination of two consecutively entered characters, that is going to be a very big case statement.
If, for example, the keypad has 16 keys, that is 256 possible entries.

this is true, my keypad is only 4x3, and if i only have cases up to 25, im sure there is a way to return a error should a case other then that be attempted to be entered.

Any advice?

If only 25 cases are valid, that is not so bad. If you receive a combination which is not explicitly handled in your case statement, it falls into the default case and you can treat it as an error.

If you are receiving a single char from the keypad, I guess you can use the trick vaj4088 suggested but use 256 instead of 10 and use a long to store the result in. Use Serial.print to see what value each of the valid combinations of 2 digits yields and build your case statement accordingly.

If you have got any code already where you are reading form the keypad, then post it. There may be a nicer solution.

Do not confuse the characters '1', '2', etc., with the numeric values 1, 2, etc.
This is how it works:

char c = '1';        // c is '1' (a character)
int x = c;           // x is 49  (a number)
int y = (c - '0');   // y is  1  (a number)

char t = '2';                         // t is '2' (a character)
char u = '5';                         // u is '5' (a character)
int v = ((t - '0') * 10) + (u - '0'); // v is 25  (a number)

6v6gt:
If you have got any code already where you are reading form the keypad, then post it. There may be a nicer solution.

I have been mucking around with stuff buts its absolute rubbish and it doesnt work, ill post something now.

 *
 */
#include <Keypad.h>

const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the 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] = { 45, 47, 49, 51 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 39, 41, 43}; 

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

#define ledpin 13
  

void setup()
{
  pinMode(ledpin,OUTPUT);
  digitalWrite(ledpin, HIGH);
  Serial.begin(9600);
}

int i=0;
char Key;



char entryStr[3];



  
void loop(){
  char key = kpd.getKey();
 
 if (key){ 
   if (key == '*'){
   memset(entryStr, 0, sizeof(entryStr));  
   i=0;
   key=0;
   Serial.println(""); 
   Serial.println("Canceled"); 
   
   } else if (key != '#'){
     entryStr[i]= key;
     i++;
     Serial.print(key);
     }
   else {
   Serial.println(""); 
   i=0;
   key=0; 

   Serial.println(entryStr);
  
   }  
 }






    switch(entryStr)
    {

      case '10':

      Serial.print("ENTRY 10");
      Serial.println();
      break;


      case 12:

      Serial.print("Entry 12");
      Serial.println();
      break;


      default:
      Serial.print("NOTHING");
      Serial.println();





    }
    
 }
}

the problem with this code is that entryStr wont pass through the switch statement because it is not a integer.

odometer:
Do not confuse the characters ‘1’, ‘2’, etc., with the numeric values 1, 2, etc.
This is how it works:

char c = '1';        // c is '1' (a character)

int x = c;          // x is 49  (a number)
int y = (c - ‘0’);  // y is  1  (a number)

char t = ‘2’;                        // t is ‘2’ (a character)
char u = ‘5’;                        // u is ‘5’ (a character)
int v = ((t - ‘0’) * 10) + (u - ‘0’); // v is 25  (a number)

i get you, so what are you trying to say? that if i work out whether im using a character or a number im able to ‘concatenate’ so to speak… because through the keypad.h library when i return the keypad number 1, it returns 1 as a char, however if i store it as a integer it returns 49, which is ASCII im assuming?

frank1293:
i get you, so what are you trying to say? that if i work out whether im using a character or a number im able to 'concatenate' so to speak.. because through the keypad.h library when i return the keypad number 1, it returns 1 as a char, however if i store it as a integer it returns 49, which is ASCII im assuming?

Yes, it is ASCII.

What I am trying to do is show you how to convert characters, and pairs of characters, into the numeric values that it seems you are after.

What are you really trying to do?
http://xyproblem.info/

odometer:
Yes, it is ASCII.

What I am trying to do is show you how to convert characters, and pairs of characters, into the numeric values that it seems you are after.

What are you really trying to do?
http://xyproblem.info/

Okay... I have a 4x3 keypad and a range of other devices connected to my arduino mega 2560. I am trying to design a set of cases within a switch statement,.ie Case 10: case 11: case 12: to each perform seperate functions, HOWEVER the case requirements ("10"),("11") are based upon the KEYPAD ENTRY, HENCE my need to enter two digits and concatenate them so i can pass them through a switch statement.

HENCE my need to enter two digits and concatenate them so i can pass them through a switch statement.

Where are you stuck ?

its alright thanks heaps guys what odometer ended up saying worked! appreciate it