Skip to content

Instantly share code, notes, and snippets.

@ddd1600
Created March 2, 2015 22:54
Show Gist options
  • Save ddd1600/dedd469b18c2b7513cec to your computer and use it in GitHub Desktop.
Save ddd1600/dedd469b18c2b7513cec to your computer and use it in GitHub Desktop.
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
//Key message
char msgs[5][15] = {"Right Key OK ",
"Up Key OK ",
"Down Key OK ",
"Left Key OK ",
"Select Key OK" };
int adc_key_val[5] ={30, 150, 360, 535, 760 };
int NUM_KEYS = 5;
int adc_key_in;
int key=-1;
int oldkey=-1;
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("Test keys:");
}
void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
adc_key_in = analogRead(0); // read the value from the sensor
key = get_key(adc_key_in); // convert into key press
if (key != oldkey) // if keypress is detected
{
delay(50); // wait for debounce time
adc_key_in = analogRead(0); // read the value from the sensor
key = get_key(adc_key_in); // convert into key press
if (key != oldkey)
{
oldkey = key;
if (key >=0)
{
lcd.print(msgs[key]);
}
}
}
}
// Convert ADC value to key number
int get_key(unsigned int input)
{ int k;
for (k = 0; k < NUM_KEYS; k++)
{
if (input < adc_key_val[k])
{ return k; }
}
if (k >= NUM_KEYS)
k = -1; // No valid key pressed
return k;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment