#include <LiquidCrystal.h>
int keyIn = 0; //Variable for keypad input
int keyVals [16] = {423, 454, 503, 562, 429, 459, 507, 565, 451, 480, 525, 579, 462, 487, 531, 585}; //Array to store keypad numerical values
char keys [16] = {'1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16'}; //Key characters corressponding to each numerical value
int range = 3; //Tolerance above or below the numerical value
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); //Assign LCD screen pins, as per LCD shield requirements
void setup()
{
lcd.begin(16,2); // columns, rows. use 16,2 for a 16x2 LCD, etc.
lcd.clear();
lcd.setCursor(0,0); //Display some text on LCD then clear it
lcd.print("Keypad");
lcd.setCursor(0,1);
lcd.print("One Input");
delay(2000);
lcd.clear();
Serial.begin(9600);
}
void loop()
{
keyIn = analogRead(A1); //Read in keypad input
Serial.println(keyIn);
String temp = ""; //Create a variable to store the key character
for (int i=0; i<=15; i++) //Run through the array of button values
{
if (keyIn >= keyVals[i]-range && keyIn <= keyVals[i]+range) //If the measured value is in the range of one key
{
temp = keys[i]; //Set temp equal to the character for that key
}
}
lcd.print("Button: ");
lcd.print(temp); //Display button character on the LCD
delay(1000); //Wait one second
lcd.clear(); //Clear the display
}