Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal.h>
- /*******************************************************
- This program allows the user to create a series of
- messages on an LCD keypad screen that will display when
- a particular key is pressed.
- The only values that need to be changed are:
- - upScreen, downScreen, leftScreen, rightScreen,
- selectScreen and noneScreen
- - noneScreen is what will display when no buttons are
- being pressed.
- Messages must be exactly 16 characters long and contain
- 2 lines. You must use spaces to clear old messages!
- Modified from code at: https://www.dfrobot.com/wiki/index.php/Arduino_LCD_KeyPad_Shield_(SKU:_DFR0009)
- ********************************************************/
- // select the pins used on the LCD panel
- LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
- // define some values used by the panel and buttons
- int lcd_key = 0;
- int adc_key_in = 0;
- #define btnRIGHT 0
- #define btnUP 1
- #define btnDOWN 2
- #define btnLEFT 3
- #define btnSELECT 4
- #define btnNONE 5
- // this is where you put your messages, using this format:
- // "FIRST LINE------", "SECOND LINE-----"
- // 1234567890123456 1234567890123456
- char* upScreen[] = { "You are pressing", "the UP button" }; // displays when UP is pressed
- char* downScreen[] = { "You are pressing", "the DOWN button " }; // displays when DOWN is pressed
- char* leftScreen[] = { "You are pressing", "the LEFT button " }; // displays when LEFT is pressed
- char* rightScreen[] = { "You are pressing", "the RIGHT button" }; // displays when RIGHT is pressed
- char* selectScreen[] = { "You are pressing", " SELECT button" }; // displays when SELECT is pressed
- char* noneScreen[] = { "Try pressing one", "of the buttons! " }; // displays when SELECT is pressed
- // read the buttons
- int read_LCD_buttons()
- {
- adc_key_in = analogRead(0); // read the value from the sensor
- if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
- int rightButtonValue = 0;
- int upButtonValue = 143;
- int downButtonValue = 328;
- int leftButtonValue = 504;
- int selectButtonValue = 740;
- // if (adc_key_in == rightButtonValue) return btnRIGHT;
- if (adc_key_in >= (rightButtonValue - 10) && adc_key_in <= (rightButtonValue + 10)) return btnRIGHT;
- if (adc_key_in >= (upButtonValue - 10) && adc_key_in <= (upButtonValue + 10)) return btnUP;
- if (adc_key_in >= (downButtonValue - 10) && adc_key_in <= (downButtonValue + 10)) return btnDOWN;
- if (adc_key_in >= (leftButtonValue - 10) && adc_key_in <= (leftButtonValue + 10)) return btnLEFT;
- if (adc_key_in >= (selectButtonValue - 10) && adc_key_in <= (selectButtonValue + 10)) return btnSELECT;
- return btnNONE; // when all others fail, return this...
- }
- void setup()
- {
- lcd.begin(16, 2); // start the library
- lcd.setCursor(0,0);
- lcd.print(noneScreen[0]);
- lcd.setCursor(0,1);
- lcd.print(noneScreen[1]);
- }
- void loop()
- {
- lcd_key = read_LCD_buttons(); // read the buttons
- switch (lcd_key) // depending on which button was pushed, we perform an action
- {
- case btnRIGHT:
- {
- lcd.setCursor(0,0);
- lcd.print(rightScreen[0]);
- lcd.setCursor(0,1);
- lcd.print(rightScreen[1]);
- break;
- }
- case btnLEFT:
- {
- lcd.setCursor(0,0);
- lcd.print(leftScreen[0]);
- lcd.setCursor(0,1);
- lcd.print(leftScreen[1]);
- break;
- }
- case btnUP:
- {
- // reset the display
- lcd.setCursor(0,0);
- lcd.print(upScreen[0]);
- lcd.setCursor(0,1);
- lcd.print(upScreen[1]);
- break;
- }
- case btnDOWN:
- {
- // reset the display
- lcd.setCursor(0,0);
- lcd.print(downScreen[0]);
- lcd.setCursor(0,1);
- lcd.print(downScreen[1]);
- break;
- }
- case btnSELECT:
- {
- lcd.setCursor(0,0);
- lcd.print(selectScreen[0]);
- lcd.setCursor(0,1);
- lcd.print(selectScreen[1]);
- break;
- }
- case btnNONE:
- {
- lcd.setCursor(0,0);
- lcd.print(noneScreen[0]);
- lcd.setCursor(0,1);
- lcd.print(noneScreen[1]);
- break;
- }
- } // end switch
- } // end void loop
Add Comment
Please, Sign In to add comment