How do i select an option in my menu then perform the task selected?

Hi

I am trying to create a menu for a 16x2 LCD keypad shield that will allow me to scroll through the options, then when the select button is pressed, it will complete the task described.

for example, if I select "flash led once" it will flash the led then stop and let me carry on moving between the various options.

the problem I am having is that when I move to the option it automatically starts the task rather than waiting for the select button to be pressed.

#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);         


int lcd_key     = 0;
int adc_key_in  = 0;
int calc_total;
int display_options;

#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3
#define btnSELECT 4
#define btnNONE   5

int read_LCD_buttons(){              
    adc_key_in = analogRead(0);     

  

    if (adc_key_in > 1000) return btnNONE; 


    if (adc_key_in < 50)   return btnRIGHT;  
    if (adc_key_in < 150)  return btnUP; 
    if (adc_key_in < 300)  return btnDOWN; 
    if (adc_key_in < 450)  return btnLEFT; 
    if (adc_key_in < 700)  return btnSELECT;  

  

    return btnNONE;      
    
}

void setup(){
   lcd.begin(16, 2);              
   lcd.setCursor(0,0);    
   lcd.print (calc_total);  
    pinMode(53,OUTPUT);
       
}
 
void loop(){
   
   lcd_key = read_LCD_buttons();  
   

   switch (lcd_key){              
    
       case btnDOWN:{ 
            lcd.clear();
            calc_total = calc_total - 1;
            calc_total = constrain(calc_total,-5, 5);
            delay(300); 
            break;
       }
       case btnUP:{
            lcd.clear();
            calc_total = calc_total + 1;
            calc_total = constrain(calc_total,-5, 5);
            delay(300);
            break;
       }
     }



    if (calc_total == 0){
      lcd.setCursor(0,0);
      lcd.print("Menu Home");
    }
    if (calc_total == 1){
      lcd.setCursor(0,0); 
      lcd.print("Flash X 1");
          if (btnSELECT){
            lcd.setCursor(0,1);
            lcd.print("Performing");
            digitalWrite(53,HIGH);
            delay(250);
            digitalWrite(53,LOW);
          }
    }
    if (calc_total == 2){
      lcd.setCursor(0,0);  
      lcd.print("Flash X 2 ");
          if (btnSELECT){
            lcd.setCursor(0,1);
            lcd.print("Performing");
            digitalWrite(53,HIGH);
            delay(250);
            digitalWrite(53,LOW);
            digitalWrite(53,HIGH);
            delay(250);
            digitalWrite(53,LOW);
          }
    }
    if (calc_total == 3){
      lcd.setCursor(0,0); 
      lcd.print("Flash X 3");
          if (btnSELECT){
            lcd.setCursor(0,1);
            lcd.print("Performing");
            digitalWrite(53,HIGH);
            delay(250);
            digitalWrite(53,LOW);
            digitalWrite(53,HIGH);
            delay(250);
            digitalWrite(53,LOW);
            digitalWrite(53,HIGH);
            delay(250);
            digitalWrite(53,LOW);
          }
    }
    if (calc_total == 4){
      lcd.setCursor(0,0); 
      lcd.print("Flash X 4");
          if (btnSELECT){
            lcd.setCursor(0,1);
            lcd.print("Performing");
            digitalWrite(53,HIGH);
            delay(250);
            digitalWrite(53,LOW);
            digitalWrite(53,HIGH);
            delay(250);
            digitalWrite(53,LOW);
            digitalWrite(53,HIGH);
            delay(250);
            digitalWrite(53,LOW);
            digitalWrite(53,HIGH);
            delay(250);
            digitalWrite(53,LOW);
          }
    }
    if (calc_total == 5){
      lcd.setCursor(0,0); 
      lcd.print("Flash X 5");
          if (btnSELECT){
            lcd.setCursor(0,1);
            lcd.print("Performing");
            digitalWrite(53,HIGH);
            delay(250);
            digitalWrite(53,LOW);
            digitalWrite(53,HIGH);
            delay(250);
            digitalWrite(53,LOW);
            digitalWrite(53,HIGH);
            delay(250);
            digitalWrite(53,LOW);
            digitalWrite(53,HIGH);
            delay(250);
            digitalWrite(53,LOW);
            digitalWrite(53,HIGH);
            delay(250);
            digitalWrite(53,LOW);
          }
    }
    if (calc_total == -1){
      lcd.setCursor(0,0); 
      lcd.print("Fade X 1");
    }
    if (calc_total == -2){
      lcd.setCursor(0,0); 
      lcd.print("Fade X 2");
    }
    if (calc_total == -3){
      lcd.setCursor(0,0); 
      lcd.print("Fade X 3");
    }
    if (calc_total == -4){
      lcd.setCursor(0,0); 
      lcd.print("Fade X 4");
    }
    if (calc_total == -5){
      lcd.setCursor(0,0); 
      lcd.print("Fade X 5");
    }
    
    
}

Any help will be greatly appreciated.

Liam

This line:

 if (btnSELECT)

always evaluates to true, since btnSELECT is not zero. You need to move the select button code into the switch statement with the button down and button up code.

But surely that will trigger the same effect everytime that btnSELECT is pressed, rather than triggering the option that is displayed on the LCD?

You could try wrapping all the code that acts on calc_total in a function and calling it when the select button is clicked.

You should think about how to simplify the code, for example you could have a flash function that takes the number of flashes as a parameter instead of repeating the code for each value of calc_total.

void loop() {

  lcd_key = read_LCD_buttons();


  switch (lcd_key) {

    case btnDOWN: {
        lcd.clear();
        calc_total = calc_total - 1;
        calc_total = constrain(calc_total, -5, 5);
        delay(300);
        break;
      }
    case btnUP: {
        lcd.clear();
        calc_total = calc_total + 1;
        calc_total = constrain(calc_total, -5, 5);
        delay(300);
        break;
      }
    case btnSELECT : {
        selectPressed();
      }
  }



}

void selectPressed()
{

  if (calc_total == 0) {
    lcd.setCursor(0, 0);
    lcd.print("Menu Home");
  }
  if (calc_total == 1) {
    lcd.setCursor(0, 0);
    lcd.print("Flash X 1");

    lcd.setCursor(0, 1);
    lcd.print("Performing");
    digitalWrite(53, HIGH);
    delay(250);
    digitalWrite(53, LOW);
  }

  if (calc_total == 2) {
    lcd.setCursor(0, 0);
    lcd.print("Flash X 2 ");

    lcd.setCursor(0, 1);
    lcd.print("Performing");
    digitalWrite(53, HIGH);
    delay(250);
    digitalWrite(53, LOW);
    digitalWrite(53, HIGH);
    delay(250);
    digitalWrite(53, LOW);

  }
  if (calc_total == 3) {
    lcd.setCursor(0, 0);
    lcd.print("Flash X 3");

    lcd.setCursor(0, 1);
    lcd.print("Performing");
    digitalWrite(53, HIGH);
    delay(250);
    digitalWrite(53, LOW);
    digitalWrite(53, HIGH);
    delay(250);
    digitalWrite(53, LOW);
    digitalWrite(53, HIGH);
    delay(250);
    digitalWrite(53, LOW);

  }
  if (calc_total == 4) {
    lcd.setCursor(0, 0);
    lcd.print("Flash X 4");

    lcd.setCursor(0, 1);
    lcd.print("Performing");
    digitalWrite(53, HIGH);
    delay(250);
    digitalWrite(53, LOW);
    digitalWrite(53, HIGH);
    delay(250);
    digitalWrite(53, LOW);
    digitalWrite(53, HIGH);
    delay(250);
    digitalWrite(53, LOW);
    digitalWrite(53, HIGH);
    delay(250);
    digitalWrite(53, LOW);

  }
  if (calc_total == 5) {
    lcd.setCursor(0, 0);
    lcd.print("Flash X 5");

    lcd.setCursor(0, 1);
    lcd.print("Performing");
    digitalWrite(53, HIGH);
    delay(250);
    digitalWrite(53, LOW);
    digitalWrite(53, HIGH);
    delay(250);
    digitalWrite(53, LOW);
    digitalWrite(53, HIGH);
    delay(250);
    digitalWrite(53, LOW);
    digitalWrite(53, HIGH);
    delay(250);
    digitalWrite(53, LOW);
    digitalWrite(53, HIGH);
    delay(250);
    digitalWrite(53, LOW);

  }
  if (calc_total == -1) {
    lcd.setCursor(0, 0);
    lcd.print("Fade X 1");
  }
  if (calc_total == -2) {
    lcd.setCursor(0, 0);
    lcd.print("Fade X 2");
  }
  if (calc_total == -3) {
    lcd.setCursor(0, 0);
    lcd.print("Fade X 3");
  }
  if (calc_total == -4) {
    lcd.setCursor(0, 0);
    lcd.print("Fade X 4");
  }
  if (calc_total == -5) {
    lcd.setCursor(0, 0);
    lcd.print("Fade X 5");
  }



}

But surely that will trigger the same effect everytime that btnSELECT is pressed, rather than triggering the option that is displayed on the LCD?

I'm not sure what you are trying to say here, but if(btnSelect) is NOT comparing the button that the user pressed to btnSelect, to determine if the pressed button IS the select button.

You need to have a case btnSelect: that will deal with the select button.

You were right adding the extra case for btnSELECT worked, the whole thing works now, thanks for your help.

LCD_Menu_Test_09.1.ino (8.67 KB)