LCD Menu with Switch/Case

hi all,

I try to make a simple menu on the LCD display, with switch / case menu should be in the form:

menu1 menu2 -- submenu21 -- submenu22 menu3

here is the piece of code that does not work I used button.h the problem is with the 2nd "if", there in no action when I press button3

 void state() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 0);
   // Print a message to the LCD.
  //lcd.print("Voltage:");// print the number of seconds since reset:
  //lcd.setCursor(0, 1);
  //val = analogRead(analogPin);
  //lcd.print(val/204.8);
  //lcd.print(millis()/1000);
  //lcd.print(" V");
  //lcd.setCursor(7, 1);
  if(button1.uniquePress()){
    Serial.println("B1");
   button1Presses = ++button1Presses % NUMBER_OF_STATES1;
     switch (button1Presses) {
    case 0: lcd.clear();
                lcd.setCursor(4, 0);
                lcd.print("SET TEMP");
                Serial.println("Case0");
                break;
        case 1: lcd.clear();
                lcd.setCursor(2, 0);
                lcd.print("SET SELECTOR");
                Serial.println("Case1");
                if(button3.uniquePress()){
                  Serial.println("B2");
                    button3Presses = ++button3Presses % NUMBER_OF_STATES2;
                   switch (button3Presses) {
                case 'sel1':
                          lcd.clear();
                          lcd.setCursor(0, 0);
                          lcd.print("SELECTOR1");
                          break;
                    case 'sel2':
                          lcd.clear();
                          lcd.setCursor(0, 0);
                          lcd.print("SELECTOR2");
                          break;
                   }
                }
        break;
        case 2: 
                 lcd.clear();        
                do {
                  button1State = digitalRead(button1Pin);
                  //lcd.clear();
                  lcd.setCursor(2, 0);
                  lcd.print("TEMP: ");
                  lcd.setCursor(0, 1); 
                  val = analogRead(analogPin);
                  lcd.print(val/204.8);
                  delay(10);
                } while (button1State == LOW);
                Serial.println("Case2");
        break;
      }
    }

What is wrong in my code?

Not a complete code. What is button.h? Rest of your code? Plenty of things don't make sense if you don't include all you code.

FYI, I have this complete menu and interaction code.

http://liudr.wordpress.com/2011/03/17/phi-menu-is-about-to-be-released/

ok,

This is the complete code:

// http://www.arduino.cc/en/Tutorial/LiquidCrystal
// include the library code:
#include <LiquidCrystal440.h> //libraria 4x40 disp 1x16 ca 2x8
#include <Button.h> // libraria button.h 

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(11, 6, 12, 7, 8, 9, 10);

//caractere

byte newChar1[8] = {
	B00000,
	B00000,
	B00000,
	B00000,
	B00000,
	B00000,
	B11111,
	B11111
};

byte newChar2[8] = {
	B00000,
	B00000,
	B00000,
	B00000,
	B00000,
	B11111,
	B11111,
	B11111
};
byte newChar3[8] = {
	B00000,
	B00000,
	B00000,
	B00000,
	B11111,
	B11111,
	B11111,
	B11111
};
byte newChar4[8] = {
	B00000,
	B00000,
	B00000,
	B11111,
	B11111,
	B11111,
	B11111,
	B11111
};
byte newChar5[8] = {
	B00000,
	B00000,
	B11111,
	B11111,
	B11111,
	B11111,
	B11111,
	B11111
};
byte newChar6[8] = {
	B00000,
	B11111,
	B11111,
	B11111,
	B11111,
	B11111,
	B11111,
	B11111
};
byte newChar7[8] = {
	B11111,
	B11111,
	B11111,
	B11111,
	B11111,
	B11111,
	B11111,
	B11111
};
int analogPin = 3; //pin pot
int ledPin = 13; // led pin

int button1Pin = 5;
int button1State = 0;
 
Button button1 = Button(5,PULLUP); //butonul0 la pin 2 dig
Button button2 = Button(2,PULLUP); //butonul1 la pinul 3 dig
Button button3 = Button(3,PULLUP); //butonul0 la pin 2 dig
Button button4 = Button(4,PULLUP); //butonul0 la pin 2 dig

byte button1Presses = 0; // variabila pt butonul1
byte button2Presses = 0; //variabila pt butonul2
byte button3Presses = 0; //variabila pt butonul3
byte button4Presses = 0; //variabila pt butonul4

const byte NUMBER_OF_STATES = 8; // numar stari butonul0
const byte NUMBER_OF_STATES1 = 3;
const byte NUMBER_OF_STATES2 = 2;

int val = 0;  
void setup() {
  Serial.begin(9600);
  // set up the LCD's number of columns and rows: 
  lcd.begin(8, 2);
  pinMode(ledPin, OUTPUT); 
  pinMode (button1Pin, INPUT);
  
  lcd.createChar(0, newChar1);
  lcd.createChar(1, newChar2);
  lcd.createChar(2, newChar3);
  lcd.createChar(3, newChar4);
  lcd.createChar(4, newChar5);
  lcd.createChar(5, newChar6);
  lcd.createChar(6, newChar7);
  //lcd.write(0);
}

void loop() {
   state();
}
    void state() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 0);
   // Print a message to the LCD.
  //lcd.print("Voltage:");// print the number of seconds since reset:
  //lcd.setCursor(0, 1);
  //val = analogRead(analogPin);
  //lcd.print(val/204.8);
  //lcd.print(millis()/1000);
  //lcd.print(" V");
  //lcd.setCursor(7, 1);
  if(button1.uniquePress()){
    Serial.println("B1");
   button1Presses = ++button1Presses % NUMBER_OF_STATES1;
     switch (button1Presses) {
	case 0: lcd.clear();
                lcd.setCursor(4, 0);
                lcd.print("SET TEMP");
                Serial.println("Case0");
                break;
        case 1: lcd.clear();
                lcd.setCursor(2, 0);
                lcd.print("SET SELECTOR");
                Serial.println("Case1");
                if(button3.uniquePress()){
                  Serial.println("B2");
                    button3Presses = ++button3Presses % NUMBER_OF_STATES2;
                   switch (button3Presses) {
	            case 'sel1':
                          lcd.clear();
                          lcd.setCursor(0, 0);
                          lcd.print("SELECTOR1");
                          break;
                    case 'sel2':
                          lcd.clear();
                          lcd.setCursor(0, 0);
                          lcd.print("SELECTOR2");
                          break;
                   }
                }
        break;
        case 2: 
                 lcd.clear();        
                do {
                  button1State = digitalRead(button1Pin);
                  //lcd.clear();
                  lcd.setCursor(2, 0);
                  lcd.print("TEMP: ");
                  lcd.setCursor(0, 1); 
                  val = analogRead(analogPin);
                  lcd.print(val/204.8);
                  delay(10);
                } while (button1State == LOW);
                Serial.println("Case2");
        break;
      }
    }
  if(button4.uniquePress()){
    lcd.setCursor(0, 0);
   button4Presses = ++button4Presses % NUMBER_OF_STATES;
     switch (button4Presses) {
	case 0: lcd.write(0);
        digitalWrite(ledPin ,LOW);
        break;
        case 1: lcd.write(1);
        digitalWrite(ledPin ,HIGH);
        break;
        case 2: lcd.write(2);
        digitalWrite(ledPin ,LOW);
        break;
        case 3: lcd.write(3);
        digitalWrite(ledPin ,HIGH);
        break;
        case 4: lcd.write(4);
        digitalWrite(ledPin ,LOW);
        break;
        case 5: lcd.write(5);
        digitalWrite(ledPin ,HIGH);
        break;
        case 6: lcd.write(6);
        digitalWrite(ledPin ,LOW);
        break;
  }
  }
  delay(30);
}

This doesn't look good:

switch (button3Presses) {
                case 'sel1':
                          lcd.clear();
                          lcd.setCursor(0, 0);
                          lcd.print("SELECTOR1");
                          break;
                    case 'sel2':
                          lcd.clear();
                          lcd.setCursor(0, 0);
                          lcd.print("SELECTOR2");
                          break;
                   }

Compare it to your other case statements where you've used integer constants.

I think I’m blind, I can not see what is wrong…

In the code fragment above, button3Presses will be either 0 or 1, those are what your case statements should be testing, not 'se1' and 'sel2'

ok.
I tried it, does not work

When switch (first one) is in case 1: it must wait for this piece of code:

if(button3.uniquePress())
                {
                  Serial.println("B2");  //debug
                    button3Presses = ++button3Presses % NUMBER_OF_STATES2;
                   switch (button3Presses) 
                   {
	            case 0:
                          lcd.clear();
                          lcd.setCursor(0, 0);
                          lcd.print("SELECTOR1");
                          break;
                    case 1:
                          lcd.clear();
                          lcd.setCursor(0, 0);
                          lcd.print("SELECTOR2");
                          break;
                   }
                }

if button3 is pressed I must have a “B2” on serial, and SELECTOR1 on LCD.
they do not happen…
code again

// http://www.arduino.cc/en/Tutorial/LiquidCrystal
// include the library code:
#include <LiquidCrystal440.h> //libraria 4x40 disp 1x16 ca 2x8
#include <Button.h> // libraria button.h 

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(11, 6, 12, 7, 8, 9, 10);

//custom char

byte newChar1[8] = {
	B00000,
	B00000,
	B00000,
	B00000,
	B00000,
	B00000,
	B11111,
	B11111
};

byte newChar2[8] = {
	B00000,
	B00000,
	B00000,
	B00000,
	B00000,
	B11111,
	B11111,
	B11111
};
byte newChar3[8] = {
	B00000,
	B00000,
	B00000,
	B00000,
	B11111,
	B11111,
	B11111,
	B11111
};
byte newChar4[8] = {
	B00000,
	B00000,
	B00000,
	B11111,
	B11111,
	B11111,
	B11111,
	B11111
};
byte newChar5[8] = {
	B00000,
	B00000,
	B11111,
	B11111,
	B11111,
	B11111,
	B11111,
	B11111
};
byte newChar6[8] = {
	B00000,
	B11111,
	B11111,
	B11111,
	B11111,
	B11111,
	B11111,
	B11111
};
byte newChar7[8] = {
	B11111,
	B11111,
	B11111,
	B11111,
	B11111,
	B11111,
	B11111,
	B11111
};
int analogPin = 3; //pin pot
int ledPin = 13; // led pin

int button1Pin = 5;
int button1State = 0;
 
Button button1 = Button(5,PULLUP); //butonul0 la pin 2 dig
Button button2 = Button(2,PULLUP); //butonul1 la pinul 3 dig
Button button3 = Button(3,PULLUP); //butonul0 la pin 2 dig
Button button4 = Button(4,PULLUP); //butonul0 la pin 2 dig

byte button1Presses = 0; // variabila pt butonul1
byte button2Presses = 0; //variabila pt butonul2
byte button3Presses = 0; //variabila pt butonul3
byte button4Presses = 0; //variabila pt butonul4

const byte NUMBER_OF_STATES = 8; // numar stari butonul0
const byte NUMBER_OF_STATES1 = 3;
const byte NUMBER_OF_STATES2 = 2;

int val = 0;  
void setup() {
  Serial.begin(9600);
  // set up the LCD's number of columns and rows: 
  lcd.begin(8, 2);
  pinMode(ledPin, OUTPUT); 
  pinMode (button1Pin, INPUT);
  
  lcd.createChar(0, newChar1);
  lcd.createChar(1, newChar2);
  lcd.createChar(2, newChar3);
  lcd.createChar(3, newChar4);
  lcd.createChar(4, newChar5);
  lcd.createChar(5, newChar6);
  lcd.createChar(6, newChar7);
  //lcd.write(0);
}

void loop() {
  lcd.setCursor(0, 0);
  if(button1.uniquePress()){
    Serial.println("B1"); //debug
   button1Presses = ++button1Presses % NUMBER_OF_STATES1;
     switch (button1Presses) 
     {
	case 0: lcd.clear();
                lcd.setCursor(4, 0);
                lcd.print("SET TEMP");
                Serial.println("Case0"); //debug
                break;
        case 1: lcd.clear();
                lcd.setCursor(2, 0);
                lcd.print("SET SELECTOR");
                Serial.println("Case1");  //debug
                if(button3.uniquePress())
                {
                  Serial.println("B2");  //debug
                    button3Presses = ++button3Presses % NUMBER_OF_STATES2;
                   switch (button3Presses) 
                   {
	            case 0:
                          lcd.clear();
                          lcd.setCursor(0, 0);
                          lcd.print("SELECTOR1");
                          break;
                    case 1:
                          lcd.clear();
                          lcd.setCursor(0, 0);
                          lcd.print("SELECTOR2");
                          break;
                   }
                }
        break;
        case 2: 
                 lcd.clear();        
                do {
                  button1State = digitalRead(button1Pin);
                  //lcd.clear();
                  lcd.setCursor(2, 0);
                  lcd.print("TEMP: ");
                  lcd.setCursor(0, 1); 
                  val = analogRead(analogPin);
                  lcd.print(val/204.8);
                  delay(10);
                   } while (button1State == LOW);
                Serial.println("Case2"); //debug
        break;
      }
    }
  if(button4.uniquePress()){
    lcd.setCursor(0, 0);
   button4Presses = ++button4Presses % NUMBER_OF_STATES;
     switch (button4Presses) {
	case 0: lcd.write(0);
        digitalWrite(ledPin ,LOW);
        break;
        case 1: lcd.write(1);
        digitalWrite(ledPin ,HIGH);
        break;
        case 2: lcd.write(2);
        digitalWrite(ledPin ,LOW);
        break;
        case 3: lcd.write(3);
        digitalWrite(ledPin ,HIGH);
        break;
        case 4: lcd.write(4);
        digitalWrite(ledPin ,LOW);
        break;
        case 5: lcd.write(5);
        digitalWrite(ledPin ,HIGH);
        break;
        case 6: lcd.write(6);
        digitalWrite(ledPin ,LOW);
        break;
  }
}
  delay(30);
}

I found a solution, maybe not the best but it works,
problem was that switch / case is no loop.

// http://www.arduino.cc/en/Tutorial/LiquidCrystal
// include the library code:
#include <LiquidCrystal440.h> //libraria 4x40 disp 1x16 ca 2x8
#include <Button.h> // libraria button.h 

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(11, 6, 12, 7, 8, 9, 10);

//custom char

byte newChar1[8] = {
	B00000,
	B00000,
	B00000,
	B00000,
	B00000,
	B00000,
	B11111,
	B11111
};

byte newChar2[8] = {
	B00000,
	B00000,
	B00000,
	B00000,
	B00000,
	B11111,
	B11111,
	B11111
};
byte newChar3[8] = {
	B00000,
	B00000,
	B00000,
	B00000,
	B11111,
	B11111,
	B11111,
	B11111
};
byte newChar4[8] = {
	B00000,
	B00000,
	B00000,
	B11111,
	B11111,
	B11111,
	B11111,
	B11111
};
byte newChar5[8] = {
	B00000,
	B00000,
	B11111,
	B11111,
	B11111,
	B11111,
	B11111,
	B11111
};
byte newChar6[8] = {
	B00000,
	B11111,
	B11111,
	B11111,
	B11111,
	B11111,
	B11111,
	B11111
};
byte newChar7[8] = {
	B11111,
	B11111,
	B11111,
	B11111,
	B11111,
	B11111,
	B11111,
	B11111
};
int analogPin = 3; //pin pot
int ledPin = 13; // led pin

int button1Pin = 5;
int button1State = 0;

int incomingByte = 0; //debug
 
Button button1 = Button(5,PULLUP); //butonul0 la pin 2 dig
Button button2 = Button(2,PULLUP); //butonul1 la pinul 3 dig
Button button3 = Button(3,PULLUP); //butonul0 la pin 2 dig
Button button4 = Button(4,PULLUP); //butonul0 la pin 2 dig

byte button1Presses = 0; // variabila pt butonul1
byte button2Presses = 0; //variabila pt butonul2
byte button3Presses = 0; //variabila pt butonul3
byte button4Presses = 0; //variabila pt butonul4

const byte NUMBER_OF_STATES = 8; // numar stari butonul0
const byte NUMBER_OF_STATES1 = 3;
const byte NUMBER_OF_STATES2 = 2;

int val = 0;

void setup() {
  Serial.begin(9600);
  // set up the LCD's number of columns and rows: 
  lcd.begin(8, 2);
  pinMode(ledPin, OUTPUT); 
  pinMode (button1Pin, INPUT);
  
  lcd.createChar(0, newChar1);
  lcd.createChar(1, newChar2);
  lcd.createChar(2, newChar3);
  lcd.createChar(3, newChar4);
  lcd.createChar(4, newChar5);
  lcd.createChar(5, newChar6);
  lcd.createChar(6, newChar7);
  //lcd.write(0);
}

void loop() {
  lcd.setCursor(0, 0);
  if(button1.uniquePress()){
    Serial.println("B1"); //debug
     button1Presses = ++button1Presses % NUMBER_OF_STATES1;
     switch (button1Presses) 
     {
	case 0: lcd.clear();
                lcd.setCursor(4, 0);
                lcd.print("SET TEMP");
                
                Serial.println("Case0"); //debug
                break;
        case 1: //lcd.clear();
                lcd.setCursor(2, 0);
                lcd.print("SET SELECTOR");
                Serial.println("Case1");  //debug
                do {
                   button1State = digitalRead(button1Pin);
                   val = analogRead(analogPin);
                 if(button3.uniquePress())
                {
                  Serial.println("B2");  //debug
                    button3Presses = ++button3Presses % NUMBER_OF_STATES2;
                   switch (button3Presses) 
                   {
	            case 0:
                          lcd.clear();
                          lcd.setCursor(0, 0);
                          lcd.print("SELECTOR1");
                          break;
                    case 1:
                          lcd.clear();
                          lcd.setCursor(0, 0);
                          lcd.print("SELECTOR2");
                          break;
                   }
                }
                } while (button1State == LOW);
        break;
        case 2: 
                 lcd.clear();        
                do {
                  button1State = digitalRead(button1Pin);
                  //lcd.clear();
                  lcd.setCursor(2, 0);
                  lcd.print("TEMP: ");
                  lcd.setCursor(0, 1); 
                  val = analogRead(analogPin);
                  lcd.print(val/204.8);
                  delay(10);
                   } while (button1State == LOW);
                Serial.println("Case2"); //debug
        break;
      }
    }
  if(button4.uniquePress()){
    lcd.setCursor(0, 0);
   button4Presses = ++button4Presses % NUMBER_OF_STATES;
     switch (button4Presses) {
	case 0: lcd.write(0);
        digitalWrite(ledPin ,LOW);
        break;
        case 1: lcd.write(1);
        digitalWrite(ledPin ,HIGH);
        break;
        case 2: lcd.write(2);
        digitalWrite(ledPin ,LOW);
        break;
        case 3: lcd.write(3);
        digitalWrite(ledPin ,HIGH);
        break;
        case 4: lcd.write(4);
        digitalWrite(ledPin ,LOW);
        break;
        case 5: lcd.write(5);
        digitalWrite(ledPin ,HIGH);
        break;
        case 6: lcd.write(6);
        digitalWrite(ledPin ,LOW);
        break;
  }
}
  delay(30);
}