Hi, I´m trying to do a part of a program to input variables for more than one digit by a keypad to use that in a program...in other post, http://arduino.cc/forum/index.php/topic,57627.0.html ....I got it, but it sometimes get bad numbers......When it has 3 digits, it hasnt problem, but for example if I enter 456789 I get -1963......What could be the problem????
The code is the next:
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columnschar keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {6,7,8,9};
byte colPins[COLS] = {2,3,4,5};int v1 = 0;
int v2 = 0;
int v3 = 0;Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup()
{Serial.begin(9600);
}void loop()
{
v1 = GetNumber();
Serial.println ();
Serial.print ("v1");
Serial.print (v1);v2 = GetNumber();
Serial.println ();
Serial.print ("v2");
Serial.print (v2);
//v3 = GetNumber();}
int GetNumber()
{
int num = 0;
char key = kpd.getKey();
while(key != '#')
{
switch (key)
{
case NO_KEY:
break;case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
//lcd.print(key);
Serial.print(key);
num = num * 10 + (key - '0');
break;case '*':
num = 0;
//lcd.clear();
break;
}key = kpd.getKey();
}return num;
}
Thank you to everyone!