duplicated blinking without delay - debugging issue

Hello, I am finding it difficult to debug a code that I have written. I have created a code to blink LEDs to show the currently selected mode (it's for a midi controller). I have two mode buttons for my potentiometers and three mode buttons for my encoders. I have used the blinking without delay as a guide and created three possible modes (with the LED ON for mode 1, BLINKING for mode 2 and BLINKING FAST for mode 3). Now the code works fine by itself. However, due to shortage of pins I had to use a multiplexer and I had to separate the potentiometers and the encoders (I now have my 2 mode buttons for the potentiometers connected to the multiplexer).
I thought it would be a simple matter of splitting the code. I changed the name of the variables adding a "Pot" suffix to all the variables I was using for the encoders mode buttons.
Although the mode selection function works fine, the LED blinking function is not working as expected (button making blink the wrong led, turning off instead of blinking, all sort of things...). If I comment out the "switch - case "part of the code for either the potentiometer mode buttons or the encoders mode buttons the other one respectively work fine but they would not work together.
I am posting below the part of the code that seems to be problematic . I can post the whole code if helpful - but in a separate post as it makes me go over the 9000 characters allowed.
Thanks in advance for any help .

  switch (modeNumb[i]) {
      case 1:
        digitalWrite(modeLedPin[i], HIGH);                            // SWITCH LED ON
        break;
      case 2:                                                         // LED BLINKING SLOW
        if (currentMillis - previousMillis[i] >= 500) {               // check that 500 ms have passed
          previousMillis[i] = currentMillis;                          // save the last time you blinked the LED
          if (modeLedState[i] == LOW) {
            modeLedState[i] = HIGH;
          }
          else {
            modeLedState[i] = LOW;
          }
          digitalWrite(modeLedPin[i], modeLedState[i]);                // set the LED with the ledState of the variable:
        }
        break;
      case 3:
        if (currentMillis - previousMillis[i] >= 200) {                // check that 200 ms have passed
          previousMillis[i] = currentMillis;                           // save the last time you blinked the LED
          if (modeLedState[i] == LOW) {
            modeLedState[i] = HIGH;
          }
          else {
            modeLedState[i] = LOW;
          }
          digitalWrite(modeLedPin[i], modeLedState[i]);                // set the LED with the ledState of the variable:
        }
        break;
}

and/or this:

  switch (modeNumbPot[i - MuxCCNumber]) {
      case 1:
        digitalWrite(modeLedPinPot[i - MuxCCNumber], HIGH);                            // SWITCH LED ON
        break;
      case 2:                                                                            // LED BLINKING SLOW
        if (currentMillis - previousMillisPot[i - MuxCCNumber] >= 500) {               // check that 500 ms have passed
          previousMillisPot[i - MuxCCNumber] = currentMillis;                          // save the last time you blinked the LED
          if (modeLedStatePot[i - MuxCCNumber] == LOW) {
            modeLedStatePot[i - MuxCCNumber] = HIGH;
          }
          else {
            modeLedStatePot[i - MuxCCNumber] = LOW;
          }
          digitalWrite(modeLedPinPot[i - MuxCCNumber], modeLedStatePot[i - MuxCCNumber]);                // set the LED with the ledState of the variable:
        }
        break;
      case 3:
        if (currentMillis - previousMillisPot[i - MuxCCNumber] >= 200) {                                   // check that 200 ms have passed
          previousMillisPot[i - MuxCCNumber] = currentMillis;                                              // save the last time you blinked the LED
          if (modeLedStatePot[i - MuxCCNumber] == LOW) {
            modeLedStatePot[i - MuxCCNumber] = HIGH;
          }
          else {
            modeLedStatePot[i - MuxCCNumber] = LOW;
          }
          digitalWrite(modeLedPinPot[i - MuxCCNumber], modeLedStatePot[i - MuxCCNumber]);                // set the LED with the ledState of the variable:
        }
        break;
}

It would be easier if you could post a complete program that demonstrates the problem. If it is too big to post then you can attach it to a post

In this line

  digitalWrite(modeLedPinPot[i - MuxCCNumber], HIGH);

What is the smallest value of i that can occur ?

Well done!!! you have already found the issue! I am going to post the whole code to show my mistake.
note the missing line :
if (i >= MuxCCNumber) {.....}
....now included in v 2 which is working very well.
I am very impressed :slight_smile:

PS

One thing it may be apparent from my code is my limitations in coding and whilst trying to resolve this issue I was wandering if I should try and minimise the use of global variables and also create functions(i.e. blinking and blinking fast functions) to keep the code simpler. ... but this may be the subject for another post....
THANKS A LOT!!!!!

mux_and_both_modes_v1.ino (11.5 KB)

mux_and_both_modes_v2.ino (11.6 KB)