You are on page 1of 2

#include <LiquidCrystal.

h>
LiquidCrystal lcd(12,13,8,9,10,11);
panel

// select the pins used on the LCD

// define some values used by the panel and buttons


int lcd_key
= 0;
int adc_key_in = 0;
#define
#define
#define
#define
#define
#define

btnRIGHT
btnUP
btnDOWN
btnLEFT
btnSELECT
btnNONE

0
1
2
3
4
5

int read_LCD_buttons(){
adc_key_in = analogRead(0);

// read the buttons


// read the value from the sensor

// my buttons when read are centered at these valies: 0, 144, 329, 504, 741
// we add approx 50 to those values and check to see if we are close
// We make this the 1st option for speed reasons since it will be the most l
ikely result
if (adc_key_in > 1000) return btnNONE;
//
if
if
if
if
if

For V1.1 us
(adc_key_in
(adc_key_in
(adc_key_in
(adc_key_in
(adc_key_in

this threshold
< 50) return
< 250) return
< 450) return
< 650) return
< 850) return

// For V1.0 comment the


/*
if (adc_key_in < 50)
if (adc_key_in < 195)
if (adc_key_in < 380)
if (adc_key_in < 555)
if (adc_key_in < 790)
*/

btnRIGHT;
btnUP;
btnDOWN;
btnLEFT;
btnSELECT;

other threshold and use the one below:


return
return
return
return
return

return btnNONE;

btnRIGHT;
btnUP;
btnDOWN;
btnLEFT;
btnSELECT;
// when all others fail, return this.

}
void setup(){
lcd.begin(16, 2);
// start the library
lcd.setCursor(0,0);
// set the LCD cursor position
lcd.print("Push the buttons"); // print a simple message on the LCD
}
void loop(){
lcd.setCursor(9,1);
s over
lcd.print(millis()/1000);

// move cursor to second line "1" and 9 space


// display seconds elapsed since power-up

lcd.setCursor(0,1);
lcd_key = read_LCD_buttons();

// move to the begining of the second line


// read the buttons

switch (lcd_key){

// depending on which button was pushed, we p

erform an action
case btnRIGHT:{
the screen
lcd.print("RIGHT ");
delay(1000);
break;
}
case btnLEFT:{
lcd.print("LEFT ");
he screen
delay(1000);
break;
}
case btnUP:{
lcd.print("UP
");
screen
delay(1000);
break;
}
case btnDOWN:{
lcd.print("DOWN ");
he screen
delay(1000);
break;
}
case btnSELECT:{
lcd.print("SELECT");
the screen
delay(1000);
break;
}
case btnNONE:{
lcd.print("NONE ");
break;
}
}
}

// push button "RIGHT" and show the word on

// push button "LEFT" and show the word on t

// push button "UP" and show the word on the

// push button "DOWN" and show the word on t

// push button "SELECT" and show the word on

// No action will show "None" on the screen

You might also like