Advertisement
Hanneman

Encoder Menu Version 3

May 27th, 2014
634
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.20 KB | None | 0 0
  1.     /*
  2.         Original Menu code; Giuseppe Di Cillo (www.coagula.org)
  3.         For Menu Code; Contact: dicillo@coagula.org
  4.        
  5.         Adjusted for use with Incremental Encoder.
  6.         This is an adaptation from a menu working with buttons.
  7.        
  8.         This program is free software: you can redistribute it and/or modify
  9.         it under the terms of the GNU General Public License as published by
  10.         the Free Software Foundation, either version 3 of the License, or
  11.         (at your option) any later version.
  12.  
  13.         This program is distributed in the hope that it will be useful,
  14.         but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.         GNU General Public License for more details.
  17.  
  18.         You should have received a copy of the GNU General Public License
  19.         along with this program.  If not, see <http://www.gnu.org/licenses/>.
  20.     */
  21.  
  22.     /*
  23.     IMPORTANT: to use the menubackend library by Alexander Brevig download it at http://www.arduino.cc/playground/uploads/Profiles/MenuBackend_1-4.zip and add the next code at line 195
  24.        void toRoot() {
  25.           setCurrent( &getRoot() );
  26.        }
  27.     */
  28.  
  29.     #include <MenuBackend.h>    //MenuBackend library - copyright by Alexander Brevig
  30.     #include <LiquidCrystal.h>  //this library is included in the Arduino IDE
  31.    
  32.     // LCD Display array
  33.     LiquidCrystal lcd(12, 11, 5, 4, 9, 7);
  34.    
  35.     //
  36.     // Encoder Pin definitions
  37.     //
  38.     #define encoder0PinA 2
  39.     #define encoder0PinB 3
  40.     //
  41.     // Encoder variables
  42.     //
  43.     int encoder0Pos = 30000;   // variable for when u are not using the Encoder
  44.     int reading;               // For the status of the button
  45.     //
  46.     // Button Pin definitions
  47.     //
  48.     int buttonPinLeft;         // pin for the Up button,  now used for Encoder
  49.     int buttonPinRight;        // pin for the Down button,  now used for Encoder
  50.     const int buttonPinEnter  = 8;   // pin for the Enter button
  51.     //
  52.     // Variables for Encoder    
  53.     //
  54.     int lastButtonPushed = 0;         // saves the state after a turn left or right on encoder
  55.     int lastButtonLeftState  = LOW;   // the previous reading from the Left input pin
  56.     int lastButtonRightState = LOW;   // the previous reading from the Right input pin
  57.     int buttonState;                  // the current reading from the input pin
  58.     int lastButtonState = LOW;        // the previous reading from the input pin
  59.     long lastDebounceTime = 0;        // the last time the output pin was toggled
  60.     long debounceDelayButton = 50;    // the debounce time; increase if the output flickers
  61.     long lastLeftDebounceTime  = 0;   // the last time the output pin was toggled
  62.     long lastRightDebounceTime = 0;   // the last time the output pin was toggled
  63.     long debounceDelay = 1;           // the debounce time, important for Encoder
  64.     //
  65.     // LED flashes when button is pushed
  66.     //
  67.     int ledPin = 13;
  68.  
  69.     //Menu variables
  70.     MenuBackend menu = MenuBackend(menuUsed,menuChanged);
  71.     //initialize menuitems
  72.         MenuItem menu1 = MenuItem("menu1");
  73.         MenuItem menu2 = MenuItem("menu2");
  74.         MenuItem menu3 = MenuItem("menu3");
  75.         MenuItem menu4 = MenuItem("menu4");
  76.         MenuItem menu5 = MenuItem("menu5");
  77.         MenuItem menu6 = MenuItem("menu6");
  78.         MenuItem menu7 = MenuItem("menu7");
  79.         MenuItem menu8 = MenuItem("menu8");
  80.  
  81.  
  82.     void setup()
  83.     {
  84.     pinMode(ledPin, OUTPUT);   // For the LED      
  85.     //
  86.     // Encoder Setup
  87.     //
  88.     pinMode(encoder0PinA, INPUT);
  89.     pinMode(encoder0PinB, INPUT);
  90.     // encoder pin on interrupt 0 (pin 2)
  91.     attachInterrupt(0, doEncoderA, CHANGE);
  92.     // encoder pin on interrupt 1 (pin 3)
  93.     attachInterrupt(1, doEncoderB, CHANGE);
  94.     //
  95.     // Prints this to serial monitor
  96.     Serial.begin (9600); Serial.println("START READING");
  97.     //
  98.     // Button Setup
  99.     //
  100.     pinMode(buttonPinEnter, INPUT);
  101.     digitalWrite(buttonPinEnter, HIGH); // Activates Pull-Up for Encoder button
  102.     //
  103.     // LCD Menu Setup
  104.     //
  105.     lcd.begin(16, 2);
  106.     lcd.setCursor(0,0);
  107.     lcd.print("Hannes's Menu");
  108.     delay(2000);
  109.  
  110.       //configure menu
  111.     menu.getRoot().add(menu1);
  112.       menu1.add(menu2);
  113.       menu2.add(menu3);
  114.       menu3.add(menu4);
  115.       menu4.add(menu5);
  116.       menu5.add(menu6);
  117.       menu6.add(menu7);
  118.       menu7.add(menu8);
  119.     menu.toRoot();
  120.  
  121.     }  
  122.    
  123.     void loop()
  124.     {
  125.     Serial.println(encoder0Pos,DEC); // Prints encoder values in Serial monitor
  126.     //
  127.     // main loop runs continuesly
  128.     //
  129.     readButtons();  
  130.     }
  131.  
  132.     //
  133.     // Encoder voids for left and right turns
  134.     //
  135.     void doEncoderA(){
  136.       // look for a low-to-high on channel A
  137.       if (digitalRead(encoder0PinA) == HIGH) {
  138.         // check channel B to see which way encoder is turning
  139.         if (digitalRead(encoder0PinB) == LOW) {
  140.           encoder0Pos = encoder0Pos + 5;         // CW
  141.         }
  142.         else {
  143.           encoder0Pos = encoder0Pos - 5;         // CCW
  144.         }
  145.       }
  146.       else   // must be a high-to-low edge on channel A                                      
  147.       {
  148.         // check channel B to see which way encoder is turning
  149.         if (digitalRead(encoder0PinB) == HIGH) {  
  150.           encoder0Pos = encoder0Pos + 5;          // CW
  151.         }
  152.         else {
  153.           encoder0Pos = encoder0Pos - 5;          // CCW
  154.         }
  155.       }
  156.      
  157.     }
  158.  
  159.     void doEncoderB(){
  160.       // look for a low-to-high on channel B
  161.       if (digitalRead(encoder0PinB) == HIGH) {  
  162.        // check channel A to see which way encoder is turning
  163.         if (digitalRead(encoder0PinA) == HIGH) {
  164.           encoder0Pos = encoder0Pos + 5;         // CW increments encoder0Pos value with 5
  165.         }
  166.         else {
  167.           encoder0Pos = encoder0Pos - 5;         // CCW
  168.         }
  169.       }
  170.       // Look for a high-to-low on channel B
  171.       else {
  172.         // check channel B to see which way encoder is turning
  173.         if (digitalRead(encoder0PinA) == LOW) {  
  174.           encoder0Pos = encoder0Pos + 5;          // CW
  175.         }
  176.         else {
  177.           encoder0Pos = encoder0Pos - 5;          // CCW
  178.         }
  179.       }
  180.     }
  181.     //
  182.     // Print the choosen menu to Display
  183.     //
  184.     void menuChanged(MenuChangeEvent changed){
  185.      
  186.       MenuItem newMenuItem=changed.to; //get the destination menu
  187.      
  188.       lcd.setCursor(0,1); //set the start position for lcd printing to the second row
  189.      
  190.       if(newMenuItem.getName()==menu.getRoot()){
  191.           lcd.print("Main Menu       ");
  192.       }else if(newMenuItem.getName()=="menu1"){
  193.           lcd.print("1           ");
  194.       }else if(newMenuItem.getName()=="menu2"){
  195.           lcd.print("2");
  196.       }else if(newMenuItem.getName()=="menu3"){
  197.           lcd.print("3   ");
  198.       }else if(newMenuItem.getName()=="menu4"){
  199.           lcd.print("4           ");
  200.       }else if(newMenuItem.getName()=="menu5"){
  201.           lcd.print("5   ");
  202.       }else if(newMenuItem.getName()=="menu6"){
  203.           lcd.print("6   ");
  204.       }else if(newMenuItem.getName()=="menu7"){
  205.           lcd.print("7   ");
  206.       }else if(newMenuItem.getName()=="menu8"){
  207.           lcd.print("8           ");
  208.       }
  209.     }
  210.     //
  211.     // When the button is pushed it, blinks the LED, sets the Current menu and shows it on the Display
  212.     //
  213.     void menuUsed(MenuUseEvent used){
  214.       lcd.setCursor(0,0);
  215.       lcd.print("Current Menu   ");
  216.       lcd.setCursor(0,1);
  217.       lcd.print(used.item.getName());
  218.     }
  219.  
  220.     void  readButtons(){             //read buttons status
  221.       int readingDown;               // For encoder
  222.       int readingUp;                 // For encoder
  223.       int buttonLeftState=LOW;       // the current reading from the input pin
  224.       int buttonRightState=LOW;      // the current reading from the input pin
  225.     //
  226.     // Encoder Button
  227.     //
  228.     // read the state of the switch into a local variable:
  229.     reading = digitalRead(buttonPinEnter);
  230.  
  231.     // check to see if you just pressed the button
  232.     // (i.e. the input went from LOW to HIGH),  and you've waited
  233.     // long enough since the last press to ignore any noise:  
  234.  
  235.     // If the switch changed, due to noise or pressing:
  236.     if (reading != lastButtonState) {
  237.     // reset the debouncing timer
  238.     lastDebounceTime = millis();
  239.     }
  240.  
  241.     if ((millis() - lastDebounceTime) > debounceDelayButton) {
  242.     // whatever the reading is at, it's been there for longer
  243.     // than the debounce delay, so take it as the actual current state:
  244.     buttonState = reading;
  245.     }    
  246.  
  247.     digitalWrite(ledPin, !buttonState); // Pull up resistor is active, when buttonState is NOT high it saves the Current menu
  248.  
  249.       if (buttonState==LOW){
  250.         menu.use();
  251.     }        
  252.          
  253.     lastButtonState = reading;
  254.     //
  255.     // Up button
  256.     //
  257.     readingUp = encoder0Pos; // read the state of the switch into a local variable:
  258.     // check to see if you just pressed the Down button
  259.     // (i.e. the input went from LOW to HIGH),  and you've waited
  260.     // long enough since the last press to ignore any noise:
  261.  
  262.     // If the switch changed, due to noise or pressing:
  263.     if (readingUp != lastButtonRightState) {
  264.     // reset the debouncing timer
  265.         lastRightDebounceTime = millis();
  266.        }
  267.  
  268.     if ((millis() - lastRightDebounceTime) > debounceDelay) {
  269.     // whatever the reading is at, it's been there for longer
  270.     // than the debounce delay, so take it as the actual current state:
  271.        if (encoder0Pos > 30004 ) {
  272.            buttonRightState=buttonPinRight;
  273.              menu.moveUp();
  274.                buttonRightState = readingUp;
  275.                  }
  276.     }
  277.     // save the reading.  Next time through the loop,
  278.     // it'll be the lastButtonState:
  279.     lastButtonRightState = readingUp;    
  280.     //
  281.     // Down button              
  282.     //
  283.     // read the state of the switch into a local variable:
  284.     readingDown = encoder0Pos;
  285.     // check to see if you just pressed the Down button
  286.     // (i.e. the input went from LOW to HIGH),  and you've waited
  287.     // long enough since the last press to ignore any noise:
  288.    
  289.     // If the switch changed, due to noise or pressing:
  290.     if (readingDown != lastButtonLeftState) {
  291.     // reset the debouncing timer
  292.         lastLeftDebounceTime = millis();
  293.       }
  294.    
  295.     if ((millis() - lastLeftDebounceTime) > debounceDelay) {
  296.     // whatever the reading is at, it's been there for longer
  297.     // than the debounce delay, so take it as the actual current state:
  298.     if (encoder0Pos < 29997 ) {
  299.       buttonLeftState=buttonPinLeft;
  300.         menu.moveDown();
  301.           buttonLeftState = readingDown;
  302.             }
  303.     }
  304.    
  305.     // save the reading.  Next time through the loop,
  306.     // it'll be the lastButtonState:
  307.     lastButtonLeftState = readingDown;
  308.     //
  309.     // Determines wich button is pressed
  310.     //
  311.     if(buttonRightState==HIGH){
  312.       lastButtonPushed=buttonPinRight;
  313.         }else{
  314.           encoder0Pos = 30000;
  315.           }
  316.             if(buttonLeftState==HIGH){
  317.               lastButtonPushed=buttonPinLeft;
  318.                 }else{
  319.                   encoder0Pos = 30000;
  320.                     }
  321.      }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement