OK, so now you enter a single digit, turn it into a number and use that number just like any other variable in your sketch. Before moving on to accepting and using multiple digit numbers let's deal with something that might occur to you. Why not declare the keys array like this
char keys[ROWS][COLS] =
{
{1, 2, 3, 'A'},
{4, 5, 6, 'B'},
{7, 8, 9, 'C'},
{'*', 0, '#', 'D'}
};
then there would be no need to manipulate the input to turn it into a number. Try this sketch and you will see that there is a problem when you run it
#include <Keypad.h>
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] =
{
{1, 2, 3, 'A'},
{4, 5, 6, 'B'},
{7, 8, 9, 'C'},
{'*', 0, '#', 'D'}
};
byte colPins[COLS] = {10, 11, 12, A4}; //column pins
byte rowPins[ROWS] = {2, 4, 7, 8}; //row pins
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup()
{
Serial.begin(115200);
}
void loop()
{
byte key = keypad.getKey();
if (key >= 0 && key <= 9) //only act on numeric keys
{
Serial.print("key : ");
Serial.println(key);
Serial.print("key * 10 : ");
Serial.println(key * 10);
Serial.println();
}
}
The problem is that the author of the Keypad library chose to return a zero from the getKey() function if no key is pressed and, of course, the sketch cannot tell the difference between the zero returned for no key and the zero returned when you press zero. The library could, of course, be modified to avoid the problem but that is for another day and another thread (maybe)
Another way round it would be to use the waitForKey() function, but that halts the sketch until a key is pressed, so if it is required to do something else whilst waiting, such as timing out if the user does not enter something after a period, then it is impossible. For that reason, the examples in this thread will use getKey()
So, how to read a multi digit input and put it into a numeric variable ? Let's start by restructuring a previous example like this
#include <Keypad.h>
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] =
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte colPins[COLS] = {10, 11, 12, A4}; //column pins
byte rowPins[ROWS] = {2, 4, 7, 8}; //row pins
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
char key;
byte keyAsANumber;
boolean entryComplete;
void setup()
{
Serial.begin(115200);
}
void loop()
{
readKeypad();
if (entryComplete)
{
Serial.println(keyAsANumber);
}
}
void readKeypad()
{
key = keypad.getKey();
if (key >= '0' && key <= '9') //only act on numeric keys
{
keyAsANumber = key - 48;
entryComplete = true;
}
else
{
entryComplete = false;
}
}
Note the changes :
The actual reading of a key and dealing with it has been turned into a function. Once it is debugged we can call it as and when required and could, if we wanted to, make it available to other sketches
Variables have been declared as global to make them available throughout the sketch (this is not the place to discuss the merits of local variables over global variables)
A new variable (entryComplete) has been introduced that flags what its name suggests. More on this later
Most of the debugging Serial.print()s have been removed now that we know what is going on
Next, the big change, inputting multi digit numbers