ccarman602

Arduino - Liquid Crystal Display w/buttons

Jun 1st, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <LiquidCrystal.h>
  2.  
  3. /*******************************************************
  4. This program allows the user to create a series of
  5. messages on an LCD keypad screen that will display when
  6. a particular key is pressed.
  7.  
  8. The only values that need to be changed are:
  9. - upScreen, downScreen, leftScreen, rightScreen,
  10.   selectScreen and noneScreen
  11. - noneScreen is what will display when no buttons are
  12.   being pressed.
  13.  
  14. Messages must be exactly 16 characters long and contain
  15. 2 lines. You must use spaces to clear old messages!
  16.  
  17. Modified from code at: https://www.dfrobot.com/wiki/index.php/Arduino_LCD_KeyPad_Shield_(SKU:_DFR0009)
  18. ********************************************************/
  19.  
  20. // select the pins used on the LCD panel
  21. LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
  22.  
  23. // define some values used by the panel and buttons
  24. int lcd_key     = 0;
  25. int adc_key_in  = 0;
  26. #define btnRIGHT  0
  27. #define btnUP     1
  28. #define btnDOWN   2
  29. #define btnLEFT   3
  30. #define btnSELECT 4
  31. #define btnNONE   5
  32.  
  33. // this is where you put your messages, using this format:
  34. //                        "FIRST LINE------", "SECOND LINE-----"
  35. //                         1234567890123456    1234567890123456
  36. char* upScreen[]      = { "You are pressing", "the  UP   button" }; // displays when UP is pressed
  37. char* downScreen[]    = { "You are pressing", "the DOWN button " }; // displays when DOWN is pressed
  38. char* leftScreen[]    = { "You are pressing", "the LEFT button " }; // displays when LEFT is pressed
  39. char* rightScreen[]   = { "You are pressing", "the RIGHT button" }; // displays when RIGHT is pressed
  40. char* selectScreen[]  = { "You are pressing", "  SELECT  button" }; // displays when SELECT is pressed
  41. char* noneScreen[]    = { "Try pressing one", "of the buttons! " }; // displays when SELECT is pressed
  42.  
  43. // read the buttons
  44. int read_LCD_buttons()
  45. {
  46.   adc_key_in = analogRead(0);      // read the value from the sensor
  47.   if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
  48.   int rightButtonValue = 0;
  49.   int upButtonValue = 143;
  50.   int downButtonValue = 328;
  51.   int leftButtonValue = 504;
  52.   int selectButtonValue = 740;
  53.  
  54.   // if (adc_key_in == rightButtonValue)   return btnRIGHT;  
  55.   if (adc_key_in >= (rightButtonValue - 10) && adc_key_in <= (rightButtonValue + 10))  return btnRIGHT;
  56.   if (adc_key_in >= (upButtonValue - 10) && adc_key_in <= (upButtonValue + 10))  return btnUP;
  57.   if (adc_key_in >= (downButtonValue - 10) && adc_key_in <= (downButtonValue + 10))  return btnDOWN;
  58.   if (adc_key_in >= (leftButtonValue - 10) && adc_key_in <= (leftButtonValue + 10))  return btnLEFT;
  59.   if (adc_key_in >= (selectButtonValue - 10) && adc_key_in <= (selectButtonValue + 10))  return btnSELECT;
  60.  
  61.   return btnNONE;  // when all others fail, return this...
  62. }
  63.  
  64. void setup()
  65. {
  66.   lcd.begin(16, 2);              // start the library
  67.   lcd.setCursor(0,0);
  68.   lcd.print(noneScreen[0]);
  69.   lcd.setCursor(0,1);
  70.   lcd.print(noneScreen[1]);
  71. }
  72.  
  73.  
  74. void loop()
  75. {
  76.  
  77.   lcd_key = read_LCD_buttons();  // read the buttons
  78.  
  79.   switch (lcd_key)               // depending on which button was pushed, we perform an action
  80.    {
  81.  
  82.    case btnRIGHT:
  83.      {
  84.        lcd.setCursor(0,0);
  85.        lcd.print(rightScreen[0]);
  86.        lcd.setCursor(0,1);
  87.        lcd.print(rightScreen[1]);
  88.      break;
  89.      }
  90.  
  91.    case btnLEFT:
  92.      {
  93.        lcd.setCursor(0,0);
  94.        lcd.print(leftScreen[0]);
  95.        lcd.setCursor(0,1);
  96.        lcd.print(leftScreen[1]);
  97.      break;
  98.      }
  99.  
  100.    case btnUP:
  101.      {
  102.      // reset the display
  103.      lcd.setCursor(0,0);
  104.      lcd.print(upScreen[0]);
  105.      lcd.setCursor(0,1);
  106.      lcd.print(upScreen[1]);
  107.      break;
  108.      }
  109.  
  110.    case btnDOWN:
  111.      {
  112.      // reset the display
  113.      lcd.setCursor(0,0);
  114.      lcd.print(downScreen[0]);
  115.      lcd.setCursor(0,1);
  116.      lcd.print(downScreen[1]);
  117.      break;
  118.      }
  119.  
  120.    case btnSELECT:
  121.      {
  122.      lcd.setCursor(0,0);
  123.      lcd.print(selectScreen[0]);
  124.      lcd.setCursor(0,1);
  125.      lcd.print(selectScreen[1]);
  126.      break;
  127.      }
  128.  
  129.    case btnNONE:
  130.      {
  131.       lcd.setCursor(0,0);
  132.       lcd.print(noneScreen[0]);
  133.       lcd.setCursor(0,1);
  134.       lcd.print(noneScreen[1]);
  135.      break;
  136.      }
  137.  
  138.    } // end switch
  139.  
  140. } // end void loop
Add Comment
Please, Sign In to add comment