Menubackend with LCD Shield

Hello Everyone,
I am in the process of making a controller for a small greenhouse but am new to Arduino so I am just trying to get some basics down. I am looking to use the Menubackend library with an LCD Shield similar to this one http://arduino-info.wikispaces.com/LCD-Pushbuttons . The code I am using as a jumping off point had this under the read buttons routine

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;
                  }                  
}

To try and make it work with the shield I am using I replaced the original readbuttons with this :

void  readButtons(){  //read buttons status
buttonPin = analogRead(0);

                  if (buttonPin < 325){
                    lastButtonPushed=buttonPinEnter;

                  }else if(buttonPin < 150){
                    lastButtonPushed=buttonPinEsc;

                  }else if(buttonPin < 5){
                    lastButtonPushed=buttonPinRight;

                  }else if(buttonPin < 500){
                    lastButtonPushed=buttonPinLeft;

                  }else{
                    lastButtonPushed=0;
                  }                       
}

The problem I am having is that it seems like no matter what button is pressed on the shield it is only reading as a press of the enter button. When a button is pressed it immediately returns "You Used Item1SubItem1"

Like I said I am rather new to this so I am not really sure what I am doing wrong. My reason for taking so much of the original code out and replacing it with the one I included was that I was not sure how to translate the use of one pin for all buttons to the original code which uses four separate pins for the buttons. Any help would be appreciated. I have also included my entire code if that helps.

LCD_Menu_Test.ino (5.1 KB)

buttonPin = analogRead(0);

I've never heard of analogRead() returning a pin number.

The problem I am having is that it seems like no matter what button is pressed on the shield it is only reading as a press of the enter button.

As proof, you left out the code that calls the function and the serial output from that code. Well, good luck,

Thanks for the reply. I guess I am a little confused by what you mean with analogRead returning a pin number. My use of buttonPin = analogRead(0); is to read the value of the resistance on pin 0 and therefore decide which button has been pushed. I took that idea from the code given on the website I linked to. I guess I am not sure of what other way to sense what button has been pushed.

My use of ... is to read the value of the resistance on pin 0 and therefore decide which button has been pushed.

Your use of the analogRead function is to get a value. That value is NOT a pin number. Use of meaningful names is not just something I like to pick on. It is essential to writing code that YOU can understand later.

I guess I am not sure of what other way to sense what button has been pushed.

There isn't another way. But, store the value in a variable whose name reflects what that value means. buttonPin is NOT such a name.

That shield uses a single analog pin to sense which button has been pressed.

A series of resistors are arranged with each button in a discrete place to act like a multipoint voltage divider, so that a push of each button will be read by the analogRead of the pin as a different voltage.

This is a good way of using only a single analog pin to be used for a multitude of buttons in a case where only one of them will be pushed at any given time.

@OP:

In your code, you need to consider each button press as being a range. In your current code, any button that gives an analogRead value of < 325 will always give Enter as a result, as it may match multiple cases but the first one evaluated will always be true, so you'll always get a result saying enter was pressed.

You can change it to use the following logic:

if (buttonPin < 325 && buttonPin > 150){
                    lastButtonPushed=buttonPinEnter;

so that it will only evaluate as true for a single button press case.

I'm sorry if I haven't made that very clear, if it does not make sense, shout and I'll try to clarify.

@spnappels:
Thanks for the advice, what you said makes total sense. Unfortunately even with this code:

void  readButtons(){                              //read buttons status
  buttonPin=analogRead(0);

                  if (buttonPin < 470 && buttonPin > 301){
                    lastButtonPushed=buttonPinEnter;

                  }else if (buttonPin < 300  && buttonPin > 131){
                    lastButtonPushed=buttonPinEsc;
                    
                  }else if (buttonPin < 130  && buttonPin > 0){
                    lastButtonPushed=buttonPinRight;

                  }else if (buttonPin < 550 && buttonPin > 475){
                    lastButtonPushed=buttonPinLeft;

                  }
                  
                   else{
                    lastButtonPushed == 0;
                  }
                                    
}

I am still having the same issue with every button returning the same thing. If it is any help when I do an analog read of pin 0 I get the following results for resistance :

Esc Button = 132
Enter Button = 307
Left Button = 480
Right Button = 0
None = 1023
Select = 720

Not really sure what I am doing wrong, it could be many things as far as I know.

Not really sure what I am doing wrong, it could be many things as far as I know.

I'm going to try again. You are NOT reading anything related to a pin from the analog pin, using analogRead. You are NOT reading anything about buttons anywhere. Use a name that makes sense for what you ARE reading.

int switchValue = analogRead(0);
Serial.print("Value: ");
Serial.println(switchValue);

Notice that the variable is NOT global. There is no reason for it to be.

Notice to, that the value read is printed to the serial port, where the Serial Monitor will see it.

Now, reorder the statements that follow so that they make sense:

if(switchValue >= 301 && switchValue <= 470)
{
   lastButtonPushed = buttonPinEnter;
   Serial.println*("Enter switch pressed");
}
else if(switchValue >= 131 && switchValue <= 300)
{
   lastButtonPushed = buttonPinEsc;
   Serial.println("Esc switch pressed");
}
// The rest goes here...

Then, show us the serial output you get when you press one switch. If you get more than one case showing that it was executed, there is a flaw in the logic. If you get the wrong case executed, there is another problem. If you read something other than what you expect, there is a third problem.

At this point, we don't even know where the problem is, let alone what it is. Divide and conquer. Once we know where the problem is, we can help you solve it.