LCD menu options (best route)

Wow, that was brutal, but I finally got a switch case situation to work. I believe I can start actually coding something now! ::slight_smile:

Here is the very basic code:

 // include the library code:
 #include <LiquidCrystal.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 START 0
#define RED 1
#define BLUE 2
//next define which pin is which
#define RED_PIN 9
#define BLUE_PIN 10


int buttonState = 0; 
int switchPin = 11;              // switch is connected to pin 2
int val;                        // variable for reading the pin status
int val2;
int lightMode = 0;              // What mode is the light in?
int state = START; //create state variable and initialize it to START state

  
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
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 150 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 screen and reset cursor
  lcd.setCursor(1, 9);
  lcd.print("Select LED < >");  //Select desired LED
  delay(2000);
} 
pinMode(RED_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
digitalWrite(BLUE_PIN,LOW); //turn blue led off
digitalWrite(RED_PIN,LOW); //turn red led off
pinMode(switchPin, INPUT);    // Set the switch pin as input


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

}

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


void loop(){
buttonState = digitalRead(switchPin);
  
  if (buttonState == LOW){
    }
 else {
  //FSM time
switch(state){
case START:
    lcd.clear();
    lcd.print("Snare");
    digitalWrite(BLUE_PIN,LOW); //turn blue led off
    digitalWrite(RED_PIN,LOW); //turn red led off
    delay(100); //wait 100ms
    state = RED; //transition to red state
    break; //end of START case
  case RED:
    lcd.clear();
    lcd.print("TOM1");
    digitalWrite(BLUE_PIN,LOW); //turn blue led off
    digitalWrite(RED_PIN,HIGH); //turn red led on
    delay(200); //wait 200ms
    state = BLUE; //transition to blue state
    break; //end of RED case
  case BLUE:
    lcd.clear();
    lcd.print("TOM2");
    digitalWrite(RED_PIN,LOW); //turn red led off
    digitalWrite(BLUE_PIN,HIGH); //turn blue led on
    delay(200); //wait 200ms
    state = START; //transition to start state
    break; //end of BLUE case
  }
}
}

You have this:

  if (buttonState == LOW){
    }
 else {

Why not simply:

if(buttonState == HIGH){

Ha Ha, yep that works too. Thanks! :sunglasses:

I am now trying to nest a second switch/case into the first case “Start” and I a can’t get any reaction from it. I think I have some of the {} in the wrong places. Any thoughts?

#include <Button.h>

 // include the library code:
 #include <LiquidCrystal.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 START 0
#define RED 1
#define BLUE 2
#define START2 0
#define RED2 1
#define BLUE2 2
//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 state = START; //create state variable and initialize it to START state
int state2 = START2; //create state variable and initialize it to START state

  
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
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 150 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 screen and reset cursor
  lcd.setCursor(1, 9);
  lcd.print("Select LED < >");  //Select desired LED
  delay(2000);
} 
pinMode(RED_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
digitalWrite(BLUE_PIN,LOW); //turn blue led off
digitalWrite(RED_PIN,LOW); //turn red led off
pinMode(switchPin, INPUT);    // Set the switch pin as input
pinMode(switchPin2, INPUT);    // Set the switch pin as input

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

}

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


void loop(){
buttonState = digitalRead(switchPin);
buttonState2 = digitalRead(switchPin2);

  if (buttonState == HIGH){
  //FSM time
switch(state){
case START:
    lcd.clear();
    lcd.print("Snare");
    digitalWrite(BLUE_PIN,LOW); //turn blue led off
    digitalWrite(RED_PIN,LOW); //turn red led off
    delay(100); //wait 100ms
 
                    
          if (buttonState2 == HIGH) 
        switch(state2){
            case START2:
                lcd.clear();
                lcd.print("Color 1");
                state2 = RED2; //transition to red state
                break; //end of START case
            case RED2:
                lcd.clear();
                lcd.print("Color 2");
                state2 = BLUE2; //transition to blue state
                break; //end of RED case
            case BLUE2:
                lcd.clear();
                lcd.print("Color 3");
                state2 = START2; //transition to start state
                break; //end of BLUE case
        }
    state = RED; //transition to red state    
    break; //end of START case               
  

    
  case RED:
    lcd.clear();
    lcd.print("TOM1");
    digitalWrite(BLUE_PIN,LOW); //turn blue led off
    digitalWrite(RED_PIN,HIGH); //turn red led on
    delay(200); //wait 200ms
    state = BLUE; //transition to blue state
    break; //end of RED case
  case BLUE:
    lcd.clear();
    lcd.print("TOM2");
    digitalWrite(RED_PIN,LOW); //turn red led off
    digitalWrite(BLUE_PIN,HIGH); //turn blue led on
    delay(200); //wait 200ms
    state = START; //transition to start state
    break; //end of BLUE case
  }
}
}
if (buttonState2 == HIGH)
[glow]{
[/glow]        switch(state2){
            case START2:
                lcd.clear();
                lcd.print("Color 1");
                state2 = RED2; //transition to red state
                break; //end of START case
            case RED2:
                lcd.clear();
                lcd.print("Color 2");
                state2 = BLUE2; //transition to blue state
                break; //end of RED case
            case BLUE2:
                lcd.clear();
                lcd.print("Color 3");
                state2 = START2; //transition to start state
                break; //end of BLUE case
        }
[glow]}[/glow]

I tried that, but nothing changed. Any other ideas or possible errors in my code?

Thank you for looking at this for my PaulS!

The code, as you have it written, requires that you have both buttons pressed. Do you?

No, I wasn't. I guess I need to change "buttonState" back to low before I can use "buttonState2" by itself?

I tried that, but nothing changed

That's because they're not required.

Is there another way I can write “buttonState” at the beginning of the loop so that I can bring it back to LOW during the second switch/case?

What you could do is move the button 2 code after the end of the button 1 switch statement, and execute the code if state == RED.

Hope you guys aren't getting sick of me yet. I really appreciate your time and input. I am learning as I go and wearing my google button out! ::)

Here is how I tried to implement your idea PaulS. I still get no entry into the second switch/case. I changed up some of the variable names to make more sense with the project. I hope that doesn't throw too much confusion into the mix.

If I can get this last issue, then maybe I can leave you guys alone for awhile! :D

void loop(){
  buttonState = digitalRead(switchPin);
  buttonState2 = digitalRead(switchPin2);

  if (buttonState == HIGH){
    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 red 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 blue 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 start state
      break; //end of BLUE case
    }

    {
      if (ledstate == LED2)
        if (buttonState2 == HIGH)
          if (buttonState == LOW){
            switch(colorState){
            case COLOR1:
              lcd.clear();
              lcd.print("Color 1");
              colorState = COLOR2; //transition to red state
              break; //end of START case
            case COLOR2:
              lcd.clear();
              lcd.print("Color 2");
              colorState = COLOR3; //transition to blue state
              break; //end of RED case
            case COLOR3:
              lcd.clear();
              lcd.print("Color 3");
              colorState = COLOR1; //transition to start state
              break; //end of BLUE case
            }
          }

    }
  }
}

This:

      if (ledstate == LED2)
        if (buttonState2 == HIGH)
          if (buttonState == LOW){

is the same as writing:

      if  ( (ledstate == LED2) &&
            (buttonState2 == HIGH) && 
            (buttonState == LOW)) {

OK, but will that change anything to make it work?

No, but it may make it easier to read.

OK :)

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...