Switch statements within another switch statements

Hi, I have already gone through;

But could not get the solution as the final solution in the above post was actually not using switch inside another switch statement as you said;

Here's a massively simplified version - no nested switch/case constructs, no Strings, massively reduced memory consumption, no memory corruption ..."

I have two variables; chapter, verse which will be selected by the user via encoder.
There are 33 grouped verses which are located in 13 chapter folders. I have to display on I2C LCD the chapter number and verse number when selected by the user at the time of playback.

For simplicity and for the sake of testing only and to see if this compiles and works, I have used to print on serial port.

I am almost new to arduino programming. Following is the code I have written using multiple switch statements inside another multiple switch statements;

uint8_t chapter = 1;                    
uint8_t verse   = 1;                     

void setup (){
  Serial.begin (9600);
}
  void loop(){
  Serial.println ("Pls enter Chapter Number");
  chapter = Serial.read ();
  delay(1000);

  Serial.println ("Pls enter Verse Number");
  verse = Serial.read ();
  delay(1000);
 print_chapter_verse_number(chapter, verse);
}

void print_chapter_verse_number (uint8_t chapter, uint8_t verse){

switch (chapter) {
  case 1:
    switch (verse){
      case 4:
        Serial.println("chapter 1, verse 4-6");
        break;      
      case 17:
        Serial.println("Chapter 1, Verse 17-18");
        break;      
      case 20:
        Serial.println("Chapter 1, Verse 20-21");
        break;      
      case 24:
        Serial.println("Chapter 1, Verse 24-25");
        break;      
      case 38:
        Serial.println("Chapter 1, Verse 38-39");
        break;      
      }
      break;
  case 3:
    switch(verse){
      case 14:
        Serial.println("Chapter 3, Verse 14-15");
        break;    
      }
      break;
  case 4:
    switch(verse){
      case 29:
        Serial.println("Chapter 4, Verse 29-30");
        break;    
      }
      break;
  case 5:
    switch(verse){
      case 8:
        Serial.println("Chapter 5, Verse 8-9");
        break;    
      case 27:
        Serial.println("Chapter 5, Verse 27-28");
        break;    
      }
      break;
  case 7:
    switch(verse){
      case 4:
        Serial.println("Chapter 7, Verse 4-5");
        break;    
      }
      break;
  case 8:
    switch(verse){
      case 12:
        Serial.println("Chapter 8, Verse 12-13");
        break;    
      }
      break;
  case 10:
    switch(verse){
      case 4:
        Serial.println("Chapter 10, Verse 4-5");
        break;    
      case 12:
        Serial.println("Chapter 10, Verse 12-13");
        break;    
      }
      break;
  case 11:
    switch(verse){
      case 10:
        Serial.println("Chapter 11, Verse 10-11");
        break;    
      case 15:
        Serial.println("Chapter 11, Verse 15-16");
        break;    
      case 26:
        Serial.println("Chapter 11, Verse 26-27");
        break;    
      case 39:
        Serial.println("Chapter 11, Verse 39-40");
        break;    
      case 41:
        Serial.println("Chapter 11, Verse 41-42");
        break;    
      }
      break;
  case 12:
    switch(verse){
      case 3:
        Serial.println("Chapter 12, Verse 3-4");
        break;    
      case 6:
        Serial.println("Chapter 12, Verse 6-7");
        break;    
      case 13:
        Serial.println("Chapter 12, Verse 13-14");
        break;    
      case 17:
        Serial.println("Chapter 12, Verse 17-18");
        break;    
      }
      break;
  case 13:
    switch(verse){
      case 3:
        Serial.println("Chapter 13, Verse 3-4");
        break;    
      case 6:
        Serial.println("Chapter 13, Verse 6-7");
        break;    
      }
      break;
  case 16:
    switch(verse){
      case 15:
        Serial.println("Chapter 16, Verse 15-16");
        break;    
      }
      break;
  case 17:
    switch(verse){
      case 5:
        Serial.println("Chapter 17, Verse 5-6");
        break;    
      }
      break;
  case 18:
    switch(verse){
      case 19:
        Serial.println("Chapter 18, Verse 19-20");
        break;    
      case 36:
        Serial.println("Chapter 18, Verse 36-37");
        break;    
      case 51:
        Serial.println("Chapter 18, Verse 51-53");
        break;    
      case 64:
        Serial.println("Chapter 18, Verse 64-65");
        break;    
      case 72:
        Serial.println("Chapter 18, Verse 72-73");
        break;    
      case 74:
        Serial.println("Chapter 18, Verse 74-75");
        break;    
      case 76:
        Serial.println("Chapter 18, Verse 76-77");
        break;    

        default:
        Serial.println("Chapter " chapter , "Verse " verse);
        return 0;
      }      
    }

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Do you have a question?
How are you entering the required ASCII control characters?

Serial.read() does not wait for a character to arrive. If nothing is available, it returns -1 but the variable you assign to is of type uint8_t aka byte so that -1 turns into 255.

You should use the proper type of variable for input and you should check to see if anything is available before reading. I'm guessing you want your program to stay there until the user does enter something.

Hi, Thanks a lot for addressing my question. At the moment for testing, I am getting the chapter number (two digit numeric) and verse number (three digit numeric) from terminal entered by user.

The code is not getting compiled. Is it workable ?

read this: Serial Input Basics - updated

Hi, thanks a lot for addressing my question. So should I then take serial input like this;

uint8_t chapter = Serial.read ( );

See my earlier comment about control characters

Your code is almost fine.

Serial.print can't do formatted printing; your default is in an odd position.

is mostly your code, auto-formatted with a few trifiling errors fixed also.

uint8_t chapter = 1;
uint8_t verse   = 1;

void setup () {
  Serial.begin (9600);
}

void loop() {

  for (chapter = 1; chapter < 99; chapter++)
    for (verse = 99; verse >= 1; verse--)

      print_chapter_verse_number(chapter, verse);

  Serial.println("\nTHAT'S ALL FOLKS!\n");
  while (1);    // hang unitl power down
}

void print_chapter_verse_number (uint8_t chapter, uint8_t verse) {

  switch (chapter) {
    case 1:
      switch (verse) {
        case 4:
          Serial.println("chapter 1, verse 4-6");
          break;
        case 17:
          Serial.println("Chapter 1, Verse 17-18");
          break;
        case 20:
          Serial.println("Chapter 1, Verse 20-21");
          break;
        case 24:
          Serial.println("Chapter 1, Verse 24-25");
          break;
        case 38:
          Serial.println("Chapter 1, Verse 38-39");
          break;
      }
      break;
    case 3:
      switch (verse) {
        case 14:
          Serial.println("Chapter 3, Verse 14-15");
          break;
      }
      break;
    case 4:
      switch (verse) {
        case 29:
          Serial.println("Chapter 4, Verse 29-30");
          break;
      }
      break;
    case 5:
      switch (verse) {
        case 8:
          Serial.println("Chapter 5, Verse 8-9");
          break;
        case 27:
          Serial.println("Chapter 5, Verse 27-28");
          break;
      }
      break;
    case 7:
      switch (verse) {
        case 4:
          Serial.println("Chapter 7, Verse 4-5");
          break;
      }
      break;
    case 8:
      switch (verse) {
        case 12:
          Serial.println("Chapter 8, Verse 12-13");
          break;
      }
      break;
    case 10:
      switch (verse) {
        case 4:
          Serial.println("Chapter 10, Verse 4-5");
          break;
        case 12:
          Serial.println("Chapter 10, Verse 12-13");
          break;
      }
      break;
    case 11:
      switch (verse) {
        case 10:
          Serial.println("Chapter 11, Verse 10-11");
          break;
        case 15:
          Serial.println("Chapter 11, Verse 15-16");
          break;
        case 26:
          Serial.println("Chapter 11, Verse 26-27");
          break;
        case 39:
          Serial.println("Chapter 11, Verse 39-40");
          break;
        case 41:
          Serial.println("Chapter 11, Verse 41-42");
          break;
      }
      break;
    case 12:
      switch (verse) {
        case 3:
          Serial.println("Chapter 12, Verse 3-4");
          break;
        case 6:
          Serial.println("Chapter 12, Verse 6-7");
          break;
        case 13:
          Serial.println("Chapter 12, Verse 13-14");
          break;
        case 17:
          Serial.println("Chapter 12, Verse 17-18");
          break;
      }
      break;
    case 13:
      switch (verse) {
        case 3:
          Serial.println("Chapter 13, Verse 3-4");
          break;
        case 6:
          Serial.println("Chapter 13, Verse 6-7");
          break;
      }
      break;
    case 16:
      switch (verse) {
        case 15:
          Serial.println("Chapter 16, Verse 15-16");
          break;
      }
      break;
    case 17:
      switch (verse) {
        case 5:
          Serial.println("Chapter 17, Verse 5-6");
          break;
      }
      break;
    case 18:
      switch (verse) {
        case 19:
          Serial.println("Chapter 18, Verse 19-20");
          break;
        case 36:
          Serial.println("Chapter 18, Verse 36-37");
          break;
        case 51:
          Serial.println("Chapter 18, Verse 51-53");
          break;
        case 64:
          Serial.println("Chapter 18, Verse 64-65");
          break;
        case 72:
          Serial.println("Chapter 18, Verse 72-73");
          break;
        case 74:
          Serial.println("Chapter 18, Verse 74-75");
          break;
        case 76:
          Serial.println("Chapter 18, Verse 76-77");
          break;

/* why? where should this go?
        default:
          Serial.print("Chapter ");
          Serial.print(chapter);
          Serial.print(" Verse ");
          Serial.print(verse);
          Serial.println("");
          return 0;
*/
      }
      break;
  }
}

HTH

a7

Thanks, I am just going through it. The rest of the part ok ?

If you auto format your code in the IDE (Ctrl-T) it will line up all the braces, etc. for you and you will see that you haven't got enough of them. You need to close out your print_chapter_verse_number() function.

Hi, thank you so much for addressing my question, is it getting compiled at your end now ?
What I am curious to know is the multiple switch statements inside another multiple switch statements I have written is ok ?

I mean does such kind of code gets compiled in arduino ?

Hi, thanks, again. I am almost a newbie, will try what you said.

Yes.

Fearlessly click the link in my post and see your code running in a browser window on a complete simulation of the Arduino development environemt.

a7

Thank you so much !

Yes. A statement is a statement is a statement.

A for loop can be inside another for loop, or a switch/case or... wherever.

A switch statment can be inside an if statement, whatever.

And on and on.

a7

Wow ! I am just analyzing if the desired chapter and grouped verse is displayed on the terminal.

Would come back again.

uint8_t verse   = 1;
  verse = Serial.read ();

Serial.read() reads a single character. How is the user able to enter 22, for instance ?

As mentioned in #4.

I never use Serial.read, so I just looped to feed the function some values to work with.

I only mention it because it is part of the divide and rule (or conquer) strategy - separate the function elements and get each working.

Make it so you can solicit from the user and print some numbers.

Make a chpaterAndVerse function work called with constants or using a loop or nested loops for the parameters.

Both parts work? Now feed the solicited numbers to the chpaterAndVerse function and see if everything is OK.

Like I said, I skiped the Serial.read as I do not use it and wold have to figure out how to use it for user input.

a7

Hi, I am a newbie for arduino programming. Pls suggest