Advertisement
Guest User

Untitled

a guest
Aug 24th, 2020
628
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 10.16 KB | None | 0 0
  1. /***************************************************************************************
  2.     Name    : LCD Button Shield Menu
  3.     Author  : Paul Siewert
  4.     Created : June 14, 2016
  5.     Last Modified: June 14, 2016
  6.     Version : 1.0
  7.     Notes   : This code is for use with an Arduino Uno and LCD/button shield. The
  8.               intent is for anyone to use this program to give them a starting
  9.               program with a fully functional menu with minimal modifications
  10.               required by the user.
  11.     License : This program is free software. You can redistribute it and/or modify
  12.               it under the terms of the GNU General Public License as published by
  13.               the Free Software Foundation, either version 3 of the License, or
  14.               (at your option) any later version.
  15.               This program is distributed in the hope that it will be useful,
  16.               but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.               MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.               GNU General Public License for more details.
  19.  ***************************************************************************************/
  20. /*
  21.    This program is designed to get you as close as possible to a finished menu for the standard Arduino Uno LCD/button shield. The only required modifications
  22.    are to add as menu items to the master menu (menuItems array) and then modify/adjust the void functions below for each of those selections.
  23. */
  24.  
  25. #include <AccelStepper.h>
  26. // defines pins numbers
  27.   //Motor Pins
  28. const int stepPin = 11;
  29. const int dirPin = 2;
  30. const int enPin = 13;
  31. AccelStepper stepper(1,stepPin, dirPin, enPin);
  32.  
  33. // defines variables
  34. int maxStepSpeed = 10000;
  35. //long stepSpeed = 316.0299659;
  36. //long stepResetSpeed = -1000;
  37. //int forwarddir = 44802;
  38.  
  39. // 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
  40. // section of the operateMainMenu() function below. You will also have to add additional void functions (i.e. menuItem11, menuItem12, etc.) to the program.
  41. String menuItems[] = {"Start", "Reset"};
  42.  
  43. // Navigation button variables
  44. int readKey;
  45.  
  46. // Menu control variables
  47. int menuPage = 0;
  48. int maxMenuPages = round(((sizeof(menuItems) / sizeof(String)) / 2) + .5);
  49. int cursorPosition = 0;
  50.  
  51. // Creates 3 custom characters for the menu display
  52. byte downArrow[8] = {
  53.   0b00100, //   *
  54.   0b00100, //   *
  55.   0b00100, //   *
  56.   0b00100, //   *
  57.   0b00100, //   *
  58.   0b10101, // * * *
  59.   0b01110, //  ***
  60.   0b00100  //   *
  61. };
  62.  
  63. byte upArrow[8] = {
  64.   0b00100, //   *
  65.   0b01110, //  ***
  66.   0b10101, // * * *
  67.   0b00100, //   *
  68.   0b00100, //   *
  69.   0b00100, //   *
  70.   0b00100, //   *
  71.   0b00100  //   *
  72. };
  73.  
  74. byte menuCursor[8] = {
  75.   B01000, //  *
  76.   B00100, //   *
  77.   B00010, //    *
  78.   B00001, //     *
  79.   B00010, //    *
  80.   B00100, //   *
  81.   B01000, //  *
  82.   B00000  //
  83. };
  84.  
  85. #include <LiquidCrystal.h>
  86.  
  87. // Setting the LCD shields pins
  88. LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
  89.  
  90. void setup() {
  91.  
  92.   // Initializes serial communication
  93.   Serial.begin(9600);
  94.  
  95.   // Initializes and clears the LCD screen
  96.   lcd.begin(16, 2);
  97.   lcd.clear();
  98.  
  99.   // Creates the byte for the 3 custom characters
  100.   lcd.createChar(0, menuCursor);
  101.   lcd.createChar(1, upArrow);
  102.   lcd.createChar(2, downArrow);
  103.   stepper.setMaxSpeed(maxStepSpeed);
  104. //  stepper.setSpeed(stepSpeed);
  105. //  stepper.moveTo(44803);
  106.   stepper.disableOutputs();
  107. }
  108.  
  109. void loop() {
  110.   mainMenuDraw();
  111.   drawCursor();
  112.   operateMainMenu();
  113. }
  114.  
  115. // 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.
  116. void mainMenuDraw() {
  117.   Serial.print(menuPage);
  118.   lcd.clear();
  119.   lcd.setCursor(1, 0);
  120.   lcd.print(menuItems[menuPage]);
  121.   lcd.setCursor(1, 1);
  122.   lcd.print(menuItems[menuPage + 1]);
  123.   if (menuPage == 0) {
  124.     lcd.setCursor(15, 1);
  125.     lcd.write(byte(2));
  126.   } else if (menuPage > 0 and menuPage < maxMenuPages) {
  127.     lcd.setCursor(15, 1);
  128.     lcd.write(byte(2));
  129.     lcd.setCursor(15, 0);
  130.     lcd.write(byte(1));
  131.   } else if (menuPage == maxMenuPages) {
  132.     lcd.setCursor(15, 0);
  133.     lcd.write(byte(1));
  134.   }
  135. }
  136.  
  137. // When called, this function will erase the current cursor and redraw it based on the cursorPosition and menuPage variables.
  138. void drawCursor() {
  139.   for (int x = 0; x < 2; x++) {     // Erases current cursor
  140.     lcd.setCursor(0, x);
  141.     lcd.print(" ");
  142.   }
  143.  
  144.   // 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
  145.   // 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.
  146.   if (menuPage % 2 == 0) {
  147.     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
  148.       lcd.setCursor(0, 0);
  149.       lcd.write(byte(0));
  150.     }
  151.     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
  152.       lcd.setCursor(0, 1);
  153.       lcd.write(byte(0));
  154.     }
  155.   }
  156.   if (menuPage % 2 != 0) {
  157.     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
  158.       lcd.setCursor(0, 1);
  159.       lcd.write(byte(0));
  160.     }
  161.     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
  162.       lcd.setCursor(0, 0);
  163.       lcd.write(byte(0));
  164.     }
  165.   }
  166. }
  167.  
  168.  
  169. void operateMainMenu() {
  170.   int activeButton = 0;
  171.   while (activeButton == 0) {
  172.     int button;
  173.     readKey = analogRead(0);
  174.     if (readKey < 790) {
  175.       delay(100);
  176.       readKey = analogRead(0);
  177.     }
  178.     button = evaluateButton(readKey);
  179.     switch (button) {
  180.       case 0: // When button returns as 0 there is no action taken
  181.         break;
  182.       case 1:  // This case will execute if the "forward" button is pressed
  183.         button = 0;
  184.         switch (cursorPosition) { // The case that is selected here is dependent on which menu page you are on and where the cursor is.
  185.           case 0:
  186.             menuItem1();
  187.             break;
  188.           case 1:
  189.             menuItem2();
  190.             break;
  191.         }
  192.         activeButton = 1;
  193.         mainMenuDraw();
  194.         drawCursor();
  195.         break;
  196.       case 2:
  197.         button = 0;
  198.         if (menuPage == 0) {
  199.           cursorPosition = cursorPosition - 1;
  200.           cursorPosition = constrain(cursorPosition, 0, ((sizeof(menuItems) / sizeof(String)) - 1));
  201.         }
  202.         if (menuPage % 2 == 0 and cursorPosition % 2 == 0) {
  203.           menuPage = menuPage - 1;
  204.           menuPage = constrain(menuPage, 0, maxMenuPages);
  205.         }
  206.  
  207.         if (menuPage % 2 != 0 and cursorPosition % 2 != 0) {
  208.           menuPage = menuPage - 1;
  209.           menuPage = constrain(menuPage, 0, maxMenuPages);
  210.         }
  211.  
  212.         cursorPosition = cursorPosition - 1;
  213.         cursorPosition = constrain(cursorPosition, 0, ((sizeof(menuItems) / sizeof(String)) - 1));
  214.  
  215.         mainMenuDraw();
  216.         drawCursor();
  217.         activeButton = 1;
  218.         break;
  219.       case 3:
  220.         button = 0;
  221.         if (menuPage % 2 == 0 and cursorPosition % 2 != 0) {
  222.           menuPage = menuPage + 1;
  223.           menuPage = constrain(menuPage, 0, maxMenuPages);
  224.         }
  225.  
  226.         if (menuPage % 2 != 0 and cursorPosition % 2 == 0) {
  227.           menuPage = menuPage + 1;
  228.           menuPage = constrain(menuPage, 0, maxMenuPages);
  229.         }
  230.  
  231.         cursorPosition = cursorPosition + 1;
  232.         cursorPosition = constrain(cursorPosition, 0, ((sizeof(menuItems) / sizeof(String)) - 1));
  233.         mainMenuDraw();
  234.         drawCursor();
  235.         activeButton = 1;
  236.         break;
  237.     }
  238.   }
  239. }
  240.  
  241. // 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.
  242. int evaluateButton(int x) {
  243.   int result = 0;
  244.   if (x < 60) {
  245.     result = 1; // right
  246.   } else if (x < 300) {
  247.     result = 2; // up
  248.   } else if (x < 500) {
  249.     result = 3; // down
  250.   } else if (x < 700) {
  251.     result = 4; // left
  252.   }
  253.   return result;
  254. }
  255.  
  256. // If there are common usage instructions on more than 1 of your menu items you can call this function from the sub
  257. // menus to make things a little more simplified. If you don't have common instructions or verbage on multiple menus
  258. // I would just delete this void. You must also delete the drawInstructions()function calls from your sub menu functions.
  259. void drawInstructions() {
  260.   lcd.setCursor(0, 1); // Set cursor to the bottom line
  261.   lcd.print("Use ");
  262.   lcd.print(byte(1)); // Up arrow
  263.   lcd.print("/");
  264.   lcd.print(byte(2)); // Down arrow
  265.   lcd.print(" buttons");
  266. }
  267.  
  268. void menuItem1() { // Function executes when you select the 1st item from main menu
  269.   int activeButton = 0;
  270.  
  271.   lcd.clear();
  272.   lcd.setCursor(3, 0);
  273.   lcd.print("running");
  274.   long stepSpeed = 316.0299659;
  275.   stepper.setSpeed(stepSpeed);
  276.  
  277.   while (activeButton == 0) {
  278.     stepper.runSpeed();
  279.     int button;
  280.     readKey = analogRead(0);
  281.     if (readKey < 790) {
  282.       delay(100);
  283.       readKey = analogRead(0);
  284.     }
  285.     button = evaluateButton(readKey);
  286.     switch (button) {
  287.       case 4:  // This case will execute if the "back" button is pressed
  288.         button = 0;
  289.         activeButton = 1;
  290.         break;
  291.     }
  292.   }
  293. }
  294.  
  295. void menuItem2() { // Function executes when you select the 2nd item from main menu
  296.   int activeButton = 0;
  297.  
  298.   lcd.clear();
  299.   lcd.setCursor(3, 0);
  300.   lcd.print("resetting");
  301.   long stepSpeed = -1000;
  302.   stepper.setSpeed(stepSpeed);
  303.  
  304.   while (activeButton == 0) {
  305.     stepper.runSpeed();
  306.     int button;
  307.     readKey = analogRead(0);
  308.     if (readKey < 790) {
  309.       delay(100);
  310.       readKey = analogRead(0);
  311.     }
  312.     button = evaluateButton(readKey);
  313.     switch (button) {
  314.       case 4:  // This case will execute if the "back" button is pressed
  315.         button = 0;
  316.         activeButton = 1;
  317.         break;
  318.     }
  319.   }
  320. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement