switch case array to avoid duplicate coding?

Hey everyone I have a switch state with something like 22 cases, code in all 22 cases is almost the same with just two differences. I was wondering if anyone can help me use an array so that I can save my self from having too much delicate coding

Here is part of the code in each case. Only thing that's different between all 22 cases is the: display.println TEXT, and EEPROM read value. How to put those in arrays and still have the switch (state) function?

  switch (state) { 
    
    case 1:
      display.clearDisplay();
      display.setTextColor(WHITE);
      display.setCursor(0,0);
      display.setTextSize(1);
      display.println(" SET: 1000-2000");
      display.println(EEPROM.read(sav12));
      display.display();
      break;

    case 2:
      display.clearDisplay();
      display.setTextColor(WHITE);
      display.setCursor(0,0);
      display.setTextSize(1);
      display.println(" SET: 2000-3000");
      display.println(EEPROM.read(sav23));
      display.display();
      break;

Put the common code into a function and then call the function from within the CASE - something like

void commonDisplayStuff() {
      display.clearDisplay();
      display.setTextColor(WHITE);
      display.setCursor(0,0);
      display.setTextSize(1);
      display.println(" SET: 2000-3000");
}

and then

   case 1:
      commonDisplayStuff();
      display.println(EEPROM.read(sav12));
      display.display();
      break;

I leave it to you to think of a more sensible name for the function :)

And you could take things a step further and create the function to take a parameter and contain all of the code

void displayAll( byte savX) {
      display.clearDisplay();
      display.setTextColor(WHITE);
      display.setCursor(0,0);
      display.setTextSize(1);
      display.println(" SET: 2000-3000 RPM \n");
      display.println(EEPROM.read(savX));
      display.display();
}

and call it with

    case 1:
      displayAll(sav12);
      break;

By the way I have just guessed the type of the variable sav12 and I may be wrong

...R

Are all of the cases identical except for the text printed and the eeprom location read, and are those values of 'state' contiguous? If so, it should be possible to use a couple of arrays to store the text and the eeprom location to access and use 'state' as an index into the arrays.

david_2018: Are all of the cases identical except for the text printed and the eeprom location read, and are those values of 'state' contiguous? If so, it should be possible to use a couple of arrays to store the text and the eeprom location to access and use 'state' as an index into the arrays.

Yes everything is same just print text, and eeprom read location are different, and yes all contiguous 1 though 22, may even have more than 22 later this is why I was hoping to do it with an array just not sure how to go about it

myele: I was hoping to do it with an array just not sure how to go about it

Then look (again ?) at the last example in Reply #1 and replace the parameter in the call with an array element.

...R

or just move the common code before and after the switch() block...