Keypad code adapted from http://howtomechatronics.com/projects/arduino-security-alarm-system-project/
I modified it to accept 1, 2, 3 or 4 digits and return that value so it can be used in a variable for driving servo motors to a specific feedback voltage.
Hope this helps someone!
Plank
char keypressed;
int tempFeedback = 0;
int value0;
int value1;
int value2;
int value3;
long val;
long readVal;
bool activated;
const byte ROWS = 4; //--(four rows)--
const byte COLS = 4; //--(four columns)--
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'}, //--(define the symbols on the buttons of the keypad)--
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {26, 27, 28, 29};
byte colPins[COLS] = {30, 31, 32, 33};
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); //--(Initialize an instance of class NewKeypad)--
/*
_ _______ _____ _ ___
| |/ / __\ \ / / _ \/_\ | \
| ' <| _| \ V /| _/ _ \| |) |
|_|\_\___| |_| |_|/_/ \_\___/
int atoi(const char* string); Convert string to int
long atol(const char* string); Convert string to long
double atof(const char* string); Convert string to double
long double atolf(const char* string); Convert string to long double
*/
//--(keypad)--
int GetNumber() //--(Function for 1 to 4 digit number with return value int)--
{
int k=10; //--(local variable)--
static char buffer1[4]; //--(Used to store char string variable for atoi conversion)--
tempFeedback = 0; //--(Reset any previous held value for variable to zero)--
activated = true; //--(flag variable for while loop below)--
lcd.clear();
lcd.setCursor(0,0);
lcd.print("*** Keypad Enter ***");
lcd.setCursor(0,1);
lcd.print("Feedback>");
lcd.setCursor(0, 2);
lcd.print("# = Restart");
lcd.setCursor(0,3);
lcd.print("* = Enter");
while(activated)
{
keypressed = kpd.getKey();
if (keypressed != NO_KEY)
{
if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
keypressed == '8' || keypressed == '9' )
{
tempFeedback += keypressed;
lcd.setCursor(k,1); //--(Place digit at display location i)--
lcd.print(keypressed); //--(Print digit)--
buffer1[i] = keypressed; //--(Put keypressed string in buffer1 for processing conversion to float)--
if (k == 10)
{
value0 = atoi(buffer1); //--(Convert string to int and put result in value0)--
}
if (k == 11)
{
value1 = atoi(buffer1); //--(Convert string to int and put result in value1)--
}
if (k == 12)
{
value2 = atoi(buffer1); //--(Convert string to int and put result in value2)--
}
if (k == 13)
{
value3 = atoi(buffer1); //--(Convert string to int and put result in value3)--
}
k++;
}
}
if (k > 14 || keypressed == '#') //--(Reset keypad entry)--
{
tempFeedback = 0;
k=10;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("*** Keypad Enter ***");
lcd.setCursor(0,1);
lcd.print("Feedback>");
lcd.setCursor(0, 2);
lcd.print("# = Restart");
lcd.setCursor(0,3);
lcd.print("* = Enter");
}
if ( keypressed == '*') //--(ACTIVATE SERVO TO GO TO temFeedback position)--
{
activated = false; //--(flag variable to stop while loop and continue)--
delay(4000);
if (k == 14)
{
tempFeedback = value0 * 1000 + value1 * 100 + value2 * 10 + value3; //--(Construct complete 4 digit number)--
}
else if (k == 13)
{
tempFeedback = value0 * 100 + value1 * 10 + value2; //--(Construct complete 3 digit number)--
}
else if (k == 12)
{
tempFeedback = value0 * 10 + value1; //--(Construct complete 2 digit number)--
}
else if (k == 11)
{
tempFeedback = value0; //--(Construct complete 1 digit number)--
}
}
}
return tempFeedback; //--(This variable is the product of this function and is returned to the function call statement)--
}