Hälp att programera en meny med arduino och LCD

PIC, AVR, Arduino, Raspberry Pi, Basic Stamp, PLC mm.
Karlsson25
Inlägg: 22
Blev medlem: 10 september 2012, 12:50:50

Hälp att programera en meny med arduino och LCD

Inlägg av Karlsson25 »

Hejsan!
Jag håller på med ett projekt där jag ska programera en meny och navigera mig med 4 olika knappar. Då hittade jag denna sidan:
http://www.coagula.org/content/pages/tu ... ay-arduino. Jag har fått denna att fungera och så, bara det att jag vill ha mer än 3 menyer.
Så jag undrar om någon har tid att ta en titt och se om det går att göra så man har fler menyer.

Tack på förhand!
mattswe
Inlägg: 159
Blev medlem: 20 augusti 2012, 13:27:51

Re: Hälp att programera en meny med arduino och LCD

Inlägg av mattswe »

Det går säkert att åstadkomma det du önskar. Jag har inte tittat i lib-filerna, och endast kastat en mycket snabb blick på den kod du länkar till. Genom att göra lämpliga ändringar under kommentarerna "initialize menuitems", "configure menu" samt i funktionen "menuChanged" så bör du kunna lägga till fler menygrenar.
Karlsson25
Inlägg: 22
Blev medlem: 10 september 2012, 12:50:50

Re: Hälp att programera en meny med arduino och LCD

Inlägg av Karlsson25 »

Jag har provat detta, men ja lyckas bara få fyra menyer.. ändå har lagt till exakt samma kommandon på den femte menyn som de resterande fyra.

Här är koden jag har jobbat med

Kod: Markera allt

 
/*
    Copyright Giuseppe Di Cillo (www.coagula.org)
    Contact: dicillo@coagula.org
    
    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

const int buttonPinLeft = 8;      // pin for the Up button
const int buttonPinRight = 9;    // pin for the Down button
const int buttonPinEsc = 11;     // pin for the Enter button
const int buttonPinEnter = 10;   // pin for the Esc button

int lastButtonPushed = 0;

int lastButtonEnterState = LOW;   // the previous reading from the Enter input pin
int lastButtonEscState = LOW;   // the previous reading from the Esc input pin
int lastButtonLeftState = LOW;   // the previous reading from the Left input pin
int lastButtonRightState = LOW;   // the previous reading from the Right input pin
int ledPin = 13;
int ledPon = 12;

long lastEnterDebounceTime = 0;  // the last time the output pin was toggled
long lastEscDebounceTime = 0;  // the last time the output pin was toggled
long lastLeftDebounceTime = 0;  // the last time the output pin was toggled
long lastRightDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 500;    // the debounce time

// LiquidCrystal display with:
// rs on pin 7
// rw on ground
// enable on pin 6
// d4, d5, d6, d7 on pins 5, 4, 3, 2
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

//Menu variables
MenuBackend menu = MenuBackend(menuUsed,menuChanged);
//initialize menuitems
    MenuItem menu1Item1 = MenuItem("Item1");
      MenuItem menuItem1SubItem1 = MenuItem("Item1SubItem1");
      MenuItem menuItem1SubItem2 = MenuItem("Item1SubItem2");
    MenuItem menu1Item2 = MenuItem("Item2");
      MenuItem menuItem2SubItem1 = MenuItem("Item2SubItem1");
      MenuItem menuItem2SubItem2 = MenuItem("Item2SubItem2");
    MenuItem menu1Item3 = MenuItem("Item3");
      MenuItem menuItem3SubItem1 = MenuItem("Item3SubItem1");
      MenuItem menuItem3SubItem2 = MenuItem("Item3SubItem2");
    MenuItem menu1Item4 = MenuItem("Item4");
      MenuItem menuItem4SubItem1 = MenuItem("Item4SubItem1");
      MenuItem menuItem4SubItem2 = MenuItem("Item4SubItem2");
    MenuItem menu1Item5 = MenuItem("Item5");
      MenuItem menuItem5SubItem1 = MenuItem("Item5SubItem1");
      MenuItem menuItem5SubItem2 = MenuItem("Item5SubItem2");
    MenuItem menu1Item6 = MenuItem("Item6");
      MenuItem menuItem6SubItem1 = MenuItem("Item6SubItem1");
      MenuItem menuItem6SubItem2 = MenuItem("Item6SubItem2");
    


void setup()
{
  pinMode(buttonPinLeft, INPUT);
  pinMode(buttonPinRight, INPUT);
  pinMode(buttonPinEnter, INPUT);
  pinMode(buttonPinEsc, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(ledPon, OUTPUT);
  
  lcd.begin(20, 4);

  //configure menu
  menu.getRoot().add(menu1Item1);
  menu1Item1.addRight(menu1Item2).addRight(menu1Item3).addRight(menu1Item4);
  menu1Item1.add(menuItem1SubItem1).addRight(menuItem1SubItem2);
  menu1Item2.add(menuItem2SubItem1).addRight(menuItem2SubItem2);
  menu1Item3.add(menuItem3SubItem1).addRight(menuItem3SubItem2);
  menu1Item4.add(menuItem4SubItem1).addRight(menuItem4SubItem2);
  menu1Item5.add(menuItem5SubItem1).addRight(menuItem5SubItem2);
  menu1Item6.add(menuItem6SubItem1).addRight(menuItem6SubItem2);
  menu.toRoot();
  lcd.setCursor(0,0);  
  lcd.print("Menu");

}  // setup()...


void loop()
{

  readButtons();  //I splitted button reading and navigation in two procedures because 
  navigateMenus();  //in some situations I want to use the button for other purpose (eg. to change some settings)
                  
} //loop()... 


void menuChanged(MenuChangeEvent changed){
  
  MenuItem newMenuItem=changed.to; //get the destination menu
  
  lcd.setCursor(0,0); //set the start position for lcd printing to the second row
  
  if(newMenuItem.getName()==menu.getRoot()){
      lcd.print("                    Press enter         Main Menu    ");
  }else if(newMenuItem.getName()=="Item1"){
      lcd.print("Acton 1                                              ");
  }else if(newMenuItem.getName()=="Item1SubItem1"){
      lcd.print("Performing action                              ");
      {digitalWrite(ledPin, HIGH);
      delay(1000);
      digitalWrite(ledPin, LOW);
      delay(1000);
    digitalWrite(ledPin, HIGH);
      delay(1000);
      digitalWrite(ledPin, LOW);
      delay(1000);
    digitalWrite(ledPin, HIGH);
      delay(1000);
      digitalWrite(ledPin, LOW);
      delay(1000);
      lcd.setCursor(0,0);
      lcd.print("press Enter                                                ");
      }
    
      
    
  }else if(newMenuItem.getName()=="Item2"){
      lcd.print("Action 2                                               ");
  }else if(newMenuItem.getName()=="Item2SubItem1"){
      lcd.print("Performing action                                         ");
       {digitalWrite(ledPon, HIGH);
      delay(1000);
      digitalWrite(ledPon, LOW);
      delay(1000);
    digitalWrite(ledPon, HIGH);
      delay(1000);
      digitalWrite(ledPon, LOW);
      delay(1000);
    digitalWrite(ledPon, HIGH);
      delay(1000);
      digitalWrite(ledPon, LOW);
      delay(1000);}
  }else if(newMenuItem.getName()=="Item2SubItem2"){
      lcd.print("Item2SubItem2   ");
      
      
  }else if(newMenuItem.getName()=="Item3"){
      lcd.print("Item3                                                     ");
  }else if(newMenuItem.getName()=="Item3SubItem1"){
      lcd.print("Performing action       ");
      {digitalWrite(ledPin, HIGH);
      delay(1000);
      digitalWrite(ledPin, LOW);
      delay(1000);
    digitalWrite(ledPin, HIGH);
      delay(1000);
      digitalWrite(ledPin, LOW);
      delay(1000);
    digitalWrite(ledPin, HIGH);
      delay(1000);
      digitalWrite(ledPin, LOW);
      delay(1000);
      lcd.setCursor(0,0);
      lcd.print("press Enter                                           ");
      }
  }else if(newMenuItem.getName()=="Item3SubItem2"){
      lcd.print("Item3SubItem2   ");
      
      
  }else if(newMenuItem.getName()=="Item4"){
      lcd.print("Item4                                                     ");
  }else if(newMenuItem.getName()=="Item4SubItem1"){
      lcd.print("Performing action        ");
      {digitalWrite(ledPin, HIGH);
      delay(1000);
      digitalWrite(ledPin, LOW);
      delay(1000);
    digitalWrite(ledPin, HIGH);
      delay(1000);
      digitalWrite(ledPin, LOW);
      delay(1000);
    digitalWrite(ledPin, HIGH);
      delay(1000);
      digitalWrite(ledPin, LOW);
      delay(1000);
      lcd.setCursor(0,0);
      lcd.print("Groggen klar        Tryck Enter                       ");
      }
  }else if(newMenuItem.getName()=="Item4SubItem2"){
      lcd.print("Item4SubItem2   ");
      
      
  }else if(newMenuItem.getName()=="Item5"){
    lcd.setCursor(0,0);  
    lcd.print("Item5                                                     ");
  }else if(newMenuItem.getName()=="Item5SubItem1"){
      lcd.print("Performing action        ");
      {digitalWrite(ledPin, HIGH);
      delay(1000);
      digitalWrite(ledPin, LOW);
      delay(1000);
    digitalWrite(ledPin, HIGH);
      delay(1000);
      digitalWrite(ledPin, LOW);
      delay(1000);
    digitalWrite(ledPin, HIGH);
      delay(1000);
      digitalWrite(ledPin, LOW);
      delay(1000);
      lcd.setCursor(0,0);
      lcd.print("Press Enter                                    ");
      }
  }else if(newMenuItem.getName()=="Item5SubItem2"){
      lcd.print("Item5SubItem2   "); }
      else if(newMenuItem.getName()=="Item6"){
      lcd.print("Item6                                                     ");
  }else if(newMenuItem.getName()=="Item6SubItem1"){
      lcd.print("Performing action       ");
      {digitalWrite(ledPin, HIGH);
      delay(1000);
      digitalWrite(ledPin, LOW);
      delay(1000);
    digitalWrite(ledPin, HIGH);
      delay(1000);
      digitalWrite(ledPin, LOW);
      delay(1000);
    digitalWrite(ledPin, HIGH);
      delay(1000);
      digitalWrite(ledPin, LOW);
      delay(1000);
      lcd.setCursor(0,0);
      lcd.print("Press Enter                                        ");
      }
  }else if(newMenuItem.getName()=="Item6SubItem2"){
      lcd.print("Item6SubItem2   ");
  }
}
  

void menuUsed(MenuUseEvent used){
  lcd.setCursor(0,0);  
  lcd.print("You chose                                                 ");
  lcd.setCursor(0,1); 
  lcd.print(used.item.getName());
  delay(3000);  //delay to allow message reading
  lcd.setCursor(0,0);  
  lcd.print("www.coagula.org");
  menu.toRoot();  //back to Main
}


void  readButtons(){  //read buttons status
  int reading;
  int buttonEnterState=LOW;             // the current reading from the Enter input pin
  int buttonEscState=LOW;             // the current reading from the input pin
  int buttonLeftState=LOW;             // the current reading from the input pin
  int buttonRightState=LOW;             // the current reading from the input pin

  //Enter button
                  // read the state of the switch into a local variable:
                  reading = digitalRead(buttonPinEnter);

                  // check to see if you just pressed the enter 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 != lastButtonEnterState) {
                    // reset the debouncing timer
                    lastEnterDebounceTime = millis();
                  } 
                  
                  if ((millis() - lastEnterDebounceTime) > debounceDelay) {
                    // whatever the reading is at, it's been there for longer
                    // than the debounce delay, so take it as the actual current state:
                    buttonEnterState=reading;
                    lastEnterDebounceTime=millis();
                  }
                  
                  // save the reading.  Next time through the loop,
                  // it'll be the lastButtonState:
                  lastButtonEnterState = reading;
                  

    //Esc button               
                  // read the state of the switch into a local variable:
                  reading = digitalRead(buttonPinEsc);

                  // 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 (reading != lastButtonEscState) {
                    // reset the debouncing timer
                    lastEscDebounceTime = millis();
                  } 
                  
                  if ((millis() - lastEscDebounceTime) > debounceDelay) {
                    // whatever the reading is at, it's been there for longer
                    // than the debounce delay, so take it as the actual current state:
                    buttonEscState = reading;
                    lastEscDebounceTime=millis();
                  }
                  
                  // save the reading.  Next time through the loop,
                  // it'll be the lastButtonState:
                  lastButtonEscState = reading; 
                  
                     
   //Down button               
                  // read the state of the switch into a local variable:
                  reading = digitalRead(buttonPinRight);

                  // 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 (reading != 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:
                    buttonRightState = reading;
                   lastRightDebounceTime =millis();
                  }
                  
                  // save the reading.  Next time through the loop,
                  // it'll be the lastButtonState:
                  lastButtonRightState = reading;                  
                  
                  
    //Up button               
                  // read the state of the switch into a local variable:
                  reading = digitalRead(buttonPinLeft);

                  // 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 (reading != 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:
                    buttonLeftState = reading;
                    lastLeftDebounceTime=millis();;
                  }
                  
                  // save the reading.  Next time through the loop,
                  // it'll be the lastButtonState:
                  lastButtonLeftState = reading;  

                  //records which button has been pressed
                  if (buttonEnterState==HIGH){
                    lastButtonPushed=buttonPinEnter;

                  }else if(buttonEscState==HIGH){
                    lastButtonPushed=buttonPinEsc;

                  }else if(buttonRightState==HIGH){
                    lastButtonPushed=buttonPinRight;

                  }else if(buttonLeftState==HIGH){
                    lastButtonPushed=buttonPinLeft;

                  }else{
                    lastButtonPushed=0;
                  }                  
}

void navigateMenus() {
  MenuItem currentMenu=menu.getCurrent();
  
  switch (lastButtonPushed){
    case buttonPinEnter:
      if(!(currentMenu.moveDown())){  //if the current menu has a child and has been pressed enter then menu navigate to item below
        menu.use();
      }else{  //otherwise, if menu has no child and has been pressed enter the current menu is used
        menu.moveDown();
       } 
      break;
    case buttonPinEsc:
      menu.toRoot();  //back to main
      break;
    case buttonPinRight:
      menu.moveRight();
      break;      
    case buttonPinLeft:
      menu.moveLeft();
      break;      
  }
  
  lastButtonPushed=0; //reset the lastButtonPushed variable
}




sodjan
EF Sponsor
Inlägg: 43247
Blev medlem: 10 maj 2005, 16:29:20
Ort: Söderköping

Re: Hälp att programera en meny med arduino och LCD

Inlägg av sodjan »

Behöver du inte lägga (AddRight) på menu1Item5 och 6 också?
Karlsson25
Inlägg: 22
Blev medlem: 10 september 2012, 12:50:50

Re: Hälp att programera en meny med arduino och LCD

Inlägg av Karlsson25 »

Det löste alla mina problem!

Tackar så mycket!
Användarvisningsbild
TomasL
EF Sponsor
Inlägg: 46929
Blev medlem: 23 september 2006, 23:54:55
Ort: Borås
Kontakt:

Re: Hälp att programera en meny med arduino och LCD

Inlägg av TomasL »

Har en tidig beta till en menygenerator publicerad här.
http://elektronikforumet.com/forum/view ... Menu+maker

Vi får se om vi kan fixa till en lite senare version, framöver.
sodjan
EF Sponsor
Inlägg: 43247
Blev medlem: 10 maj 2005, 16:29:20
Ort: Söderköping

Re: Hälp att programera en meny med arduino och LCD

Inlägg av sodjan »

> Det löste alla mina problem!

Hade du bara helt enkelt missat det?
Eller varför var det inte fixat?

Jag har aldrig sett koden innan och jag vet inget (eller mycket lite) om
Arduino, men det var ju vädigt uppenbart att det var en tänkbar orsak.
Det ser man ju även om man inte har en aning om vad koden är till för.
Jag är alltså lite förvånad över att felet var så "litet", så att säga. :-)
mattswe
Inlägg: 159
Blev medlem: 20 augusti 2012, 13:27:51

Re: Hälp att programera en meny med arduino och LCD

Inlägg av mattswe »

Jag tolkade det som att han inte är någon programmerare, men vad vet jag?
Karlsson25
Inlägg: 22
Blev medlem: 10 september 2012, 12:50:50

Re: Hälp att programera en meny med arduino och LCD

Inlägg av Karlsson25 »

Nej jag är ingen programmerare, utan jag jobbar med ett projektarbete i skolan där jag tyvärr inte har tid att lära mig grunderna.
Och jag hade bara missat den uppenbart enkla saken. Lite pinsamt..

Men tack för hjälpen
Skriv svar