// include the library code:
#include
// 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;
int brightness=250;
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 LCD Keypad 1");
}
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;
if (key==1)
{
brightness+=25;
if(brightness>250) brightness=0;
analogWrite(10,brightness);
show_brightness();
lcd.setCursor(0, 1); //x=0, line=2
lcd.print("brightness: ");
}
if(key==2)
{
brightness-=25;if(brightness<0) brightness=250;
analogWrite(10,brightness);
show_brightness();
}
}
}
}
int show_brightness(void)
{
int temp;
lcd.setCursor(12, 1); //x=12, line=2
temp=brightness/25;
if(temp>9)
lcd.print("100%");
else
{
lcd.print(temp%10+'0');
lcd.print("% ");
}
}
// 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;
}