Problem in Simple Menu using "AnalogButtons.h" and "ShiftRegLCD.h"

Dear friends and honorable helping Hands,

Currently working on a simple Menu for my Solar Tracker . I am using DIY 12 Keys Keypad that is controlled with only one Analoge Pin of Arduino using "AnalogButtons.h" library. I have mapped the Keypad and all the keys giving me correct results when pressed. Here is the sketch:

AnalogButtons analogButtons(ANALOG_PIN, 30, &handleButtons);
Button b1 = Button(1, 47,50);   // Button "1" is pressed when the Analogue Value is between 47 and 50.  Normally I am getting consistent 48.
Button b2 = Button(2, 84, 87);
Button b3 = Button(3, 127, 130);
Button b4 = Button(4, 194, 198);
Button b5 = Button(5, 303,309);
Button b6 = Button(6, 405, 410);
Button b7 = Button(7, 546, 549);
Button b8 = Button(8, 690, 695);
Button b9 = Button(9, 780,785);
Button b10 = Button(10, 864, 870);
Button b11 = Button(11, 925, 935);
Button b12= Button(12, 955, 965);

Initializing the values in the Setup:

void setup() 
{
  Serial.begin(9600); 
  //Serial.println("Testing your Analog buttons");
  //INITIALIZING THE BUTTONS
  analogButtons.addButton(b1); 
  analogButtons.addButton(b2);
  analogButtons.addButton(b3);
  analogButtons.addButton(b4);
  analogButtons.addButton(b5);
  analogButtons.addButton(b6);
  analogButtons.addButton(b7);
  analogButtons.addButton(b8);
  analogButtons.addButton(b9);
  analogButtons.addButton(b10);
  analogButtons.addButton(b11);
  analogButtons.addButton(b12);

The following function used to assigned the button values:

 void handleButtons(int id, boolean held)
{  
  if (!held) {    ///If the Button is not in contineously pressed or button is pressed and released
     if (id==1)(ButtonPressed=1);
     if (id==2)(ButtonPressed=2);
     if (id==3)(ButtonPressed=3);
     if (id==4)(ButtonPressed=4);
     if (id==5)(ButtonPressed=5);
     if (id==6)(ButtonPressed=6);
     if (id==7)(ButtonPressed=7);
     if (id==8)(ButtonPressed=8);
     if (id==9)(ButtonPressed=9);
     if (id==10)(ButtonPressed=11);
     if (id==11)(ButtonPressed=0);
     if (id==12)(ButtonPressed=12);

                  }
}

The following function is called inside the Void Loop()

 void loop() 
{ analogButtons.checkButtons(); // This fuction is required to track the Buttons. On the basis of this , "handleButtons" works.

}

I am also using 3-Wire LCD using Shiftregister. The library is <ShiftRegLCD.h>. I have checked a sample code by pressing any button on the keypad i am getting display of the pressed button on 16x2 LCD.

PROBLEM:

I have developed a Two tier menu that will be expanded more in future. But i am getting problem. Following is the Key map i am using to move within the menu system.

  /*
               2(^)
    (<)4       5(*)      6(>)
               8(v)
*/

the following code is compiled and uploaded into Arduino with following observations.
1- initially menuOption is set to 1. so The LCD display

1.SET LIGHTS   *
          2.VIEW PERAMS

2- When I press the Button 4. I go into idlemenu(). No problem here.
3- In the idlemenu() when i press button2 i go into menuOption=2.

1.SET LIGHTS   
          2.VIEW PERAMS  *

Its again All right

4- Here when i press 2, I am getting

1.SET LIGHTS     *
          2.VIEW PERAMS  *

You can see both '*' up and down. When i see the Serial Monitor I am getting alternate "I am Case 2" and "I am Case 1"

MY Question is Why? Due to Space restraint the complete sketch is being posted in Second Post.

Please help me. Following is my complete sketch:

/*---------------------------------------------------------
                 SHIFTREGsrlcd BASIC PARAMETERS:
-----------------------------------------------------------
//   Data on pin 12
//   Clock on pin 13
//   Enable on pin 8
// USAGE: ShiftRegsrlcd srlcdobjectvariablename(Datapin, Clockpin, Enablepin or TWO_WIRE [, Lines [, Font]]])
//   where Lines and Font are optional.
//     Enablepin: can be replaced by constant TWO_WIRE, if using only 2 wires.
//     Lines: 1 or 2 lines (or more if possible)
//     Font : 0 or 1, small or big font (8 or 10 pixel tall font, if available).
//srsrlcd.clear(); // Clear display, set cursor position to zero
//srsrlcd.home();  // Set cursor position to zero
//srsrlcd.setCursor(column, row); // Sets cursor position
//srsrlcd.noDisplay(); // Turn the display off
//srsrlcd.shiftLeft();  //  This is for text that flows Left to Right
//srsrlcd.shiftRight(); // This is for text that flows Right to Left
srsrlcd.print("HELLO, WORLD!");
              PROCEDURE FOR SPECIAL CHARCACTER CREATION
//In the Setup:
uint8_t heart[8] = {0x0,0xa,0x1f,0x1f,0xe,0x4,0x0}; //1st define the Shape
srsrlcd.createChar(3, heart);                          //Then create the character
              TO DISPLAY CUSTOM CHARACTER 
srsrlcd.write(3);
*/
/*---------------------------------------------------------
                 ANALOGBUTTON  BASIC PARAMETERS:
-----------------------------------------------------------
  When the AnalogButtons object is created, the final argument is a function to be called 
  when the checkButtons() function is called, and a button is pressed.In that function (handleButtons()), 
  you are told which button was pressed or held. In that function, you can do whatever you need to do, 
  if the appropriate button is pressed or held.

Code:
if(id == 3)
{
  // The button with id of 3 was pressed or held. Do whatever...
}
// Default hold duration is 1 second, lets make it 5 seconds for button5
//Button b5 = Button(5, 860, 875, 5);

FOR CONFIGURATION OF BUTTONS IN VOID LOOP
  // To configure the MAX/Min values for each 
  // Button, uncomment this line, make sure you've called Serial.begin(9600); 
  // Then in turn hold town each botton, noting the max/min values
  //AnalogButtons::configure(ANALOG_PIN); //delay(1000);
*/
#include "AnalogButtons.h"
#include <ShiftRegLCD.h>
ShiftRegLCD srlcd(12, 13, 8,2); //Initializing srlcd 16x2
#define ANALOG_PIN 0            //Keypad connected to Analoge Pin 0
int ButtonPressed;   /// This will till us which button is pressed to drive the Menu
int menuOption=1; //This is required for Switch case, Currently display the MainmenuOneOne

AnalogButtons analogButtons(ANALOG_PIN, 30, &handleButtons);
Button b1 = Button(1, 47,50);
Button b2 = Button(2, 84, 87);
Button b3 = Button(3, 127, 130);
Button b4 = Button(4, 194, 198);
Button b5 = Button(5, 303,309);
Button b6 = Button(6, 405, 410);
Button b7 = Button(7, 546, 549);
Button b8 = Button(8, 690, 695);
Button b9 = Button(9, 780,785);
Button b10 = Button(10, 864, 870);
Button b11 = Button(11, 925, 935);
Button b12= Button(12, 955, 965);


void setup() 
{
  Serial.begin(9600); 
  //Serial.println("Testing your Analog buttons");
  //INITIALIZING THE BUTTONS
  analogButtons.addButton(b1); 
  analogButtons.addButton(b2);
  analogButtons.addButton(b3);
  analogButtons.addButton(b4);
  analogButtons.addButton(b5);
  analogButtons.addButton(b6);
  analogButtons.addButton(b7);
  analogButtons.addButton(b8);
  analogButtons.addButton(b9);
  analogButtons.addButton(b10);
  analogButtons.addButton(b11);
  analogButtons.addButton(b12);
  
  ///INITIAL SCREEN ON THE LCD
  srlcd.print(" SUN TRACKER ");
  srlcd.setCursor(0,1);
  srlcd.print("by Khalid V1.00");
  delay(500);
  srlcd.clear();
 }
 void loop() 
{  
  // To check values when button are pressed
analogButtons.checkButtons(); // This fuction is required to track the Buttons. On the basis of this , "handleButtons" works.
switch(menuOption){//loads each menu depending on what value "menuOption" has at the start of the loop.
         //srlcd.clear();
    case 0:  idleMenu();break;
    case 1:  mainMenuOneOne();break;
    case 2:  mainMenuOneTwo();break;
    
    default: idleMenu();break;
  }  
}//end of void


//-------------------------------------------------
 void handleButtons(int id, boolean held)
{  
  if (!held) {    ///If the Button is not in contineously pressed or button is pressed and released
     if (id==1)(ButtonPressed=1);
     if (id==2)(ButtonPressed=2);
     if (id==3)(ButtonPressed=3);
     if (id==4)(ButtonPressed=4);
     if (id==5)(ButtonPressed=5);
     if (id==6)(ButtonPressed=6);
     if (id==7)(ButtonPressed=7);
     if (id==8)(ButtonPressed=8);
     if (id==9)(ButtonPressed=9);
     if (id==10)(ButtonPressed=11);
     if (id==11)(ButtonPressed=0);
     if (id==12)(ButtonPressed=12);

                  }
}
void idleMenu(){//case 0

  srlcd.setCursor(0,0);
  srlcd.print("Main Menu");//top line
  srlcd.setCursor(0,1);
  srlcd.print("Select option");
 
  /*
               2(^)
    (<)4       5(*)      6(>)
               8(v)
*/ 
if (ButtonPressed==2) {menuOption=2;} // go to mainMenuOneTwo()
}
//-----------------------------------------------------
void mainMenuOneOne(){ //case 1

  srlcd.setCursor(0,0);
  srlcd.print("1.SET LIGHTS   *");
  srlcd.setCursor(0,1);
  srlcd.print("2.VIEW PERAMS    ");
  srlcd.setCursor(0,0);
  Serial.println("I am Case 1");
        
  /*
               2(^)
    (<)4       5(*)      6(>)
               8(v)
*/

  /*if V pressed load menuOptionOneTwo();
    if ^ pressed do nothing
    if < pressed load idleMenu()/ menuOption = 0;
    if > pressed load setLightsOne();/ menuOption = 8;
    if * pressed load setLightsOne();/ menuOption = 9;
  */
if (ButtonPressed==8){ menuOption=2;}
//if (ButtonPressed==2) {menuOption=2;}
if (ButtonPressed==4) {menuOption=0;}
if (ButtonPressed==6) {menuOption=1;}
//if (ButtonPressed==5) {menuOption=9;}
}
void mainMenuOneTwo(){ //case 2

  srlcd.setCursor(0,0);
  srlcd.print("1.SET LIGHTS    ");
  srlcd.setCursor(0,1);
  srlcd.print("2.VIEW PERAMS  *");
  srlcd.setCursor(0,0);
  Serial.println("I am Case 2");
        /*
               2(^)
    (<)4       5(*)      6(>)
               8(v)
*/
  /*if V pressed load menuOptionTwoOne()/menuOption = 3;
    if ^ pressed load menuOptionOneOne()/menuOption = 2;
    if < pressed load idleMenu()/menuOption = 0;
    if > pressed load viewPerams(); not coded yet
    if * pressed load viewPerams(); not coded yet
  */
//if (ButtonPressed==8) {menuOption=1;}
if (ButtonPressed==2) {menuOption=1;}
if (ButtonPressed==4) {menuOption=0;}
//if (ButtonPressed==6) {lcdclr=true;menuOption=8;return;}
//if (ButtonPressed==5) {lcdclr=true;menuOption=9;return;}
 }

Here is the refined Sketch without unnecessary comments.

#include "AnalogButtons.h"
#include <ShiftRegLCD.h>
ShiftRegLCD srlcd(12, 13, 8,2); //Initializing srlcd 16x2
#define ANALOG_PIN 0            //Keypad connected to Analoge Pin 0
int ButtonPressed;   /// This will till us which button is pressed to drive the Menu
int menuOption=1; //This is required for Switch case, Currently display the MainmenuOneOne

AnalogButtons analogButtons(ANALOG_PIN, 30, &handleButtons);
Button b1 = Button(1, 47,50);
Button b2 = Button(2, 84, 87);
Button b3 = Button(3, 127, 130);
Button b4 = Button(4, 194, 198);
Button b5 = Button(5, 303,309);
Button b6 = Button(6, 405, 410);
Button b7 = Button(7, 546, 549);
Button b8 = Button(8, 690, 695);
Button b9 = Button(9, 780,785);
Button b10 = Button(10, 864, 870);
Button b11 = Button(11, 925, 935);
Button b12= Button(12, 955, 965);


void setup() 
{
  Serial.begin(9600); 

  analogButtons.addButton(b1); 
  analogButtons.addButton(b2);
  analogButtons.addButton(b3);
  analogButtons.addButton(b4);
  analogButtons.addButton(b5);
  analogButtons.addButton(b6);
  analogButtons.addButton(b7);
  analogButtons.addButton(b8);
  analogButtons.addButton(b9);
  analogButtons.addButton(b10);
  analogButtons.addButton(b11);
  analogButtons.addButton(b12);
  
  srlcd.print(" SUN TRACKER ");
  srlcd.setCursor(0,1);
  srlcd.print("by Khalid V1.00");
  delay(500);
  srlcd.clear();
 }
 void loop() 
{  

analogButtons.checkButtons(); 
switch(menuOption){

    case 0:  idleMenu();break;
    case 1:  mainMenuOneOne();break;
    case 2:  mainMenuOneTwo();break;
    
    default: idleMenu();break;
  }  
}


//-------------------------------------------------
 void handleButtons(int id, boolean held)
{  
  if (!held) {    ///If the Button is not in contineously pressed or button is pressed and released
     if (id==1)(ButtonPressed=1);
     if (id==2)(ButtonPressed=2);
     if (id==3)(ButtonPressed=3);
     if (id==4)(ButtonPressed=4);
     if (id==5)(ButtonPressed=5);
     if (id==6)(ButtonPressed=6);
     if (id==7)(ButtonPressed=7);
     if (id==8)(ButtonPressed=8);
     if (id==9)(ButtonPressed=9);
     if (id==10)(ButtonPressed=11);
     if (id==11)(ButtonPressed=0);
     if (id==12)(ButtonPressed=12);

                  }
}
void idleMenu(){//case 0

  srlcd.setCursor(0,0);
  srlcd.print("Main Menu");//top line
  srlcd.setCursor(0,1);
  srlcd.print("Select option");
 
  /*
               2(^)
    (<)4       5(*)      6(>)
               8(v)
*/ 
if (ButtonPressed==2) {menuOption=2;} // go to mainMenuOneTwo()
}
//-----------------------------------------------------
void mainMenuOneOne(){ //case 1

  srlcd.setCursor(0,0);
  srlcd.print("1.SET LIGHTS   *");
  srlcd.setCursor(0,1);
  srlcd.print("2.VIEW PERAMS    ");
  srlcd.setCursor(0,0);
  Serial.println("I am Case 1");
        
  /*
               2(^)
    (<)4       5(*)      6(>)
               8(v)
*/


if (ButtonPressed==8){ menuOption=2;}
if (ButtonPressed==4) {menuOption=0;}
if (ButtonPressed==6) {menuOption=1;}

}
void mainMenuOneTwo(){ //case 2

  srlcd.setCursor(0,0);
  srlcd.print("1.SET LIGHTS    ");
  srlcd.setCursor(0,1);
  srlcd.print("2.VIEW PERAMS  *");
  srlcd.setCursor(0,0);
  Serial.println("I am Case 2");
        /*
               2(^)
    (<)4       5(*)      6(>)
               8(v)
*/

if (ButtonPressed==2) {menuOption=1;}
if (ButtonPressed==4) {menuOption=0;}

 }

hi i am from singapore, i have some problem about arduino library. I am doing a school project which already have some arduino codes... I have the #include <ShiftRegLCD.h> header... but so far until now i can't get the correct library for that header file.
So may i know how to overcome that problem and where can i get that ShiftRegLCD library..

Regards,
Kris

where can i get that ShiftRegLCD library

From the same place you got the header file.

If you don't have the header file, where did you hear of it?

Google seems to be able to find it. I wonder why you couldn't.
http://code.google.com/p/arduinoshiftreglcd/