Advertisement
FahmiG

Simple Arduino LCD Menu

Jul 7th, 2015
30,186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.60 KB | None | 0 0
  1. /*+++++++++++++++++++++++++++++++++++++++++++++++++++
  2. Author      : Fahmi Ghani
  3. Date        : 5 July 2015
  4. Project     : LCD Menu Function
  5. Component   : LCD Keypad shield
  6. Description : Simple Menu function on LCD
  7.               Button Function:
  8.                 Up/Down - change Menu Page
  9.                 Select - enter page
  10.                 Left - Exit
  11.  
  12. Video Link: http://youtu.be/cMqif5ICS5M
  13.  
  14. +++++++++++++++++++++++++++++++++++++++++++++++++++*/
  15.  
  16. #include <LiquidCrystal.h>
  17. LiquidCrystal lcd(8,9,4,5,6,7);  
  18.  
  19. int keypad_pin = A0;
  20. int keypad_value = 0;
  21. int keypad_value_old = 0;
  22.  
  23. char btn_push;
  24.  
  25. byte mainMenuPage = 1;
  26. byte mainMenuPageOld = 1;
  27. byte mainMenuTotal = 4;
  28.  
  29. void setup()
  30. {
  31.     lcd.begin(16,2);  //Initialize a 2x16 type LCD
  32.  
  33.     MainMenuDisplay();
  34.     delay(1000);
  35. }
  36. void loop()
  37. {
  38.     btn_push = ReadKeypad();
  39.    
  40.     MainMenuBtn();
  41.    
  42.     if(btn_push == 'S')//enter selected menu
  43.     {
  44.         WaitBtnRelease();
  45.         switch (mainMenuPage)
  46.         {
  47.             case 1:
  48.               MenuA();
  49.               break;
  50.             case 2:
  51.               MenuB();
  52.               break;
  53.             case 3:
  54.               MenuC();
  55.               break;
  56.             case 4:
  57.               MenuD();
  58.               break;
  59.         }
  60.  
  61.           MainMenuDisplay();
  62.           WaitBtnRelease();
  63.     }
  64.    
  65.  
  66.  
  67.     delay(10);
  68.  
  69. }//--------------- End of loop() loop ---------------------
  70. void MenuA()
  71. {  
  72.     lcd.clear();
  73.     lcd.setCursor(0,0);
  74.     lcd.print("Inside Menu A");
  75.    
  76.     while(ReadKeypad()!= 'L')
  77.     {
  78.         //Insert Task for Menu A here
  79.        
  80.     }
  81. }
  82. void MenuB()
  83. {  
  84.     lcd.clear();
  85.     lcd.setCursor(0,0);
  86.     lcd.print("Inside Menu B");
  87.    
  88.     while(ReadKeypad()!= 'L')
  89.     {
  90.         //Insert Task for Menu B here
  91.        
  92.     }
  93. }
  94. void MenuC()
  95. {  
  96.     lcd.clear();
  97.     lcd.setCursor(0,0);
  98.     lcd.print("Inside Menu C");
  99.    
  100.     while(ReadKeypad()!= 'L')
  101.     {
  102.         //Insert Task for Menu C here
  103.        
  104.     }
  105. }
  106. void MenuD()
  107. {  
  108.     lcd.clear();
  109.     lcd.setCursor(0,0);
  110.     lcd.print("Inside Menu D");
  111.    
  112.     while(ReadKeypad()!= 'L')
  113.     {
  114.         //Insert Task for Menu D here
  115.        
  116.     }
  117. }
  118.  
  119. void MainMenuDisplay()
  120. {
  121.     lcd.clear();
  122.     lcd.setCursor(0,0);
  123.     switch (mainMenuPage)
  124.     {
  125.         case 1:
  126.           lcd.print("1. Menu A");
  127.           break;
  128.         case 2:
  129.           lcd.print("2. Menu B");
  130.           break;
  131.         case 3:
  132.           lcd.print("3. Menu C");
  133.           break;
  134.         case 4:
  135.           lcd.print("4. Menu D");
  136.           break;
  137.     }
  138. }
  139.  
  140. void MainMenuBtn()
  141. {
  142.     WaitBtnRelease();
  143.     if(btn_push == 'U')
  144.     {
  145.         mainMenuPage++;
  146.         if(mainMenuPage > mainMenuTotal)
  147.           mainMenuPage = 1;
  148.     }
  149.     else if(btn_push == 'D')
  150.     {
  151.         mainMenuPage--;
  152.         if(mainMenuPage == 0)
  153.           mainMenuPage = mainMenuTotal;    
  154.     }
  155.    
  156.     if(mainMenuPage != mainMenuPageOld) //only update display when page change
  157.     {
  158.         MainMenuDisplay();
  159.         mainMenuPageOld = mainMenuPage;
  160.     }
  161. }
  162.  
  163. char ReadKeypad()
  164. {
  165.   /* Keypad button analog Value
  166.   no button pressed 1023
  167.   select  741
  168.   left    503
  169.   down    326
  170.   up      142
  171.   right   0
  172.   */
  173.   keypad_value = analogRead(keypad_pin);
  174.  
  175.   if(keypad_value < 100)
  176.     return 'R';
  177.   else if(keypad_value < 200)
  178.     return 'U';
  179.   else if(keypad_value < 400)
  180.     return 'D';
  181.   else if(keypad_value < 600)
  182.     return 'L';
  183.   else if(keypad_value < 800)
  184.     return 'S';
  185.   else
  186.     return 'N';
  187.  
  188. }
  189.  
  190. void WaitBtnRelease()
  191. {
  192.     while( analogRead(keypad_pin) < 800){}
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement