Nested switch case - Help for understanding the issue

Hello,

I'm currently facing an issue on an arduino based project. I'm using an atmega328pb on a custom board to fit my hardware requirements.

The project use a touch screen and i'm facing an issue in the software integration. Let me explain the problem.

I have 4 different pages to display on the Screen. Most of the display is common on each page so i have created a function PageMenu(int Page_Num) to make the display thing.

On this function i make a switch case on the Page_Num so it will be something like this :

void PageMenu(int Page_Num)
{


// Make the common display thing


switch (page)
  {
      case 1 : 
      // Do display thing for page 1
      break;

      case 2 : 
      // Do display thing for page 2
      break;


      case 3 : 
      // Do display thing for page 3
      break;

      case 4 : 
      // Do display thing for page 4
      break;
  }
}

This part seems to be ok. But now let implement the Tactil part.

On each part i have a tactile area button to exit the page to the next one. Let's name it Area_1.

I have Area_1, Area_2 ..... , Area_11, which are all the tactile area of my project. They are common to all pages but are not always used (For exemple on the main page only Area_0 leads to something).

I have created a Tactile function to return the Area Number if it is pressed, including the default one (Area_0). What i do so far looks like this :

void PageMenu(int Page_Num)

{
bool token_exit=False

// Make the common display thing


switch (page)
  {
      case 1 : 
      // Do display thing for page 1

      do
      {

          switch(Tactile_function())
          {
              case 1:
              // Area 1 has been touched (exit area)

              token_exit=True
              break;

              case 2: 
              // Area 2 has been touched

             // Do stuff if Area 2 is activated on this page
             break;

             case 3:
             // Area 3 has been touched
            // Do stuff if Area 3 is activated on this page
            break;

            case 0:
            // No Listened area has been touched
           break;
           }

      }while(token_exit==False)
      break;
      



      case 2 : 
      // Do display thing for page 2

      do
      {

          switch(Tactile_function())
          {
              case 1:
              // Area 1 has been touched (exit area)

              token_exit=True
              break;

              case 2: 
              // Area 2 has been touched

             // Do stuff if Area 2 is activated on this page
             break;

             case 3:
             // Area 3 has been touched
            // Do stuff if Area 3 is activated on this page
            break;

            case 0:
            // No Listened area has been touched
           break;
           }

      }while(token_exit==False)
      break;


      case 3 : 
      // Do display thing for page 3

      do
      {

          switch(Tactile_function())
          {
              case 1:
              // Area 1 has been touched (exit area)

              token_exit=True
              break;

              case 2: 
              // Area 2 has been touched

             // Do stuff if Area 2 is activated on this page
             break;

             case 3:
             // Area 3 has been touched
            // Do stuff if Area 3 is activated on this page
            break;

            case 0:
            // No Listened area has been touched
           break;
           }

      }while(token_exit==False)
      break;

      case 4 : 
      // Do display thing for page 4

      do
      {

          switch(Tactile_function())
          {
              case 1:
              // Area 1 has been touched (exit area)

              token_exit=True
              break;

              case 2: 
              // Area 2 has been touched

             // Do stuff if Area 2 is activated on this page
             break;

             case 3:
             // Area 3 has been touched
            // Do stuff if Area 3 is activated on this page
            break;

            case 0:
            // No Listened area has been touched
           break;
           }

      }while(token_exit==False)
      break;

}
}

This Example illustrated exactly how my program is working. But saddly i have an error message as maybe you have already guessed :

Duplicate case value

Action when a certain area is pressed depends on the page we are on. This means that i call in each case (//Do stuff if Area x is pressed on this page) a function defined like this :

void WhatToDo(int Page_Num, int Area_Pressed)
{
switch (Page_Num)
{

    case 1:
    switch (Area_Pressed)
    {
        case 1:
        // Do the thing when area 1 is pressed on Page 1
        break;

        case 2:
        // Do the thing when area 2 is pressed on Page 1
        break;

        case ...:
        // Do the thing when area ... is pressed on Page 1
        break;

        default:
        // Empty for all the area which are not important on this page
        break;
    }
    break;

    case 2:
    switch (Area_Pressed)
    {
        case 1:
        // Do the thing when area 1 is pressed on Page 2
        break;

        case 2:
        // Do the thing when area 2 is pressed on Page 2
        break;

        case ...:
        // Do the thing when area ... is pressed on Page 2
        break;

        default:
        // Empty for all the area which are not important on this page
        break;
    }
    break;

    case 3:
    switch (Area_Pressed)
    {
        case 1:
        // Do the thing when area 1 is pressed on Page 3
        break;

        case 2:
        // Do the thing when area 2 is pressed on Page 3
        break;

        case ...:
        // Do the thing when area ... is pressed on Page 3
        break;

        default:
        // Empty for all the area which are not important on this page
        break;
    }
    break;

    case 4:
    switch (Area_Pressed)
    {
        case 1:
        // Do the thing when area 1 is pressed on Page 4
        break;

        case 2:
        // Do the thing when area 2 is pressed on Page 4
        break;

        case ...:
        // Do the thing when area ... is pressed on Page 4
        break;

        default:
        // Empty for all the area which are not important on this page
        break;
    }
    break;
}
      
}

I guess i can do a different way for this one. Here it is a 11 by 4 matrix, but i guess i can make it a 44 Vector line vector (so a switch with 44 Case if we imagine that all area are used on all pages).

I don't understand what is going on, and as i have auto learned C code in arduino projects, i don't understand what is going on (First use of such a multiple Switch thing).

So if someone can explain me what is going on and how we are supposed to do such a thing, i will appreciate.

Thank you.

I don't understand what is going on

I don't understand why you posted a snippet of your code.

I don't understand why you failed to post the COMPLETE error message(s).

              token_exit==True

I don't understand why you are using the equality operator here, instead of the assigment operator.

I don't understand why the f**k you left the ; of the end of the statement.

An awful lot of not understanding going on here...

There MUST be a better way to create a menu, without the need to replicate code so many times.

Hello,
First, thank you for you answer.

I don't understand why you posted a snippet of your code

My code is quite fat, and i just would like to understand how Nested switch can be done. Idea of the code i have written here is to illustrate what i'm saying. Indeed, i think it's my logic which is not good and not the Code itself. And i would like to understand why it's not good, and how i should do such a thing.

I don't understand why you failed to post the COMPLETE error message(s).

Only Duplicate Case value error but lot of times.

I don't understand why you are using the equality operator here, instead of the assigment operator.

I don't understand why the f**k you left the ; of the end of the statement.

The code is bad and for sur not "beautiful" as i have written it directly on the Forum. But first idea is only to illustrate.

There MUST be a better way to create a menu, without the need to replicate code so many times.

For sur, it's exactly why i'm asking help, i must have the wrong logic here.

Indeed, i think it's my logic which is not good and not the Code itself.

If you won't post your code, we'll never know.

And i would like to understand why it's not good

I'd like to know why people have a hard time understanding the message on my t-shirt, too.

The programme is a package using quite some library, and the code is composed by 5 cpp (+ the main.ino) files for a total of arround 3000 lines (And Variable are in French, and i didn't put any comments in the Code, as everything is clearly explained on my paper notebook).

I used to put my logic on paper, and then make the code according to this. And for sur i keep those paper as it is the "Skeleton" of my program. I Could almost make the program on the paper before writing it in Arduino IDE.

That's why i didn't post it here.

Nevertheless, if you want i can forward it to you with the right library as i understand that it might be a good point to have a view of the real code.

The Main fact is that i'm not even sure that it is normally possible to Put a Switch case in a Switch case.

So, create a simple sketch to test that.

byte a = 0;
byte b = 3;

void setup()
{
}

void loop()
{
   switch(a)
   {
      case 0:
        break;
      case 1:
        break;
      case 2:
        switch(b)
        {
           case 0:
             break;
           case 1:
             break;
        }
        break;
   }
}

Sketch uses 444 bytes (1%) of program storage space. Maximum is 32256 bytes.
Global variables use 9 bytes (0%) of dynamic memory, leaving 2039 bytes for local variables. Maximum is 2048 bytes.

You are missing a ; after the do while loop.
The valid syntax is:

do {
   ;
} while();

Ah ok so it is possible indeed. And it seems to even work if i do it like this :

byte a = 0;
byte b = 3;

void setup()
{
}

void loop()
{
   switch(a)
   {
      case 0:
        switch(b)
        {
           case 0:
             break;
           case 1:
             break;
        }
        break;
      case 1:
        switch(b)
        {
           case 0:
             break;
           case 1:
             break;
        }
        break;
      case 2:
        switch(b)
        {
           case 0:
             break;
           case 1:
             break;
        }
        break;
   }
}

Didn't think about making small "simulations" like this. Thank you.

You are missing a ; after the do while loop.
The valid syntax is:

do {

;
} while();

Thank you indeed, i forgot the ; but only in the Exemple i gave and not my main one

Thank you for your Help PaulS.

I have tested few different things, and finally i found what was going on.
Thank to your test idea, i figured that it was supposed to work, and, as i was not expected, it was a code failure.

In the second switch statement i wrote :
switch(buttonDetection();
{

// Blabla case
}

There were a ; after switch() and then case were in the main case and as they add the same number range it was causing the issue.

SO it’s solved, and it’s working.

SO it's solved, and it's working.

And, as a bonus, you learned something.