Multiple Digit input from a keypad

Hello, I am new to programming and new to the Arduino community and was hoping one of you could provide some guidance. Basically I am trying to program a motor that will turn for varying amounts of time that is specified by the input from a 3X4 keypad. I figured out how to set up the keypad and can correctly display the button I press but am struggling to get the it to read 2-digit numbers (like 15). The only numbers I need it to read are between 10-25. I have tried to make case statements which haven't worked and do not really know how else I can get it to read a 2 digit number. Any advice would be greatly appreciated! Thank you

Show me the code you have that reads and prints the keys and I'll consider writing some code that will combine the inputs into the values you're looking for.

BTW Don't forget to surround your code with [code] and [/code] tags when posting.

void Keypad_func()
{
char key = kpd.getKey();
     // Serial.println(key);
           if(key = 1)
              {
                delay(1000);
               // Keypad_1();                            
              char key = kpd.getKey();
                         switch(key)
                            {
                             case '0':
                             Length = 10;
                             break;
                             
                             case '1':
                             Length = 11;
                             break;
                             
                             case '2':
                             Length = 12;
                             break;
                             }
              }
             else if(key = 2)
              {
              delay(1000);
                  char key = kpd.getKey();
                 
                                switch(key)
                                    {
                                     case '0':
                                     Length = 20;
                                     break;
                                     case '1':
                                     Length = 21;
                                     break;
                                    } 
                 
                    /*  if(key = 0)
                         {
                            Length = 20;
                         }
                         else if(key = 1)
                         {
                             Length = 21;
                         }
                         else if(key = 2)
                         {
                             Length = 22;
                         }     
                         else if(key = 3)
                         {
                             Length = 23;
                         }
                         else if(key = 4)
                         {
                             Length = 24;
                         }                         
                         else if(key = 5)
                         {
                             Length = 25;
                         }                         
                         */
              }                                                  
          
         else
         {
          Keypad_func();
         }
}  






 /*

void Keypad_1()  
{
 int key = kpd.getKey();
 if (key != NO_KEY)
 {
                         if(key = 0)
                         {
                             Length = 10;
                         }
                         else if(key = 1)
                         {
                             Length = 11;
                         }
                         else if(key = 2)
                         {
                             Length = 12;
                         }     
                         else if(key = 3)
                         {
                             Length = 13;
                         }
                         else if(key = 4)
                         {
                             Length = 14;
                         }                         
                         else if(key = 5)
                         {
                             Length = 15;
                         }                         
                         else if(key = 6)
                         {
                             Length = 16;
                         }                         
                         else if(key = 7)
                         {
                             Length = 17;
                         } 
                         else if(key = 8)
                         {
                             Length = 18;
                         }
                         else if(key = 9)
                         {
                             Length = 19;
                         }        
 }
 }
 else
 {
 Keypad_1();
 }
  
}
  
  */
]

Not sure what you meant by the to post code, but here is what i have so far. I ahve been trying different things so theses probably some unnecessary thing but I think i commented it all out.

Thank you

Agh. If you check the answer you’ve just given you may understand the code tag issue. (but it was supposed to be your code that goes into that box)

byte value=0;

void loop() {
char key = kpd.getKey();
 if (key != NO_KEY)
   {
    if ( (key >= '0') && (key<= '9') )
      {
       value = value *10;
       value = value + key -'0';
      }
    if ( key == '#' )
     {
      //do stuff with value
      //for demonstration purposes, for now we'll just print it
      Serial.println(value);

      value=0;//Now reset ready for next input
     } 
   }
}
void Keypad_func()
{
  
char key = kpd.getKey();
 if (key != NO_KEY)
   {
    if ( (key >= '0') && (key<= '9') )
      {
       Length = Length *10;
       Length = Length + key -'0';
      }
    if ( key == '#')
     {
      //do stuff with value
      //for demonstration purposes, for now we'll just print it
      Serial.println(Length);

      Length=0;//Now reset ready for next input
     } 
   }
  else
     {
      Keypad_func();
     }
}]

I changed some of the variable names and it was working nicely but once I hit a key it leaves the function

jjm10n: once I hit a key it leaves the function

This is why I put my demo in the loop function. I don't think there's much point having yet another function involved. You merely want to keep the global variable up to date.

Sorry I think I phased my last message wrong. For example if I want to enter 20, once I hit the '2' on the keypad it reads the value as '2' running the rest of the code as if the input was '2' and does not give me a chance to hit the '0' to make '20'

Would it help if I posted my entire program? Just didnt want to confuse you

jjm10n: For example if I want to enter 20, once I hit the '2' on the keypad it reads the value as '2' running the rest of the code as if the input was '2' and does not give me a chance to hit the '0' to make '20'

When you hit the 2 the rest of your program should NOT do anything with it. This is why, in my example, It does nothing with the value until some other key (I've used '#') is pressed.

Mr. Ken F,

Could you please explain what happens in this part of the program with an example if possible:

if ( (key >= ‘0’) && (key<= ‘9’) )
{
value = value *10;
value = value + key -‘0’;

The key values are ascii characters (for numbers they range from 0x30 to 0x39). Subtracting 0x30 ('0') from the key value gives you the numeric value. E.g. when you press '5', the ascii value is 0x35 and subtracting 0x30 gives the numeric value 5.

An example is probably the easiest to explain why the code multiplies by 10. Imagine you press '5', '3', '1'

key     value after multiplication  value after addition
'5'     0 (0 * 10)                  5 (0 + 5)
'3'     50 (5 * 10)                 53 (50 +3)
'1'     530 (53 * 10)               531 (530 + 1)

thanx a lot..! I got the concept..! Its same as subtracting 48 from the key, I guess. Also, I need to give two two-digit numbers as input to the arduino through keypad. Giving a delay and reading again after saving the past number is not working.

deepa_v93: thanx a lot..! I got the concept..! Its same as subtracting 48 from the key, I guess. Also, I need to give two two-digit numbers as input to the arduino through keypad. Giving a delay and reading again after saving the past number is not working.

It's indeed the same as subtracting 48 or 0x30. Show your code :) Please use code tags.

[code] your code here [/code] will result in

 your code here
void loop() {
char key = keypad.getKey();
 if (key != NO_KEY)
   {
    if ( (key >= '0') && (key<= '9') )
      {
       value = value *10;
       value = value + key -'0'; 
      }
    if (key='#')
     {
     
      byte value1=value;
      Serial.println("value:");
      Serial.println(value);
      Serial.println("value1:");
      Serial.println(value1);
      delay(1000);
      value=0;
     value1=0;
     } 
   }
}

if (key = '#') does not do what you might think it does. You need to use a double equal sign for a compare.

yeah. Sorry. I dint copy the code I typed it. It was a typo error. There are no errors while compiling. I just want to know what changes to make in the code given above so that arduino accepts and processes two numeric values one after the other using a single keypad.

Hello, I have got the same problem when i was working on a project where i have to enter amount from the keypad, so i worked it out by the following way

#include <Keypad.h>
char digit[4],num[10]={‘h’,‘e’,‘l’,‘l’,‘o’};
int count=0,f[4];
char keypressed;
String temppassword="";
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{‘1’,‘2’,‘3’,‘A’},
{‘4’,‘5’,‘6’,‘B’},
{‘7’,‘8’,‘9’,‘C’},
{’*’,‘0’,’#’,‘D’}
};
byte rowPins[ROWS] = {D0, D1, D2, D3}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {D4, D5, D6, D7}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad myKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

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

void loop(){
Serial.print(“Enter Amount:”);
do{keypressed = myKeypad.getKey();
if (keypressed){
Serial.print(keypressed);
if (keypressed != NO_KEY)
{

// temppassword="";

if(keypressed == ‘0’ || keypressed == ‘1’ || keypressed == ‘2’ || keypressed == ‘3’ ||
keypressed == ‘4’ || keypressed == ‘5’ || keypressed == ‘6’ || keypressed == ‘7’ ||
keypressed == ‘8’ || keypressed == ‘9’ )
{
//Serial.println(“hello”);
temppassword += keypressed;
}

for(int i=0;i<4;i++)
{

f_=temppassword*;*_

* }*

_ if ( keypressed == ‘’&& f[0]!=0)_
_
{_
_
Serial.println("");_
_
Serial.print(“Amount is:”);Serial.println(temppassword);_
_
}_
_
else if(keypressed==‘A’ || keypressed==‘B’ || keypressed==‘C’ || f[0]==0)_
_
{_
_
Serial.println(" ");_
_
Serial.println(“Invalid”);_
_
}_
_
}_
_
} _
_
yield();_
_ }while(keypressed!=’’||keypressed==‘A’ || keypressed==‘B’ || keypressed==‘C’ || keypressed==‘D’ );_

//end of do while loop
temppassword="";
}