Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Original Menu code; Giuseppe Di Cillo (www.coagula.org)
- For Menu Code; Contact: dicillo@coagula.org
- Adjusted for use with Incremental Encoder.
- This is an adaptation from a menu working with buttons.
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- /*
- 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
- void toRoot() {
- setCurrent( &getRoot() );
- }
- */
- #include <MenuBackend.h> //MenuBackend library - copyright by Alexander Brevig
- #include <LiquidCrystal.h> //this library is included in the Arduino IDE
- // LCD Display array
- LiquidCrystal lcd(12, 11, 5, 4, 9, 7);
- //
- // Encoder Pin definitions
- //
- #define encoder0PinA 2
- #define encoder0PinB 3
- //
- // Encoder variables
- //
- int encoder0Pos = 30000; // variable for when u are not using the Encoder
- int reading; // For the status of the button
- //
- // Button Pin definitions
- //
- int buttonPinLeft; // pin for the Up button, now used for Encoder
- int buttonPinRight; // pin for the Down button, now used for Encoder
- const int buttonPinEnter = 8; // pin for the Enter button
- //
- // Variables for Encoder
- //
- int lastButtonPushed = 0; // saves the state after a turn left or right on encoder
- int lastButtonLeftState = LOW; // the previous reading from the Left input pin
- int lastButtonRightState = LOW; // the previous reading from the Right input pin
- int buttonState; // the current reading from the input pin
- int lastButtonState = LOW; // the previous reading from the input pin
- long lastDebounceTime = 0; // the last time the output pin was toggled
- long debounceDelayButton = 50; // the debounce time; increase if the output flickers
- long lastLeftDebounceTime = 0; // the last time the output pin was toggled
- long lastRightDebounceTime = 0; // the last time the output pin was toggled
- long debounceDelay = 1; // the debounce time, important for Encoder
- //
- // LED flashes when button is pushed
- //
- int ledPin = 13;
- //Menu variables
- MenuBackend menu = MenuBackend(menuUsed,menuChanged);
- //initialize menuitems
- MenuItem menu1 = MenuItem("menu1");
- MenuItem menu2 = MenuItem("menu2");
- MenuItem menu3 = MenuItem("menu3");
- MenuItem menu4 = MenuItem("menu4");
- MenuItem menu5 = MenuItem("menu5");
- MenuItem menu6 = MenuItem("menu6");
- MenuItem menu7 = MenuItem("menu7");
- MenuItem menu8 = MenuItem("menu8");
- void setup()
- {
- pinMode(ledPin, OUTPUT); // For the LED
- //
- // Encoder Setup
- //
- pinMode(encoder0PinA, INPUT);
- pinMode(encoder0PinB, INPUT);
- // encoder pin on interrupt 0 (pin 2)
- attachInterrupt(0, doEncoderA, CHANGE);
- // encoder pin on interrupt 1 (pin 3)
- attachInterrupt(1, doEncoderB, CHANGE);
- //
- // Prints this to serial monitor
- Serial.begin (9600); Serial.println("START READING");
- //
- // Button Setup
- //
- pinMode(buttonPinEnter, INPUT);
- digitalWrite(buttonPinEnter, HIGH); // Activates Pull-Up for Encoder button
- //
- // LCD Menu Setup
- //
- lcd.begin(16, 2);
- lcd.setCursor(0,0);
- lcd.print("Hannes's Menu");
- delay(2000);
- //configure menu
- menu.getRoot().add(menu1);
- menu1.add(menu2);
- menu2.add(menu3);
- menu3.add(menu4);
- menu4.add(menu5);
- menu5.add(menu6);
- menu6.add(menu7);
- menu7.add(menu8);
- menu.toRoot();
- }
- void loop()
- {
- Serial.println(encoder0Pos,DEC); // Prints encoder values in Serial monitor
- //
- // main loop runs continuesly
- //
- readButtons();
- }
- //
- // Encoder voids for left and right turns
- //
- void doEncoderA(){
- // look for a low-to-high on channel A
- if (digitalRead(encoder0PinA) == HIGH) {
- // check channel B to see which way encoder is turning
- if (digitalRead(encoder0PinB) == LOW) {
- encoder0Pos = encoder0Pos + 5; // CW
- }
- else {
- encoder0Pos = encoder0Pos - 5; // CCW
- }
- }
- else // must be a high-to-low edge on channel A
- {
- // check channel B to see which way encoder is turning
- if (digitalRead(encoder0PinB) == HIGH) {
- encoder0Pos = encoder0Pos + 5; // CW
- }
- else {
- encoder0Pos = encoder0Pos - 5; // CCW
- }
- }
- }
- void doEncoderB(){
- // look for a low-to-high on channel B
- if (digitalRead(encoder0PinB) == HIGH) {
- // check channel A to see which way encoder is turning
- if (digitalRead(encoder0PinA) == HIGH) {
- encoder0Pos = encoder0Pos + 5; // CW increments encoder0Pos value with 5
- }
- else {
- encoder0Pos = encoder0Pos - 5; // CCW
- }
- }
- // Look for a high-to-low on channel B
- else {
- // check channel B to see which way encoder is turning
- if (digitalRead(encoder0PinA) == LOW) {
- encoder0Pos = encoder0Pos + 5; // CW
- }
- else {
- encoder0Pos = encoder0Pos - 5; // CCW
- }
- }
- }
- //
- // Print the choosen menu to Display
- //
- void menuChanged(MenuChangeEvent changed){
- MenuItem newMenuItem=changed.to; //get the destination menu
- lcd.setCursor(0,1); //set the start position for lcd printing to the second row
- if(newMenuItem.getName()==menu.getRoot()){
- lcd.print("Main Menu ");
- }else if(newMenuItem.getName()=="menu1"){
- lcd.print("1 ");
- }else if(newMenuItem.getName()=="menu2"){
- lcd.print("2");
- }else if(newMenuItem.getName()=="menu3"){
- lcd.print("3 ");
- }else if(newMenuItem.getName()=="menu4"){
- lcd.print("4 ");
- }else if(newMenuItem.getName()=="menu5"){
- lcd.print("5 ");
- }else if(newMenuItem.getName()=="menu6"){
- lcd.print("6 ");
- }else if(newMenuItem.getName()=="menu7"){
- lcd.print("7 ");
- }else if(newMenuItem.getName()=="menu8"){
- lcd.print("8 ");
- }
- }
- //
- // When the button is pushed it, blinks the LED, sets the Current menu and shows it on the Display
- //
- void menuUsed(MenuUseEvent used){
- lcd.setCursor(0,0);
- lcd.print("Current Menu ");
- lcd.setCursor(0,1);
- lcd.print(used.item.getName());
- }
- void readButtons(){ //read buttons status
- int readingDown; // For encoder
- int readingUp; // For encoder
- int buttonLeftState=LOW; // the current reading from the input pin
- int buttonRightState=LOW; // the current reading from the input pin
- //
- // Encoder Button
- //
- // read the state of the switch into a local variable:
- reading = digitalRead(buttonPinEnter);
- // check to see if you just pressed the button
- // (i.e. the input went from LOW to HIGH), and you've waited
- // long enough since the last press to ignore any noise:
- // If the switch changed, due to noise or pressing:
- if (reading != lastButtonState) {
- // reset the debouncing timer
- lastDebounceTime = millis();
- }
- if ((millis() - lastDebounceTime) > debounceDelayButton) {
- // whatever the reading is at, it's been there for longer
- // than the debounce delay, so take it as the actual current state:
- buttonState = reading;
- }
- digitalWrite(ledPin, !buttonState); // Pull up resistor is active, when buttonState is NOT high it saves the Current menu
- if (buttonState==LOW){
- menu.use();
- }
- lastButtonState = reading;
- //
- // Up button
- //
- readingUp = encoder0Pos; // read the state of the switch into a local variable:
- // check to see if you just pressed the Down button
- // (i.e. the input went from LOW to HIGH), and you've waited
- // long enough since the last press to ignore any noise:
- // If the switch changed, due to noise or pressing:
- if (readingUp != lastButtonRightState) {
- // reset the debouncing timer
- lastRightDebounceTime = millis();
- }
- if ((millis() - lastRightDebounceTime) > debounceDelay) {
- // whatever the reading is at, it's been there for longer
- // than the debounce delay, so take it as the actual current state:
- if (encoder0Pos > 30004 ) {
- buttonRightState=buttonPinRight;
- menu.moveUp();
- buttonRightState = readingUp;
- }
- }
- // save the reading. Next time through the loop,
- // it'll be the lastButtonState:
- lastButtonRightState = readingUp;
- //
- // Down button
- //
- // read the state of the switch into a local variable:
- readingDown = encoder0Pos;
- // check to see if you just pressed the Down button
- // (i.e. the input went from LOW to HIGH), and you've waited
- // long enough since the last press to ignore any noise:
- // If the switch changed, due to noise or pressing:
- if (readingDown != lastButtonLeftState) {
- // reset the debouncing timer
- lastLeftDebounceTime = millis();
- }
- if ((millis() - lastLeftDebounceTime) > debounceDelay) {
- // whatever the reading is at, it's been there for longer
- // than the debounce delay, so take it as the actual current state:
- if (encoder0Pos < 29997 ) {
- buttonLeftState=buttonPinLeft;
- menu.moveDown();
- buttonLeftState = readingDown;
- }
- }
- // save the reading. Next time through the loop,
- // it'll be the lastButtonState:
- lastButtonLeftState = readingDown;
- //
- // Determines wich button is pressed
- //
- if(buttonRightState==HIGH){
- lastButtonPushed=buttonPinRight;
- }else{
- encoder0Pos = 30000;
- }
- if(buttonLeftState==HIGH){
- lastButtonPushed=buttonPinLeft;
- }else{
- encoder0Pos = 30000;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement