LCD menu options (best route)

Is it safe to assume that you pushed button 1, and then pushed button 2?

Yes and although you were correct in that, It is definitely not what I am after. I tried moving it outside of the case, but think it is still expecting a "high" on button one to make it function when ledstate == led2

I made some changes to the variable names as I mentioned.

You aren't debouncing the buttons, or toggling behavior only when the button state changes.

Your behavior is based on the button state being HIGH.

This is fine if you want something to happen while a button is pressed, like a doorbell ringing. It's not fine, if you want to implement a state machine, as you are (trying to).

You need to keep track of the previous state of the buttons, and only act when the state changes:

int previousButtonOneState = LOW;
int previousButtonTwoState = LOW;

void loop()
{
   int buttonOneState = digitalRead(buttonOnePin);
   int buttonTwoState = digitalRead(buttonTwoPin);

   if(buttonOneState == HIGH && previousButtonOneState == LOW)
   {
       // Button One was just pushed. Make the state changes...
   }
   previousButtonOneState = buttonOneState;

   if(buttonTwoState == HIGH && previousButtonTwoState == LOW)
   {
       // Button Two was just pushed. Now, we can rely on the state
   }
   previousButtonTwoState = buttonTwoState;
}

Thanks for that. I think maybe I am biting off more than I can chew with my limited knowledge. I added the code you gave me before the switch statements, but I obviously didn’t do it correctly.

Here is what I did. I deleted some of the code that didn’t pertain to what we are talking about.

int previousButtonOneState;
int previousButtonTwoState;
int buttonOneState;
int buttonTwoState;


//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
void setup() {



  pinMode(RED_PIN, OUTPUT);      // Set the pin as output
  pinMode(BLUE_PIN, OUTPUT);    // Set the pin as output
  pinMode(switchPin, INPUT);    // Set the switch pin as input
  pinMode(switchPin2, INPUT);    // Set the switch pin as input

  digitalWrite(BLUE_PIN,LOW);   //turn blue led off
  digitalWrite(RED_PIN,LOW);     //turn red led off

  Serial.begin(9600);           // Set up serial communication at 9600bps

  previousButtonOneState = LOW;
  previousButtonTwoState = LOW;

}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


void loop(){
  {
    if(buttonOneState == HIGH && previousButtonOneState == LOW){
      // Button One was just pushed. Make the state changes...
    }
    previousButtonOneState = buttonOneState;

    if(buttonTwoState == HIGH && previousButtonTwoState == LOW){
      // Button Two was just pushed. Now, we can rely on the state
    }
    previousButtonTwoState = buttonTwoState;
  }

    switch(ledstate){
    case LED1:
      lcd.clear();
      lcd.print("Snare");
      digitalWrite(BLUE_PIN,HIGH); //turn blue led off
      digitalWrite(RED_PIN,HIGH); //turn red led off
      delay(100); //wait 100ms
      switch(colorState){
      case COLOR1:
        lcd.clear();
        lcd.print("Color 1");
        colorState = COLOR2; //transition to color2 state
        break; //end of color1 case
      case COLOR2:
        lcd.clear();
        lcd.print("Color 2");
        colorState = COLOR3; //transition to color3 state
        break; //end of color2 case
      case COLOR3:
        lcd.clear();
        lcd.print("Color 3");
        colorState = COLOR1; //transition back to color1 state
        break; //end of color3 case
      }
      ledstate = LED2; //transition to led2 state    
      break; //end of START case               

    case LED2:
      lcd.clear();
      lcd.print("TOM 1");
      digitalWrite(BLUE_PIN,LOW); //turn blue led off
      digitalWrite(RED_PIN,HIGH); //turn red led on
      delay(200); //wait 200ms
      ledstate = LED3; //transition to led3 state
      break; //end of RED case

    case LED3:
      lcd.clear();
      lcd.print("TOM 2");
      digitalWrite(RED_PIN,LOW); //turn red led off
      digitalWrite(BLUE_PIN,HIGH); //turn blue led on
      delay(200); //wait 200ms
      ledstate = LED1; //transition to led1 state
      break; //end of BLUE case
    }
  }
}

At least, you're trying. Boy, are you trying. ;)

The whole switch statement, where you change the state goes inside the {} where the comment says

// Button One was just pushed. Make the state changes...

Man, I sure appreciate you hanging in there with me. I figure the only way for me to learn it since I live in a small town (no resources available) is to jump in and try to code something. :-?

Ok, does the nested code go in the second {} or does it remain in line like this? I'm still missing something. :-/

void loop(){


   if(buttonOneState == HIGH && previousButtonOneState == LOW)
   {
    switch(ledstate){
    case LED1:
      lcd.clear();
      lcd.print("Snare");
      digitalWrite(BLUE_PIN,HIGH); //turn blue led off
      digitalWrite(RED_PIN,HIGH); //turn red led off
      delay(100); //wait 100ms
      switch(colorState){
      case COLOR1:
        lcd.clear();
        lcd.print("Color 1");
        colorState = COLOR2; //transition to color2 state
        break; //end of color1 case
      case COLOR2:
        lcd.clear();
        lcd.print("Color 2");
        colorState = COLOR3; //transition to color3 state
        break; //end of color2 case
      case COLOR3:
        lcd.clear();
        lcd.print("Color 3");
        colorState = COLOR1; //transition back to color1 state
        break; //end of color3 case
      }
      ledstate = LED2; //transition to led2 state    
      break; //end of START case               

    case LED2:
      lcd.clear();
      lcd.print("TOM 1");
      digitalWrite(BLUE_PIN,LOW); //turn blue led off
      digitalWrite(RED_PIN,HIGH); //turn red led on
      delay(200); //wait 200ms
      ledstate = LED3; //transition to led3 state
      break; //end of RED case

    case LED3:
      lcd.clear();
      lcd.print("TOM 2");
      digitalWrite(RED_PIN,LOW); //turn red led off
      digitalWrite(BLUE_PIN,HIGH); //turn blue led on
      delay(200); //wait 200ms
      ledstate = LED1; //transition to led1 state
      break; //end of BLUE case
    }


   previousButtonOneState = buttonOneState;

   if(buttonTwoState == HIGH && previousButtonTwoState == LOW)
   {
       // Button Two was just pushed. Now, we can rely on the state
   }
   previousButtonTwoState = buttonTwoState;
}

I think I got it. I am sure I will be back, but that will get me started for now. Thanks to everyone that lent me a helping hand!!!!! 8-)

Man I am so close! But yet so far away. Here is what I have. By changing the {} around I can get it to enter the second Switch/Case, but I never have any control with the second button. It cycles through the color cases every time I get back around to the LED1 case.

void loop(){

  buttonOneState = digitalRead(switchPin);
  buttonTwoState = digitalRead(switchPin2);

  if(buttonOneState == HIGH && previousButtonOneState == LOW){
    switch(ledstate){
    case LED1:
      lcd.clear();
      lcd.print("Snare");
      if(buttonTwoState == HIGH && previousButtonTwoState == LOW){        
        switch(colorState){
        case COLOR1:
          lcd.clear();
          lcd.print("Color 1");
          colorState = COLOR2; //transition to color2 state
          break; //end of color1 case
        case COLOR2:
          lcd.clear();
          lcd.print("Color 2");
          colorState = COLOR3; //transition to color3 state
          break; //end of color2 case
        case COLOR3:
          lcd.clear();
          lcd.print("Color 3");
          colorState = COLOR1; //transition back to color1 state
          break; //end of color3 case
          previousButtonTwoState = buttonTwoState;
        }  
      }
      digitalWrite(BLUE_PIN,HIGH); //turn blue led off
      digitalWrite(RED_PIN,HIGH); //turn red led off
      delay(100); //wait 100ms
      ledstate = LED2; //transition to led2 state    
      break; //end of led1 case

    case LED2:
      lcd.clear();
      lcd.print("TOM 1");
      digitalWrite(BLUE_PIN,LOW); //turn blue led off
      digitalWrite(RED_PIN,HIGH); //turn red led on
      delay(200); //wait 200ms
      ledstate = LED3; //transition to led3 state
      break; //end of led2 case

    case LED3:
      lcd.clear();
      lcd.print("TOM 2");
      digitalWrite(RED_PIN,LOW); //turn red led off
      digitalWrite(BLUE_PIN,HIGH); //turn blue led on
      delay(200); //wait 200ms
      ledstate = LED1; //transition to led1 state
      break; //end of led3 case

    }
  } 
  previousButtonOneState = buttonOneState;
  previousButtonTwoState = buttonTwoState;
}

On any given pass through loop, which probably happens far oftener than you think, only one button is pressed. Only one button can have changed state.

Therefore, you need to treat the buttons, and their states, as completely independent.

In the loop function, see if button 1 is being presses, but was not before. If that is the case, set ledState.

Then, see if button 2 is being pressed, but was not before. If that is the case, set colorState.

The value of colorState has nothing to do with whether button 1 is being pressed, or not. It depends on (or changes as a result of) ledState, not whether button 1 is being pressed.

OK, this time I got it. I moved the entire colorstate switch out of the first loop and then added “ledstate == LED2” to the first line as was mentioned before. Thanks for the help again PaulS! :sunglasses:

Here is the code as it works for me.

// include the library code:
#include <LiquidCrystal.h>
#include <Button.h>

// initialize the 4 bit LCD library with the numbers of the interface pins
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

//first define states with obvious names and different values
#define LED1 0
#define LED2 1
#define LED3 2
#define COLOR1 3
#define COLOR2 4
#define COLOR3 5

//next define which pin is which
#define RED_PIN 9
#define BLUE_PIN 10


int buttonState = 0;
int buttonState2 = 0;
int switchPin = 11;              // switch is connected to pin 11
int switchPin2 = 12;            // switch is connected to pin 12

int ledstate = LED1; //create state variable and initialize it to START state
int colorState = COLOR1; //create state variable and initialize it to START state

int previousButtonOneState;
int previousButtonTwoState;
int buttonOneState;
int buttonTwoState;


//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
void setup() {

  //LCD SETUP

  // set up the LCD's number of rows and columns: 
  lcd.begin(16, 2);

  // Print a message to the LCD.

  lcd.print("**MIDI NIGHTS**");
  delay (1000);
  lcd.setCursor(0, 4);
  lcd.print("LED Menu Options");
  delay (1000);
  lcd.clear();

  // resets cursor position 
  lcd.setCursor(0, 4); 
  lcd.print("    Use right/left buttons to select LED    ");
  delay (20);

  // scroll 27 positions (string length) to the left 
  // to move it offscreen left:
  for (int positionCounter = 0; positionCounter < 27; positionCounter++) {
    // scroll one position left:
    lcd.scrollDisplayLeft(); 
    // wait a bit:
    delay(50);
  }
  {
    delay (300);

    lcd.clear();    //clear LCD and reset cursor
    lcd.setCursor(1, 9);
    lcd.print("Select LED < >");  //Select desired LED from menu
    delay(2000);
  }

  pinMode(RED_PIN, OUTPUT);      // Set the pin as output
  pinMode(BLUE_PIN, OUTPUT);    // Set the pin as output
  pinMode(switchPin, INPUT);    // Set the switch pin as input
  pinMode(switchPin2, INPUT);    // Set the switch pin as input

  digitalWrite(BLUE_PIN,LOW);   //turn blue led off
  digitalWrite(RED_PIN,LOW);     //turn red led off

  Serial.begin(9600);           // Set up serial communication at 9600bps

  previousButtonOneState = LOW;
  previousButtonTwoState = LOW;
  buttonOneState = LOW;
  buttonTwoState = LOW;

}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


void loop(){

  buttonOneState = digitalRead(switchPin);
  buttonTwoState = digitalRead(switchPin2);

  if(buttonOneState == HIGH && previousButtonOneState == LOW){
    switch(ledstate){
    case LED1:
      lcd.clear();
      lcd.print("Snare");
      digitalWrite(BLUE_PIN,HIGH); //turn blue led off
      digitalWrite(RED_PIN,HIGH); //turn red led off
      delay(100); //wait 100ms
      ledstate = LED2; //transition to led2 state    
      break; //end of led1 case

    case LED2:
      lcd.clear();
      lcd.print("TOM 1");
      digitalWrite(BLUE_PIN,LOW); //turn blue led off
      digitalWrite(RED_PIN,HIGH); //turn red led on
      delay(200); //wait 200ms
      ledstate = LED3; //transition to led3 state
      break; //end of led2 case

    case LED3:
      lcd.clear();
      lcd.print("TOM 2");
      digitalWrite(RED_PIN,LOW); //turn red led off
      digitalWrite(BLUE_PIN,HIGH); //turn blue led on
      delay(200); //wait 200ms
      ledstate = LED1; //transition to led1 state
      break; //end of led3 case

    }
  } 
        if(buttonTwoState == HIGH && previousButtonTwoState == LOW && ledstate == LED2){       
          
          switch(colorState){
        case COLOR1:
          lcd.clear();
          lcd.print("Color 1");
          colorState = COLOR2; //transition to color2 state
          break; //end of color1 case
        case COLOR2:
          lcd.clear();
          lcd.print("Color 2");
          colorState = COLOR3; //transition to color3 state
          break; //end of color2 case
        case COLOR3:
          lcd.clear();
          lcd.print("Color 3");
          colorState = COLOR1; //transition back to color1 state
          break; //end of color3 case
          previousButtonTwoState = buttonTwoState;
        }  
      }

  previousButtonOneState = buttonOneState;
  previousButtonTwoState = buttonTwoState;
}

Next questions for anyone NOT sick of me yet! ::) ;D

I managed to get two menu tiers working with two separate buttons. I tried to make a third tier menu and going back to the first button again. Here is the code I used. I redefined switchpin as buttonStateThree hoping that that would separate it from buttonStateOne, and added this code. I am seeing "Time" cases, but once again they refuse to behave properly.

Secondly, with the direction I have gone is there any way to have another button that would go "back" through the menu options of all of these tiers?

  //Time duration Choosing
if(buttonThreeState == HIGH && previousButtonThreeState == LOW && ledState == LED2 && colorState == COLOR1){   //sets button one to the TIME2 loop    
    
    switch(durationState){
    case TIME1:
      lcd.clear();
      lcd.print("Time 1");
      durationState = TIME2; //transition to color2 state
      break; //end of color1 case
    case TIME2:
      lcd.clear();
      lcd.print("Time 2");
      durationState = TIME3; //transition to color3 state
      break; //end of color2 case
    case TIME3:
      lcd.clear();
      lcd.print("Time 3");
      durationState = TIME4; //transition back to color1 state
      break; //end of color3 case
    case TIME4:
      lcd.clear();
      lcd.print("Time 4");
      durationState = TIME1; //transition back to color1 state
      break; //end of color3 case      
      previousButtonThreeState = buttonThreeState;
    }  
  }
  previousButtonOneState = buttonOneState;
  previousButtonTwoState = buttonTwoState;
  previousButtonThreeState = buttonThreeState;
}

My suggestion would be to separate "what the menu item does" from "what I did to get to the menu item" (i.e the navigation controls). A menu hierarchy can be simply written as a series of functions with infinite loops containing switch statements containing menu functions. Menu functions can be simple actions, or function calls to sub-menus.

Once you break out of the loop at any given level, the function returns, taking you to the menu above.

I completly understand if you want to do it yourself, but as per Grooves suggestion: http://www.arduino.cc/playground/Code/Menu this is a framework that I wrote to separate the navigation and the action.

Have a play with the example, and do not hesitate to ask any questions. :) (about either the library or other things:)

Good luck

Trust me I have been eyeing that for two days now, but I am so new to this programming stuff that I have no idea where to start in implementing it. I actually would rather use a tried-and-true method than stumble through it myself. I will see if I can make it work for me. I installed the menu this morning and gave it a look, but a lot of it confuses me.

I may not even understand what I am asking here so please bear with me. How do I know what a library is intended to do. AlphaBeta, I see where you included your menu libraries in the sample code you included, but how does the end user know what you coded into these libraries, and then be able to use them? :-?