Go Down

Topic: Nested switch...case (Read 2 times) previous topic - next topic

uberdum50

I am trying to write a menu system to integrate with my home automation system but I have run across a problem with nested switch...case switches used to perform the menu routines. The menu is driven off one keypad and needs to be multi-level.

This is the menu code so far:
Code: [Select]

void loop() {
  char key = keypad.getKey();
  switch (key) {
    case '*':
      menuActive = true; //Allows access to the menu
      GLCD.ClearArea();
      GLCD.println("1. LED On");
      GLCD.println("2. LED Off");
      GLCD.println("3. LED Blink");
      GLCD.println("4. Master config");
      GLCD.println("5. MenuItem5");
      GLCD.println("6. MenuItem6");
      GLCD.println("7. MenuItem7");
      GLCD.println("# To exit menu");
    break;
   
    case '#': //Disables menu access
      menuActive = false;
      HomeScreen(); //Basic home screen presenting user with some basic options
    break;
   
    case '1':
      if (menuActive == true) {
        digitalWrite(ledPin, HIGH); //For testing only
        ActionCompleted();
      }
    break;
   
    case '2':
      if (menuActive == true) {
        digitalWrite(ledPin, LOW);
        ActionCompleted(); //Generic screen to show user action performed sucessfully
      }
    break;
   
    case '3':
      if (menuActive == true) {
        ActionCompleted();
        digitalWrite(ledPin, HIGH);
        delay(100);
        digitalWrite(ledPin, LOW);
        delay(100);
        digitalWrite(ledPin, HIGH);
        delay(100);
        digitalWrite(ledPin, LOW);
      }
    break;
   
    case '4':
      if (menuActive == true) {
        GLCD.ClearArea();
        GLCD.println("Master Configuration");
        GLCD.println("# To exit menu");
        GLCD.println("1. Device ID");
          case '1': //Error is given on this line because the number 1 is used twice...
            GLCD.ClearArea();
            GLCD.println("Device ID menu");
          break;
      }
    break;
  }
}


The error says:
VOSHA_UCP.ino: In function 'void loop()':
VOSHA_UCP:116: error: duplicate case value
VOSHA_UCP:66: error: previously used here

But I need that to work to provide me with a multi level menu. Is there any workarounds to this problem?

uber


uberdum50

Just tried throwing this in on the end:
Code: [Select]

case '4':
      if (menuActive == true) {
        GLCD.ClearArea();
        GLCD.println("Master Configuration");
        GLCD.println("# To exit menu");
        GLCD.println("1. Device ID");
          if(key == '1') { //This toggles the function to enable LED instead of entering menu
            GLCD.ClearArea();
            GLCD.println("Device ID menu");
          }
      }


And it just enabled the other function with the same key that it was assigned to... I'm thinking about toggling the main menu lock once I get into a lower menu...

uber

UKHeliBob

OK, I give up, where is the second switch hiding ?
Presumably it should be in your case '4' somewhere
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

uberdum50

I did try that earlier before I posted and it didn't seem to like nested switches, I'll try again now...

uber

Go Up