Advertisement
Guest User

LCD MENU

a guest
Aug 22nd, 2019
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.13 KB | None | 0 0
  1.  
  2. String menuItems[] = {"Display Stats","Set 1", "Set 2", "Send signal"};
  3. String subMenuItems[] = {"Temperature","Humidity"};
  4.  
  5. int readKey;
  6. int savedDistance = 0;
  7. int subMenu;
  8.  
  9. // Menu control variables
  10. int menuPage = 0;
  11. int maxMenuPages = round(((sizeof(menuItems) / sizeof(String)) / 2) + .5);
  12. int cursorPosition = 0;
  13.  
  14. int subMenuPage = 0;
  15. int maxSubMenuPages1 = round(((sizeof(subMenuItems) / sizeof(String)) / 2) + .5);
  16. int subCursorPosition = 0;
  17.  
  18. byte downArrow[8] = {
  19. 0b00100, // *
  20. 0b00100, // *
  21. 0b00100, // *
  22. 0b00100, // *
  23. 0b00100, // *
  24. 0b10101, // * * *
  25. 0b01110, // ***
  26. 0b00100 // *
  27. };
  28.  
  29. byte upArrow[8] = {
  30. 0b00100, // *
  31. 0b01110, // ***
  32. 0b10101, // * * *
  33. 0b00100, // *
  34. 0b00100, // *
  35. 0b00100, // *
  36. 0b00100, // *
  37. 0b00100 // *
  38. };
  39.  
  40. byte menuCursor[8] = {
  41. B01000, // *
  42. B00100, // *
  43. B00010, // *
  44. B00001, // *
  45. B00010, // *
  46. B00100, // *
  47. B01000, // *
  48. B00000 //
  49. };
  50.  
  51. #include <Wire.h>
  52. #include <LiquidCrystal.h>
  53.  
  54.  
  55. // Setting the LCD shields pins
  56. LiquidCrystal lcd(44, 46, 4, 5, 6, 7);
  57.  
  58. void setup() {
  59.  
  60. // Initializes serial communication
  61. Serial.begin(9600);
  62.  
  63. // Initializes and clears the LCD screen
  64. lcd.begin(16, 2);
  65. lcd.clear();
  66. subMenu=1;
  67. // Creates the byte for the 3 custom characters
  68. lcd.createChar(0, menuCursor);
  69. lcd.createChar(1, upArrow);
  70. lcd.createChar(2, downArrow);
  71.  
  72. }
  73.  
  74. void loop() {
  75. switch(subMenu)
  76. {
  77. case 1:
  78. mainMenuDraw();
  79. drawCursor();
  80. operateMainMenu();
  81. break;
  82. case 2:
  83. subMenuDraw();
  84. drawSubCursor();
  85. operateSubMenu();
  86. break;
  87. }
  88. }
  89. void mainMenuDraw() {
  90. Serial.print(menuPage);
  91. lcd.clear();
  92. lcd.setCursor(1, 0);
  93. lcd.print(menuItems[menuPage]);
  94. lcd.setCursor(1, 1);
  95. lcd.print(menuItems[menuPage + 1]);
  96. if (menuPage == 0) {
  97. lcd.setCursor(15, 1);
  98. lcd.write(byte(2));
  99. } else if (menuPage > 0 and menuPage < maxMenuPages) {
  100. lcd.setCursor(15, 1);
  101. lcd.write(byte(2));
  102. lcd.setCursor(15, 0);
  103. lcd.write(byte(1));
  104. } else if (menuPage == maxMenuPages) {
  105. lcd.setCursor(15, 0);
  106. lcd.write(byte(1));
  107. }
  108. }
  109. void drawCursor() {
  110. for (int x = 0; x < 2; x++) { // Erases current cursor
  111. lcd.setCursor(0, x);
  112. lcd.print(" ");
  113. }
  114.  
  115. if (menuPage % 2 == 0) {
  116. 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
  117. lcd.setCursor(0, 0);
  118. lcd.write(byte(0));
  119. }
  120. 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
  121. lcd.setCursor(0, 1);
  122. lcd.write(byte(0));
  123. }
  124. }
  125. if (menuPage % 2 != 0) {
  126. 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
  127. lcd.setCursor(0, 1);
  128. lcd.write(byte(0));
  129. }
  130. 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
  131. lcd.setCursor(0, 0);
  132. lcd.write(byte(0));
  133. }
  134. }
  135. }
  136. void operateMainMenu() {
  137. int activeButton = 0;
  138. while (activeButton == 0) {
  139. int button;
  140. readKey = analogRead(0);
  141. if (readKey < 790) {
  142. delay(100);
  143. readKey = analogRead(0);
  144. }
  145. button = evaluateButton(readKey);
  146. switch (button) {
  147. case 0: // When button returns as 0 there is no action taken
  148. break;
  149. case 1: // This case will execute if the "forward" button is pressed
  150. button = 0;
  151. switch (cursorPosition) { // The case that is selected here is dependent on which menu page you are on and where the cursor is.
  152. case 0:
  153. menuItem1();
  154. break;
  155. }
  156. activeButton = 1;
  157. mainMenuDraw();
  158. drawCursor();
  159. break;
  160. case 2:
  161. button = 0;
  162. if (menuPage == 0) {
  163. cursorPosition = cursorPosition - 1;
  164. cursorPosition = constrain(cursorPosition, 0, ((sizeof(menuItems) / sizeof(String)) - 1));
  165. }
  166. if (menuPage % 2 == 0 and cursorPosition % 2 == 0) {
  167. menuPage = menuPage - 1;
  168. menuPage = constrain(menuPage, 0, maxMenuPages);
  169. }
  170.  
  171. if (menuPage % 2 != 0 and cursorPosition % 2 != 0) {
  172. menuPage = menuPage - 1;
  173. menuPage = constrain(menuPage, 0, maxMenuPages);
  174. }
  175.  
  176. cursorPosition = cursorPosition - 1;
  177. cursorPosition = constrain(cursorPosition, 0, ((sizeof(menuItems) / sizeof(String)) - 1));
  178.  
  179. mainMenuDraw();
  180. drawCursor();
  181. activeButton = 1;
  182. break;
  183. case 3:
  184. button = 0;
  185. if (menuPage % 2 == 0 and cursorPosition % 2 != 0) {
  186. menuPage = menuPage + 1;
  187. menuPage = constrain(menuPage, 0, maxMenuPages);
  188. }
  189.  
  190. if (menuPage % 2 != 0 and cursorPosition % 2 == 0) {
  191. menuPage = menuPage + 1;
  192. menuPage = constrain(menuPage, 0, maxMenuPages);
  193. }
  194.  
  195. cursorPosition = cursorPosition + 1;
  196. cursorPosition = constrain(cursorPosition, 0, ((sizeof(menuItems) / sizeof(String)) - 1));
  197. mainMenuDraw();
  198. drawCursor();
  199. activeButton = 1;
  200. break;
  201. }
  202. }
  203. }
  204. int evaluateButton(int x) {
  205. int result = 0;
  206. if (x < 50) {
  207. result = 1; // right
  208. } else if (x < 195) {
  209. result = 2; // up
  210. } else if (x < 380) {
  211. result = 3; // down
  212. } else if (x < 790) {
  213. result = 4; // left
  214. }
  215. return result;
  216. }
  217. void drawInstructions() {
  218. lcd.setCursor(0, 1); // Set cursor to the bottom line
  219. lcd.print("Use ");
  220. lcd.write(byte(1)); // Up arrow
  221. lcd.print("/");
  222. lcd.write(byte(2)); // Down arrow
  223. lcd.print(" buttons");
  224. }
  225. void menuItem1() { // Function executes when you select the 3rd item from main menu
  226. int activeButton = 0;
  227. lcd.clear();
  228. subMenu=2;
  229. while (activeButton == 0) {
  230. int button;
  231. readKey = analogRead(0);
  232. if (readKey < 790) {
  233. delay(100);
  234. readKey = analogRead(0);
  235. }
  236. button = evaluateButton(readKey);
  237. switch (button) {
  238. case 4: // This case will execute if the "back" button is pressed
  239. button = 0;
  240. activeButton = 1;
  241. break;
  242. }
  243. }
  244. }
  245. void subMenuDraw() {
  246. Serial.print(subMenuPage);
  247. lcd.clear();
  248. lcd.setCursor(1, 0);
  249. lcd.print(subMenuItems[subMenuPage]);
  250. lcd.setCursor(1, 1);
  251. lcd.print(subMenuItems[subMenuPage + 1]);
  252. if (subMenuPage == 0) {
  253. lcd.setCursor(15, 1);
  254. lcd.write(byte(2));
  255. } else if (subMenuPage > 0 and subMenuPage < maxSubMenuPages1) {
  256. lcd.setCursor(15, 1);
  257. lcd.write(byte(2));
  258. lcd.setCursor(15, 0);
  259. lcd.write(byte(1));
  260. } else if (subMenuPage == maxSubMenuPages1) {
  261. lcd.setCursor(15, 0);
  262. lcd.write(byte(1));
  263. }
  264. }
  265.  
  266. void drawSubCursor() {
  267. for (int x = 0; x < 2; x++) { // Erases current cursor
  268. lcd.setCursor(0, x);
  269. lcd.print(" ");
  270. }
  271.  
  272. if (subMenuPage % 2 == 0) {
  273. if (subCursorPosition % 2 == 0) { // If the menu page is even and the cursor position is even that means the cursor should be on line 1
  274. lcd.setCursor(0, 0);
  275. lcd.write(byte(0));
  276. }
  277. if (subCursorPosition % 2 != 0) { // If the menu page is even and the cursor position is odd that means the cursor should be on line 2
  278. lcd.setCursor(0, 1);
  279. lcd.write(byte(0));
  280. }
  281. }
  282. if (subMenuPage % 2 != 0) {
  283. if (subCursorPosition % 2 == 0) { // If the menu page is odd and the cursor position is even that means the cursor should be on line 2
  284. lcd.setCursor(0, 1);
  285. lcd.write(byte(0));
  286. }
  287. if (subCursorPosition % 2 != 0) { // If the menu page is odd and the cursor position is odd that means the cursor should be on line 1
  288. lcd.setCursor(0, 0);
  289. lcd.write(byte(0));
  290. }
  291. }
  292. }
  293. void operateSubMenu() {
  294. int activeButton = 0;
  295. while (activeButton == 0) {
  296. int button;
  297. readKey = analogRead(0);
  298. if (readKey < 790) {
  299. delay(100);
  300. readKey = analogRead(0);
  301. }
  302. button = evaluateButton(readKey);
  303. switch (button) {
  304. case 0: // When button returns as 0 there is no action taken
  305. break;
  306. case 1: // This case will execute if the "forward" button is pressed
  307. button = 0;
  308. switch (subCursorPosition) { // The case that is selected here is dependent on which menu page you are on and where the cursor is.
  309. case 0:
  310. menuItem1();
  311. break;
  312. }
  313. activeButton = 1;
  314. subMenuDraw();
  315. drawSubCursor();
  316. break;
  317. case 2:
  318. button = 0;
  319. if (subMenuPage == 0) {
  320. subCursorPosition = subCursorPosition - 1;
  321. subCursorPosition = constrain(subCursorPosition, 0, ((sizeof(subMenuItems) / sizeof(String)) - 1));
  322. }
  323. if (subMenuPage % 2 == 0 and subCursorPosition % 2 == 0) {
  324. subMenuPage= subMenuPage - 1;
  325. subMenuPage = constrain(subMenuPage, 0, maxSubMenuPages1);
  326. }
  327.  
  328. if (subMenuPage % 2 != 0 and subCursorPosition % 2 != 0) {
  329. subMenuPage = subMenuPage - 1;
  330. subMenuPage = constrain(subMenuPage, 0, maxSubMenuPages1);
  331. }
  332.  
  333. subCursorPosition = subCursorPosition - 1;
  334. subCursorPosition = constrain(subCursorPosition, 0, ((sizeof(subMenuItems) / sizeof(String)) - 1));
  335.  
  336. subMenuDraw();
  337. drawSubCursor();
  338. activeButton = 1;
  339. break;
  340. case 3:
  341. button = 0;
  342. if (subMenuPage % 2 == 0 and subCursorPosition % 2 != 0) {
  343. subMenuPage = subMenuPage + 1;
  344. subMenuPage = constrain(subMenuPage, 0, maxSubMenuPages1);
  345. }
  346.  
  347. if (subMenuPage % 2 != 0 and subCursorPosition % 2 == 0) {
  348. subMenuPage = subMenuPage + 1;
  349. subMenuPage = constrain(subMenuPage, 0, maxSubMenuPages1);
  350. }
  351.  
  352. subCursorPosition = subCursorPosition + 1;
  353. subCursorPosition = constrain(subCursorPosition, 0, ((sizeof(subMenuItems) / sizeof(String)) - 1));
  354. subMenuDraw();
  355. drawSubCursor();
  356. activeButton = 1;
  357. break;
  358. }
  359. }
  360. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement