skizziks_53

Adafruit LCD+keypad shield demo menu v1.0

Apr 3rd, 2019
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.57 KB | None | 0 0
  1. /*********************
  2.   April 4, 2019
  3.   Adafruit 16x2_LCD+keypad shield example.
  4.  
  5.   Note: this sketch ONLY works with the Adafruit LCD + keypad shield that uses the MPC23017 i2c expander chip.
  6.   This sketch also is for a single-color LCD, so it doesn't try to change the LCD color. The same library is used for the single-color shields and the RGB-LCD shields.
  7.  
  8.   This sketch will not work with the generic parallel-LCD and series-resistance button generic shields sold elsewhere online.
  9.  
  10.   The shield fits on an Uno, which is what I tested it on. I don't know if it will work on a Mega.
  11.  
  12.   This sketch displays a two-level menu, and allows selecting choices from each sub-menu.
  13.   The sub-menu choices persist, because you can scroll left-and-right, and they remain where you left them (they are stored in the subMenu_selections[] array).
  14.  
  15.   When you press the [select] button, that re-sets all the menu choices back to the zero position, and displays the result (at the #zero menu, which is "cars").
  16.   It is kinda long due to the strings used.
  17.  
  18.  **********************/
  19.  
  20. // Libraries used:
  21. #include <Wire.h>
  22. #include <Adafruit_MCP23017.h>
  23. #include <Adafruit_RGBLCDShield.h>
  24.  
  25.  
  26. Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
  27.  
  28. String top_level_menu[] = {"cars", "trees", "countries", "colors"};
  29. int top_level_selection = 0; // This value is zero to 3, representing the four possible choices in top_level_menu[]
  30. int max_top_level_selection = 3; // This defines the maximum value that top_level_selection can be allowed to go to.
  31.  
  32. String secondary_menu_0[] = {"ford", "chevy", "mazda", "nissan", "saab"};
  33. String secondary_menu_1[] = {"oak", "elm", "walnut", "cherry"};
  34. String secondary_menu_2[] = {"canada", "brazil", "japan"};
  35. String secondary_menu_3[] = {"red", "yellow", "orange", "green", "blue", "purple", "brown", "black"};
  36.  
  37. int subMenu_selections[] = {0, 0, 0, 0}; // These are for storing the four sub-menu selections.
  38. int maximum_submenu_selections[] = {4, 3, 2, 7}; // This defines the maximum sub-menu level, based on what sub-menu menu is selected.
  39. // The value of top_level_selection will be used as an indice into these two arrays.
  40.  
  41.  
  42. void setup() {
  43.   Serial.begin(9600);
  44.   // set up the LCD's number of columns and rows:
  45.   lcd.begin(16, 2);
  46.  
  47.   show_menu_selection(); // Show where the menu is currently set (to group=zero and choice=zero).
  48.  
  49.   Serial.println("Exiting setup()");
  50. }
  51.  
  52. uint8_t i = 0; // I dunno why Adafruit used a short here to store the button ID in, but that's what they did.
  53.  
  54. void loop() {
  55.  
  56.   uint8_t buttons = lcd.readButtons();
  57.  
  58.   if (buttons) {
  59.  
  60.  
  61.     // The up and down buttons scroll through the sub-menu choices.
  62.     if (buttons == BUTTON_UP) {
  63.       if (subMenu_selections[top_level_selection] > 0) {
  64.         subMenu_selections[top_level_selection] = subMenu_selections[top_level_selection] - 1;
  65.       }
  66.       show_menu_selection();
  67.     }
  68.  
  69.     if (buttons == BUTTON_DOWN) {
  70.       if (subMenu_selections[top_level_selection] < maximum_submenu_selections[top_level_selection]) {
  71.         subMenu_selections[top_level_selection] = subMenu_selections[top_level_selection] + 1;
  72.       }
  73.       show_menu_selection();
  74.     }
  75.  
  76.  
  77.  
  78.     // The left and right buttons scroll through the top-level choices.
  79.     if (buttons == BUTTON_LEFT) {
  80.       if (top_level_selection > 0) {
  81.         top_level_selection = top_level_selection - 1;
  82.       }
  83.       show_menu_selection();
  84.     }
  85.  
  86.     if (buttons == BUTTON_RIGHT) {
  87.       if (top_level_selection < max_top_level_selection) {
  88.         top_level_selection = top_level_selection + 1;
  89.       }
  90.       show_menu_selection();
  91.     }
  92.  
  93.  
  94.     // The [select] button resets all the choices back to the first option.
  95.     if (buttons == BUTTON_SELECT) {
  96.       top_level_selection = 0;
  97.       subMenu_selections[0] = 0;
  98.       subMenu_selections[1] = 0;
  99.       subMenu_selections[2] = 0;
  100.       subMenu_selections[3] = 0;
  101.       show_menu_selection();
  102.     }
  103.  
  104.     delay(300); // This is done to de-bounce the buttons as I was getting some repeating without it.
  105.   }
  106. }
  107.  
  108.  
  109. void show_menu_selection() {
  110.   // The four lines below print the top line of the LCD display.
  111.   clear_top_LCD_line();
  112.   lcd.setCursor(0, 0); // the first line is numbered zero, and first character spot is numbered zero.
  113.   lcd.print("group=");
  114.   lcd.setCursor(6, 0);
  115.   lcd.print(top_level_menu[top_level_selection]); // This prints the group that you are selecting from.
  116.  
  117.   // The lines below print the second line of the LCD display.
  118.   clear_button_LCD_line();
  119.   lcd.setCursor(0, 1);
  120.   lcd.print("choice=");
  121.   lcd.setCursor(7, 1);
  122.   switch (top_level_selection) {
  123.     case 0:
  124.       lcd.print(secondary_menu_0[subMenu_selections[top_level_selection]]);
  125.       break;
  126.     case 1:
  127.       lcd.print(secondary_menu_1[subMenu_selections[top_level_selection]]);
  128.       break;
  129.     case 2:
  130.       lcd.print(secondary_menu_2[subMenu_selections[top_level_selection]]);
  131.       break;
  132.     case 3:
  133.       lcd.print(secondary_menu_3[subMenu_selections[top_level_selection]]);
  134.       break;
  135.   }
  136. }
  137.  
  138. void clear_top_LCD_line() {
  139.   // This just prints spaces over the whole upper LCD line, to clear it out.
  140.   // With these LCDs, the character you put into any one spot stays there until you print a [space] over it.
  141.   lcd.setCursor(0, 0);
  142.   lcd.print("                "); // This is printing sixteen spaces in the first (top) LCD line.
  143. }
  144.  
  145. void clear_button_LCD_line() {
  146.   // This just prints spaces over the whole lower LCD line, to clear it out.
  147.   lcd.setCursor(0, 1);
  148.   lcd.print("                "); // This is printing sixteen spaces in the second (lower) LCD line.
  149. }
Advertisement
Add Comment
Please, Sign In to add comment