Using variables to easily change String or Int Array values?

Hi there. I'm currently coding a button box for Sim Racing which uses a 12-way rotary switch to determine some of the various button values and messages that are output.

So for example, if I'm on position 1, I might want the messages output by certain buttons to be different than if I was on position 2 or 3 etc...

My thought was to use an array with string variables and another with int variables and use those string variables to change the message stored in the array, and the int variables to change the keyboard value depending on switch positions.

The message updates seem to not be happening and I'm trying to figure out why its not working. Been about 15 years since I did any coding so I'm definitely rusty. Looking for help. Thanks!

void setup() {
  //gameVal and blkBoxVal store the values of the 12 way selectors
  
  //Initialize the 12 way black box dial messages
  String blkBoxValMsg1 = "";
  String blkBoxValMsg2 = "";
  String blkBoxValMsg3 = "";
  String blkBoxValMsg4 = "";

  //Initialize the array with the variables defined above for the 12 way black box dial
  String blkBoxValMsg[] = { "", blkBoxValMsg1, blkBoxValMsg2, blkBoxValMsg3, blkBoxValMsg4 };
  
  //Initialize the BlackBox Twelve Way Keys which may or may not be used to output a keyboard character to the game
  int blkBoxChar1 = 0;
  int blkBoxChar2 = 0;
  int blkBoxChar3 = 0;
  int blkBoxChar4 = 0;

  //Initialize the array with the blackbox keys which may change their values
  const int twelveWayBlkBoxKeys[] { 0, blkBoxChar1, blkBoxChar2, blkBoxChar3, blkBoxChar4 };
}

void loop() 
{
  //CODE LEFT OUT:  FIRST CHECK TO SEE IF THE gameVal or blkBoxVal have changed, if so then execute below code
  
  if (gameVal == 1)  //change messages and set characters to nothing
  {
    blkBoxValMsg1 = "ADJ  BRAKE  BIAS";     blkBoxValMsg2 = "FRONT  SWAY  BAR";
    blkBoxValMsg3 = " REAR  SWAY  BAR";     blkBoxValMsg4 = " ADJ  SEAT  POS ";
  
    blkBoxChar1 = 0;    blkBoxChar2 = 0;    blkBoxChar3 = 0;  blkBoxChar4 = 0;
  }
  
  else if (gameVal == 2)  //change messages and set characters to F1-F4
  {
    blkBoxValMsg1 = "  LAP   TIMING  ";     blkBoxValMsg2 = "    STANDINGS   ";
    blkBoxValMsg3 = "RELATIVE  TIMING";     blkBoxValMsg4 = " FUEL  SETTINGS ";
  
    blkBoxChar1 = 0xC2;    blkBoxChar2 = 0xC3;    blkBoxChar3 = 0xC4;  blkBoxChar4 = 0xC5;
  }
  
  if (gameVal == 1 && blkBoxVal == 1)
  {
    lcd.print(blkBoxValMsg[blkBoxVal]);  
  }
  
  else if (gameVal == 2 && blkBoxVal == 1)  //different game, output different message and press a key
  {
    lcd.print(blkBoxValMsg[blkBoxVal]);
    Keyboard.press(twelveWayBlkBoxKeys[blkBoxVal]);
    delay(150);
    Keyboard.release(twelveWayBlkBoxKeys[blkBoxVal]);
  }
}

Seems You have duplicated local variables in the setup - do you also have global ones we did not see either the same name?

Read about variable scope

(its always better to post the full sketch, more often than not the issue you are looking for is in the unsuspected part)

So for example, if I'm on position 1, I might want the messages output by certain buttons to be different than if I was on position 2 or 3 etc...

How about using a 2 dimensional array ? The switch position would select the row and a button press would select the matching message from the current row.

I'll give the 2 dimensional array a shot (didn't think of that!)

As for the full sketch, its 749 lines long so it was well past the 9,000 character limit :smiley:

J-M-L, I didn't think I was duplicating as I was using the initially declared variables to hold messages that could change based on other criteria, and the use of those within the array was my way (I thought) of loading those variables into the array.

My thought process here was to be able to just change those variables with updated values, rather than re-load into the array each time it changes.

The 2 dimensional array is definitely the better way to handle this I think so I'll give that a shot first.

Thanks to you both!

FastGT94:
As for the full sketch, its 749 lines long so it was well past the 9,000 character limit :smiley:

One can always attach code in that case

FastGT94:
J-M-L, I didn't think I was duplicating as I was using the initially declared variables to hold messages that could change based on other criteria, and the use of those within the array was my way (I thought) of loading those variables into the array.

As you add the word String in front of the initialization in the setup() you are actually creating a local variable that will get discarded when setup() exist and you are not using the global variable

The 2D array is indeed a good way and you don’t want to use the String class for this (bad idea) - just use plain old c-string

const char * messages[12][] = {
{“m00”,”m01”,”m02”},
{“m10”,”m11”,”m12”},
...
};

then you just access the right message with messages[rotaryPos][buttonIndex];

Could even store them in flash memory only with PROGMEM, building the array would then be a bit more convoluted but would save SRAM