* key 1 = up
* key 2 = down
* key 3 = left
* key 4 = select
*/
#include <LiquidCrystal.h>
//lcd pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
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() {
lcd.begin(16,2);
lcd.home();
lcd.clear();
}
void loop() {
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;
switch (key)
{
case 0:
// righ
break;
case 1:
// up
break;
case 2:
// down
break;
case 3:
// left
break;
case 4:
// select
break;
}
}
}
}
// 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;
}