Advertisement
Guest User

Arduino LCD I2C Menu (Does not work)

a guest
Dec 2nd, 2013
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.73 KB | None | 0 0
  1. //Date 11/27/13 00:41 EST
  2. //Name: MENU_TEST2
  3.  
  4. //includes
  5. #include <Wire.h>
  6. #include <LiquidCrystal_I2C.h>
  7. #include <MenuBackend.h>
  8.  
  9. LiquidCrystal_I2C lcd(0x27,16,2); //addr 0x27,16 char,2 row
  10.  
  11. const int buttonPinU = 2; // pin for Up Button
  12. const int buttonPinD = 3; //pin for down btn
  13. const int buttonPinL = 4; // pin for left btn
  14. const int buttonPinR = 5; // pin for right btn
  15. const int buttonPinEnter = 6; // pin for return btn
  16. const int buttonPinEsc = 7; // pin for ESC btn
  17.  
  18. int led = 13;
  19.  
  20. #define DELAY 100; //Default value of this integer will be 100ms
  21.  
  22. int lastButtonPushed = 0; // Compares the button state
  23.  
  24. int lastButtonEnterState = LOW; // the previous reading from the Enter input pin
  25. int lastButtonEscState = LOW; // the previous reading from the ESC input pin
  26. int lastButtonRState = LOW; // the previous reading from the R button
  27. int lastButtonLState = LOW; // the previous reading from the L button
  28. int lastButtonDState = LOW; // the previous reading from the D button
  29. int lastButtonUState = LOW; // the previous reading from the U button
  30.  
  31. int resetPin = 12; // Reset pin is 12
  32.  
  33. // holding a button down causes it to scroll through items - these int's will count incremently until it "presses" again.
  34.  
  35. long lastEnterDebounceTime = 0; // the last time the output pin was toggled
  36. long lastEscDebounceTime = 0;
  37. long lastLDebounceTime = 0;
  38. long lastRDebounceTime = 0;
  39. long lastDDebounceTime = 0;
  40. long lastUDebounceTime = 0;
  41. long debounceDelay = 500; // debounce time = 500 ms or 0.5 Sec.
  42.  
  43. long currentmillis = 0;
  44.  
  45.  
  46. /*===============================================================
  47. || Simplified menu test...
  48. || /root
  49. ||    |_uptime
  50. ||    |_settings
  51. ||    |        |_Blink LED
  52. ||    |                  |_d100 (delay for 100ms)
  53. ||    |                  |_d200 (delay for 200ms)
  54. ||    |                  |_d300 (delay for 300ms)
  55. ||    |                  |_d400 (delay for 400ms)
  56. ||    |_reset
  57. \\=============================================================*/
  58.  
  59. MenuBackend menu = MenuBackend(menuUseEvent,menuChanged);
  60.   MenuItem time = MenuItem("Uptime");
  61.   MenuItem set = MenuItem("Settings");
  62.     MenuItem setblink = MenuItem("LED Blink Rate");
  63.       MenuItem d100 = MenuItem("100ms Harder");
  64.       MenuItem d200 = MenuItem("200ms Faster");
  65.       MenuItem d300 = MenuItem("300ms Stronger");
  66.       MenuItem d400 = MenuItem("400ms Better");
  67.   MenuItem abt = MenuItem("About");
  68.   MenuItem rst = MenuItem("Reset");
  69.  
  70. void menuSetup(){
  71.   menu.getRoot().add(time);
  72.   time.addBefore(set);
  73.   time.addAfter(rst);
  74.   set.addAfter(time);
  75.   set.addRight(setblink);
  76.           setblink.addLeft(set);
  77.           setblink.addRight(d100);
  78.           d100.addBefore(d200);
  79.           d200.addAfter(d100);
  80.           d300.addAfter(d200);
  81.           d400.addAfter(d300);
  82.           d400.addBefore(d100);
  83.           //Anytime you select left, you will go back into parent menu
  84.           d100.addLeft(setblink);
  85.           d200.addLeft(setblink);
  86.           d300.addLeft(setblink);
  87.           d400.addLeft(setblink);
  88.   abt.addAfter(set);
  89.   abt.addBefore(rst);
  90.   rst.addAfter(abt);
  91.   rst.addBefore(time);
  92. }
  93.  
  94. //Root menu
  95. void menuChanged(MenuChangeEvent changed){
  96.   lcd.clear();
  97.   lcd.print("Main Menu");
  98.   lcd.setCursor(0, 1);
  99.   lcd.print(changed.to.getName());
  100.  
  101.  
  102. //Uptime block
  103.   long days = 0;
  104.   long hours = 0;
  105.   long mins = 0;
  106.   long secs = 0;
  107.   secs=currentmillis/1000; //divide milliseconds by 1000 - get seconds
  108.   mins=secs/60; //divide secs by 60, get minutes
  109.   hours=mins/60; //divide mins by 60, get hours
  110.   days=hours/24; //divide hours by 24, get days
  111.   secs=secs-(mins*60);
  112.   mins=mins-(hours*60);
  113.   hours=hours-(days*24);
  114.  
  115.   MenuItem newMenuItem=changed.to;
  116.  
  117.   if(newMenuItem.getName()==menu.getRoot()){
  118.     lcd.print("Root");
  119.   }else if(newMenuItem.getName()=="Uptime"){
  120.     lcd.clear();
  121.     lcd.setCursor(0, 0);
  122.     lcd.print("Uptime");
  123.     lcd.setCursor(0, 1);
  124.     lcd.print(hours);
  125.     lcd.print(":");
  126.     lcd.print(mins);
  127.     lcd.print(":");
  128.     lcd.print(secs);
  129.   }else if(newMenuItem.getName()=="Settings"){
  130.     lcd.print("LED Blink Rate");
  131.   }else if(newMenuItem.getName()=="LED Blink Rate"){
  132.     //I'm not really sure how to configure this. I want to have a list of options, a menu within a menu basically...
  133.     //so... I use lcd.print(changed.to.getName());
  134.     lcd.clear();
  135.     lcd.setCursor(0, 0);
  136.     lcd.print("LED Blink Rates:");
  137.     lcd.setCursor(0, 1);
  138.     lcd.print(changed.to.getName());  //Experimental...
  139. // For the next 4 values, I want to change x of delay(x); for the blinking LED loop to 100ms, 200ms, 300ms, or 400ms
  140.   }else if(newMenuItem.getName()=="d100"){
  141.       digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  142.       delay(100);               // wait for a second
  143.       digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  144.       delay(100);  
  145.      
  146.       //OR we do this...
  147.       //(DELAY, '100'); //basically default...
  148.      
  149.   }else if(newMenuItem.getName()=="d200"){
  150.       digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  151.       delay(200);               // wait for a second
  152.       digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  153.       delay(200);  
  154.       //(DELAY, '200');
  155.   }else if(newMenuItem.getName()=="d300"){
  156.       digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  157.       delay(300);               // wait for a second
  158.       digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  159.       delay(300);  
  160.       //(DELAY, '300');
  161.   }else if(newMenuItem.getName()=="d400"){
  162.       digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  163.       delay(400);               // wait for a second
  164.       digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  165.       delay(400);  
  166.       //(DELAY, '400');
  167.   }else if(newMenuItem.getName()=="rst"){  //This is to Reset the arduino from the buttons. This will be needed because the board is inside the machine casing.
  168.     lcd.print("Resetting...");
  169.     delay(500);
  170.     digitalWrite(resetPin, LOW);
  171.   }
  172. }
  173.  
  174. void menuUseEvent(MenuUseEvent used){
  175.   //I would like to not use this, but for now I'm keeping it for testing purposes.
  176.   lcd.clear();
  177.   lcd.setCursor(0, 0);
  178.   lcd.print("You used:");
  179.   lcd.setCursor(0, 1);
  180.   lcd.print(used.item.getName());
  181.   delay(1000); //Show this screen for a second, then return to the menu
  182.   menu.toRoot();
  183. }
  184. void readButtons(){ //read buttons status
  185.   int reading;
  186.   int buttonEnterState=LOW; // the current reading from this button
  187.   int buttonEscState=LOW; // the current reading from this button
  188.   int buttonRState=LOW; // the current reading from this button
  189.   int buttonLState=LOW; //the current reading from this button
  190.   int buttonDState=LOW; // the current reading from this button
  191.   int buttonUState=LOW; // the current reading from this button
  192.  
  193. //Enter button
  194.                   // read the state of the switch into a local variable:
  195.                   reading = digitalRead(buttonPinEnter);
  196.  
  197.                   // check to see if you just pressed the enter button
  198.                   // (i.e. the input went from LOW to HIGH),  and you've waited
  199.                   // long enough since the last press to ignore any noise:  
  200.                
  201.                   // If the switch changed, due to noise or pressing:
  202.                   if (reading != lastButtonEnterState) {
  203.                     // reset the debouncing timer
  204.                     lastEnterDebounceTime = millis();
  205.                   }
  206.                  
  207.                   if ((millis() - lastEnterDebounceTime) > debounceDelay) {
  208.                     // whatever the reading is at, it's been there for longer
  209.                     // than the debounce delay, so take it as the actual current state:
  210.                     buttonEnterState=reading;
  211.                     lastEnterDebounceTime=millis();
  212.                   }
  213.                  
  214.                   // save the reading.  Next time through the loop,
  215.                   // it'll be the lastButtonState:
  216.                   lastButtonEnterState = reading;
  217.                  
  218. //Esc button              
  219.                   // read the state of the switch into a local variable:
  220.                   reading = digitalRead(buttonPinEsc);
  221.  
  222.                   // check to see if you just pressed the Down button
  223.                   // (i.e. the input went from LOW to HIGH),  and you've waited
  224.                   // long enough since the last press to ignore any noise:  
  225.                
  226.                   // If the switch changed, due to noise or pressing:
  227.                   if (reading != lastButtonEscState) {
  228.                     // reset the debouncing timer
  229.                     lastEscDebounceTime = millis();
  230.                   }
  231.                  
  232.                   if ((millis() - lastEscDebounceTime) > debounceDelay) {
  233.                     // whatever the reading is at, it's been there for longer
  234.                     // than the debounce delay, so take it as the actual current state:
  235.                     buttonEscState = reading;
  236.                     lastEscDebounceTime=millis();
  237.                   }
  238.                  
  239.                   // save the reading.  Next time through the loop,
  240.                   // it'll be the lastButtonState:
  241.                   lastButtonEscState = reading;
  242.                  
  243. //LEFT Button
  244.                   reading = digitalRead(buttonPinL);
  245.                  
  246.                   if (reading != lastButtonLState) {
  247.                     lastLDebounceTime = millis();
  248.                   }
  249.                  
  250.                   if((millis() - lastLDebounceTime) > debounceDelay) {
  251.                     buttonLState=reading;
  252.                     lastLDebounceTime=millis();
  253.                   }
  254.                  
  255.                   lastButtonLState = reading;
  256.                  
  257. //RIGHT Button
  258.                   reading = digitalRead(buttonPinR);
  259.                  
  260.                   if (reading != lastButtonRState) {
  261.                     lastRDebounceTime = millis();
  262.                   }
  263.                  
  264.                   if((millis() - lastRDebounceTime) > debounceDelay) {
  265.                     buttonRState=reading;
  266.                     lastRDebounceTime=millis();
  267.                   }
  268.                  
  269.                   lastButtonRState = reading;
  270.  
  271. //DOWN Button
  272.                   reading = digitalRead(buttonPinD);
  273.                  
  274.                   if (reading != lastButtonDState) {
  275.                     lastDDebounceTime = millis();
  276.                   }
  277.                  
  278.                   if(millis() - lastDDebounceTime > debounceDelay) {
  279.                     buttonDState=reading;
  280.                     lastDDebounceTime=millis();
  281.                   }
  282.                  
  283.                   lastButtonDState = reading;
  284.                  
  285. //UP Button
  286.                   reading = digitalRead(buttonPinU);
  287.                  
  288.                   if (reading != lastButtonUState) {
  289.                     lastUDebounceTime = millis();
  290.                   }
  291.                  
  292.                   if((millis() - lastUDebounceTime) > debounceDelay) {
  293.                     buttonUState=reading;
  294.                     lastUDebounceTime=millis();
  295.                   }
  296.                  
  297.                   lastButtonUState = reading;
  298.                  
  299.                    //records which button has been pressed
  300.                   if (buttonEnterState==HIGH){
  301.                     lastButtonPushed=buttonPinEnter;
  302.  
  303.                   }else if(buttonEscState==HIGH){
  304.                     lastButtonPushed=buttonPinEsc;
  305.  
  306.                   }else if(buttonRState==HIGH){
  307.                     lastButtonPushed=buttonPinR;
  308.  
  309.                   }else if(buttonLState==HIGH){
  310.                     lastButtonPushed=buttonPinL;
  311.                  
  312.                   }else if(buttonUState==HIGH){
  313.                     lastButtonPushed=buttonPinU;
  314.                    
  315.                   }else if(buttonDState==HIGH){
  316.                     lastButtonPushed=buttonPinD;
  317.                    
  318.                   }else{
  319.                     lastButtonPushed=0;
  320.                   }                  
  321. }
  322. void navigateMenus() {
  323.   MenuItem currentMenu=menu.getCurrent();
  324.  
  325.   switch (lastButtonPushed){
  326.     case buttonPinEnter:
  327.       if(!(currentMenu.moveDown())){
  328.         menu.use();
  329.       }
  330.       break;
  331.     case buttonPinEsc: menu.getRoot(); //back to main
  332.       break;
  333.     case buttonPinR: menu.moveRight();
  334.       break;
  335.     case buttonPinL: menu.moveLeft();
  336.       break;
  337.     case buttonPinU: menu.moveUp();
  338.       break;
  339.     case buttonPinD: menu.moveDown();
  340.       break;
  341.    
  342.   }
  343.  
  344.   lastButtonPushed=0; //reset the lastButtonPushed variable
  345. }
  346. void setup()
  347. {
  348.   //start the LCD
  349.   lcd.backlight();
  350.   lcd.init();
  351.  
  352.   //Set buttons as inputs
  353.   pinMode(buttonPinU, INPUT); // Set pin 2 as Input
  354.   pinMode(buttonPinD, INPUT); // Set pin 3 as Input
  355.   pinMode(buttonPinL, INPUT); // Set pin 4 as Input
  356.   pinMode(buttonPinR, INPUT); // Set pin 5 as Input
  357.   pinMode(buttonPinEnter, INPUT); // Set pin 6 as Input
  358.   pinMode(buttonPinEsc, INPUT); // Set pin 7 as Input
  359.  
  360.   pinMode(led, OUTPUT);
  361.    
  362.   digitalWrite(resetPin, HIGH);
  363.   delay(200);
  364.    
  365.   // start up screen...  
  366.   lcd.setCursor(2, 0);
  367.   lcd.print("Arduino2560");
  368.   lcd.setCursor(1, 1);
  369.   lcd.print("Initialized");
  370.  
  371.   delay(500);
  372.   lcd.print(".");
  373.   delay(500);
  374.   lcd.print(".");
  375.   delay(500);
  376.   lcd.print(".");
  377.   delay(1000);
  378.   lcd.clear();
  379. }
  380.    
  381. void loop(){
  382.   //LED Blink
  383.   digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  384.   delay(DELAY);               // Use DELAY Integer
  385.   digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  386.   delay(DELAY);  
  387.   menuSetup();
  388.   readButtons();
  389.   navigateMenus();
  390. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement