Advertisement
learnelectronics

LCD Keypad Shield Menu System

Sep 19th, 2017
25,873
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.90 KB | None | 0 0
  1. /*
  2.  * LCD Keypad Shield Menu System
  3.  *
  4.  * learnelectronics
  5.  * 18 Sept 2017
  6.  * adapted from an original sketch by Paul Siewert
  7.  *
  8.  * www.youtube.com/c/learnelectronics
  9.  *
  10.  */
  11.  
  12. // You can have up to 10 menu items in the menuItems[] array below without having to change the base programming at all. Name them however you'd like. Beyond 10 items, you will have to add additional "cases" in the switch/case
  13. // section of the operateMainMenu() function below. You will also have to add additional void functions (i.e. menuItem11, menuItem12, etc.) to the program.
  14. String menuItems[] = {"Yellow", "Green", "Red", "Purple"};
  15.  
  16. // Navigation button variables
  17. int readKey;
  18. int savedDistance = 0;
  19.  
  20. // Menu control variables
  21. int menuPage = 0;
  22. int maxMenuPages = round(((sizeof(menuItems) / sizeof(String)) / 2) + .5);
  23. int cursorPosition = 0;
  24.  
  25. // Creates 3 custom characters for the menu display
  26. byte downArrow[8] = {
  27.   0b00100, //   *
  28.   0b00100, //   *
  29.   0b00100, //   *
  30.   0b00100, //   *
  31.   0b00100, //   *
  32.   0b10101, // * * *
  33.   0b01110, //  ***
  34.   0b00100  //   *
  35. };
  36.  
  37. byte upArrow[8] = {
  38.   0b00100, //   *
  39.   0b01110, //  ***
  40.   0b10101, // * * *
  41.   0b00100, //   *
  42.   0b00100, //   *
  43.   0b00100, //   *
  44.   0b00100, //   *
  45.   0b00100  //   *
  46. };
  47.  
  48. byte menuCursor[8] = {
  49.   B01000, //  *
  50.   B00100, //   *
  51.   B00010, //    *
  52.   B00001, //     *
  53.   B00010, //    *
  54.   B00100, //   *
  55.   B01000, //  *
  56.   B00000  //
  57. };
  58.  
  59. #include <Wire.h>
  60. #include <LiquidCrystal.h>
  61.  
  62. // Setting the LCD shields pins
  63. LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
  64.  
  65. void setup() {
  66.  
  67.   // Initializes serial communication
  68.   Serial.begin(9600);
  69.  
  70.   // Initializes and clears the LCD screen
  71.   lcd.begin(16, 2);
  72.   lcd.clear();
  73.  
  74.   // Creates the byte for the 3 custom characters
  75.   lcd.createChar(0, menuCursor);
  76.   lcd.createChar(1, upArrow);
  77.   lcd.createChar(2, downArrow);
  78.  
  79.   pinMode(13,OUTPUT);
  80.   pinMode(12,OUTPUT);
  81.   pinMode(11,OUTPUT);
  82.   pinMode(3,OUTPUT);
  83.  
  84. }
  85.  
  86. void loop() {
  87.   mainMenuDraw();
  88.   drawCursor();
  89.   operateMainMenu();
  90. }
  91.  
  92. // This function will generate the 2 menu items that can fit on the screen. They will change as you scroll through your menu. Up and down arrows will indicate your current menu position.
  93. void mainMenuDraw() {
  94.   Serial.print(menuPage);
  95.   lcd.clear();
  96.   lcd.setCursor(1, 0);
  97.   lcd.print(menuItems[menuPage]);
  98.   lcd.setCursor(1, 1);
  99.   lcd.print(menuItems[menuPage + 1]);
  100.   if (menuPage == 0) {
  101.     lcd.setCursor(15, 1);
  102.     lcd.write(byte(2));
  103.   } else if (menuPage > 0 and menuPage < maxMenuPages) {
  104.     lcd.setCursor(15, 1);
  105.     lcd.write(byte(2));
  106.     lcd.setCursor(15, 0);
  107.     lcd.write(byte(1));
  108.   } else if (menuPage == maxMenuPages) {
  109.     lcd.setCursor(15, 0);
  110.     lcd.write(byte(1));
  111.   }
  112. }
  113.  
  114. // When called, this function will erase the current cursor and redraw it based on the cursorPosition and menuPage variables.
  115. void drawCursor() {
  116.   for (int x = 0; x < 2; x++) {     // Erases current cursor
  117.     lcd.setCursor(0, x);
  118.     lcd.print(" ");
  119.   }
  120.  
  121.   // The menu is set up to be progressive (menuPage 0 = Item 1 & Item 2, menuPage 1 = Item 2 & Item 3, menuPage 2 = Item 3 & Item 4), so
  122.   // in order to determine where the cursor should be you need to see if you are at an odd or even menu page and an odd or even cursor position.
  123.   if (menuPage % 2 == 0) {
  124.     if (cursorPosition % 2 == 0) {  // If the menu page is even and the cursor position is even that means the cursor should be on line 1
  125.       lcd.setCursor(0, 0);
  126.       lcd.write(byte(0));
  127.     }
  128.     if (cursorPosition % 2 != 0) {  // If the menu page is even and the cursor position is odd that means the cursor should be on line 2
  129.       lcd.setCursor(0, 1);
  130.       lcd.write(byte(0));
  131.     }
  132.   }
  133.   if (menuPage % 2 != 0) {
  134.     if (cursorPosition % 2 == 0) {  // If the menu page is odd and the cursor position is even that means the cursor should be on line 2
  135.       lcd.setCursor(0, 1);
  136.       lcd.write(byte(0));
  137.     }
  138.     if (cursorPosition % 2 != 0) {  // If the menu page is odd and the cursor position is odd that means the cursor should be on line 1
  139.       lcd.setCursor(0, 0);
  140.       lcd.write(byte(0));
  141.     }
  142.   }
  143. }
  144.  
  145.  
  146. void operateMainMenu() {
  147.   int activeButton = 0;
  148.   while (activeButton == 0) {
  149.     int button;
  150.     readKey = analogRead(0);
  151.     if (readKey < 790) {
  152.       delay(100);
  153.       readKey = analogRead(0);
  154.     }
  155.     button = evaluateButton(readKey);
  156.     switch (button) {
  157.       case 0: // When button returns as 0 there is no action taken
  158.         break;
  159.       case 1:  // This case will execute if the "forward" button is pressed
  160.         button = 0;
  161.         switch (cursorPosition) { // The case that is selected here is dependent on which menu page you are on and where the cursor is.
  162.           case 0:
  163.             menuItem1();
  164.             break;
  165.           case 1:
  166.             menuItem2();
  167.             break;
  168.           case 2:
  169.             menuItem3();
  170.             break;
  171.           case 3:
  172.             menuItem4();
  173.             break;
  174.          
  175.         }
  176.         activeButton = 1;
  177.         mainMenuDraw();
  178.         drawCursor();
  179.         break;
  180.       case 2:
  181.         button = 0;
  182.         if (menuPage == 0) {
  183.           cursorPosition = cursorPosition - 1;
  184.           cursorPosition = constrain(cursorPosition, 0, ((sizeof(menuItems) / sizeof(String)) - 1));
  185.         }
  186.         if (menuPage % 2 == 0 and cursorPosition % 2 == 0) {
  187.           menuPage = menuPage - 1;
  188.           menuPage = constrain(menuPage, 0, maxMenuPages);
  189.         }
  190.  
  191.         if (menuPage % 2 != 0 and cursorPosition % 2 != 0) {
  192.           menuPage = menuPage - 1;
  193.           menuPage = constrain(menuPage, 0, maxMenuPages);
  194.         }
  195.  
  196.         cursorPosition = cursorPosition - 1;
  197.         cursorPosition = constrain(cursorPosition, 0, ((sizeof(menuItems) / sizeof(String)) - 1));
  198.  
  199.         mainMenuDraw();
  200.         drawCursor();
  201.         activeButton = 1;
  202.         break;
  203.       case 3:
  204.         button = 0;
  205.         if (menuPage % 2 == 0 and cursorPosition % 2 != 0) {
  206.           menuPage = menuPage + 1;
  207.           menuPage = constrain(menuPage, 0, maxMenuPages);
  208.         }
  209.  
  210.         if (menuPage % 2 != 0 and cursorPosition % 2 == 0) {
  211.           menuPage = menuPage + 1;
  212.           menuPage = constrain(menuPage, 0, maxMenuPages);
  213.         }
  214.  
  215.         cursorPosition = cursorPosition + 1;
  216.         cursorPosition = constrain(cursorPosition, 0, ((sizeof(menuItems) / sizeof(String)) - 1));
  217.         mainMenuDraw();
  218.         drawCursor();
  219.         activeButton = 1;
  220.         break;
  221.     }
  222.   }
  223. }
  224.  
  225. // This function is called whenever a button press is evaluated. The LCD shield works by observing a voltage drop across the buttons all hooked up to A0.
  226. int evaluateButton(int x) {
  227.   int result = 0;
  228.   if (x < 50) {
  229.     result = 1; // right
  230.   } else if (x < 195) {
  231.     result = 2; // up
  232.   } else if (x < 380) {
  233.     result = 3; // down
  234.   } else if (x < 790) {
  235.     result = 4; // left
  236.   }
  237.   return result;
  238. }
  239.  
  240. // If there are common usage instructions on more than 1 of your menu items you can call this function from the sub
  241. // menus to make things a little more simplified. If you don't have common instructions or verbage on multiple menus
  242. // I would just delete this void. You must also delete the drawInstructions()function calls from your sub menu functions.
  243. void drawInstructions() {
  244.   lcd.setCursor(0, 1); // Set cursor to the bottom line
  245.   lcd.print("Use ");
  246.   lcd.write(byte(1)); // Up arrow
  247.   lcd.print("/");
  248.   lcd.write(byte(2)); // Down arrow
  249.   lcd.print(" buttons");
  250. }
  251.  
  252. void menuItem1() { // Function executes when you select the Yellow item from main menu
  253.   int activeButton = 0;
  254.  
  255.   lcd.clear();
  256.   lcd.setCursor(3, 0);
  257.   lcd.print("Yellow On");
  258.  
  259.   digitalWrite(13,HIGH);
  260.  
  261.   while (activeButton == 0) {
  262.     int button;
  263.     readKey = analogRead(0);
  264.     if (readKey < 790) {
  265.       delay(100);
  266.       readKey = analogRead(0);
  267.     }
  268.     button = evaluateButton(readKey);
  269.     switch (button) {
  270.       case 4:  // This case will execute if the "back" button is pressed
  271.         button = 0;
  272.         activeButton = 1;
  273.         digitalWrite(13,LOW);
  274.         break;
  275.     }
  276.   }
  277. }
  278.  
  279. void menuItem2() { // Function executes when you select the Green item from main menu
  280.   int activeButton = 0;
  281.  
  282.   lcd.clear();
  283.   lcd.setCursor(3, 0);
  284.   lcd.print("Green On");
  285.  
  286.   digitalWrite(12,HIGH);
  287.  
  288.   while (activeButton == 0) {
  289.     int button;
  290.     readKey = analogRead(0);
  291.     if (readKey < 790) {
  292.       delay(100);
  293.       readKey = analogRead(0);
  294.     }
  295.     button = evaluateButton(readKey);
  296.     switch (button) {
  297.       case 4:  // This case will execute if the "back" button is pressed
  298.         button = 0;
  299.         activeButton = 1;
  300.         digitalWrite(12,LOW);
  301.         break;
  302.     }
  303.   }
  304. }
  305.  
  306. void menuItem3() { // Function executes when you select the Red item from main menu
  307.   int activeButton = 0;
  308.  
  309.   lcd.clear();
  310.   lcd.setCursor(3, 0);
  311.   lcd.print("Red On");
  312.  
  313.   digitalWrite(11,HIGH);
  314.  
  315.   while (activeButton == 0) {
  316.     int button;
  317.     readKey = analogRead(0);
  318.     if (readKey < 790) {
  319.       delay(100);
  320.       readKey = analogRead(0);
  321.     }
  322.     button = evaluateButton(readKey);
  323.     switch (button) {
  324.       case 4:  // This case will execute if the "back" button is pressed
  325.         button = 0;
  326.         activeButton = 1;
  327.         digitalWrite(11,LOW);
  328.         break;
  329.     }
  330.   }
  331. }
  332.  
  333. void menuItem4() { // Function executes when you select the Purple item from main menu
  334.   int activeButton = 0;
  335.  
  336.   lcd.clear();
  337.   lcd.setCursor(3, 0);
  338.   lcd.print("Purple On");
  339.  
  340.   digitalWrite(3,HIGH);
  341.  
  342.   while (activeButton == 0) {
  343.     int button;
  344.     readKey = analogRead(0);
  345.     if (readKey < 790) {
  346.       delay(100);
  347.       readKey = analogRead(0);
  348.     }
  349.     button = evaluateButton(readKey);
  350.     switch (button) {
  351.       case 4:  // This case will execute if the "back" button is pressed
  352.         button = 0;
  353.         activeButton = 1;
  354.         digitalWrite(3,LOW);
  355.         break;
  356.     }
  357.   }
  358. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement