Unintended Randomness

I have built a circuit on my Arduino Mega 2560, with 8 LEDS plugged into digital ports 39, 41, 43, 45, 47, 49, 51, and 53. They are controlled by 8 Momentary Push Buttons plugged into digital ports 38, 40, 42, 44, 46, 48, 50, and 52. My code is as follows:

const int LED1 = 39;
const int LED2 = 41;
const int LED3 = 43;
const int LED4 = 45;
const int LED5 = 47;
const int LED6 = 49;
const int LED7 = 51;
const int LED8 = 53;
const int Button1 = 38;
const int Button2 = 40;
const int Button3 = 42;
const int Button4 = 44;
const int Button5 = 46;
const int Button6 = 48;
const int Button7 = 50;
const int Button8 = 52;
int CurrentButton[]={Button1, Button2, Button3, Button4, Button5, Button6, Button7, Button8};
int CB;
int CurrentLED[]={LED1,LED2,LED3,LED4,LED5,LED6,LED7,LED8};
int CL;
int count;
         
void setup(){
   pinMode(LED1, OUTPUT);
   pinMode(LED2, OUTPUT);
   pinMode(LED3, OUTPUT);
   pinMode(LED4, OUTPUT);
   pinMode(LED5, OUTPUT);
   pinMode(LED6, OUTPUT);
   pinMode(LED7, OUTPUT);
   pinMode(LED8, OUTPUT);
   pinMode(Button1, INPUT);
   pinMode(Button2, INPUT);
   pinMode(Button3, INPUT);
   pinMode(Button4, INPUT);
   pinMode(Button5, INPUT);
   pinMode(Button6, INPUT);
   pinMode(Button7, INPUT);
   pinMode(Button8, INPUT);
   CB=CurrentButton[0];
   CL=CurrentLED[0];
   count=0;
}
         
void loop(){
  for (count=0;count<8;count++) {
  if (digitalRead(CB) == HIGH) {
    digitalWrite(CL, LOW);
    CB=CurrentButton[count];
    CL=CurrentLED[count];
  }
  else {
    digitalWrite(CL, HIGH);
  }
  }
}

My intended purpose was for each button to be corresponded to 1 LED (which worked), and for when each button was pressed its LED to turn off (this works) and the next in sequence to turn on (this doesn't work). While when an LED's corresponding button is pressed it does turn off, it isn't the next one in sequence, it is just a random other LED. I call it random because I have reset my arduino and while the first LED is always 1, the second LED to light up could be any of them.

How are the buttons wired?

MorganS:
How are the buttons wired?

One side is wired to my positive rail, and the other has a 220 Ω resistor to the ground rail and is connected to the corresponding pin(38-52 evens only).

Do you have current limiting resistors on all the LEDs?

This code seems out of place. CB and CL are not updated if the else clause is executed.

    CB=CurrentButton[count];
    CL=CurrentLED[count];

jremington:
Do you have current limiting resistors on all the LEDs?

Yes every LED has a built in resistor.

void loop() {

for (count = 0; count < 8; count++) {
   if (digitalRead(CB) == HIGH) {
     digitalWrite(CL, LOW);
     CB = CurrentButton[count];
     CL = CurrentLED[count];
   }
   else {
     digitalWrite(CL, HIGH);
   }
 }
}

So, work though the logic. At the very beginning of loop, the first time that it runs, CB is holding the pin number of the zeroth button. Count starts at zero and works its way towards 8.

Some time later, you press that particular button and the digitalRead() returns HIGH. What is the value of count at that moment?

It looks like a fun whack-a-mole game right now. Maybe it's better that way?

jremington:
This code seems out of place. CB and CL are not updated if the else clause is executed.

    CB=CurrentButton[count];

CL=CurrentLED[count];

That is correct, I do not want CB and CL to update when the else clause is executed. I only want those to change when the button is pressed. The else clause simple tells the LED to remain on if the button hasn't been pressed yet.

MorganS:
So, work though the logic. At the very beginning of loop, the first time that it runs, CB is holding the pin number of the zeroth button. Count starts at zero and works its way towards 8.

Some time later, you press that particular button and the digitalRead() returns HIGH. What is the value of count at that moment?

It looks like a fun whack-a-mole game right now. Maybe it's better that way?

I see my problem now, I am just counting the entire time when I don't want to be. What is the correct way to increase the count only when the button is pressed?

Use the loop(). You don't need for().

void loop() {
  if (digitalRead(CB) == HIGH) {
    digitalWrite(CL, LOW);
    count++;
    if(count>=sizeof(CurrentButton)/sizeof(CurrentButton[0])) count = 0;
    CB = CurrentButton[count];
    CL = CurrentLED[count];
    digitalWrite(CL, HIGH);
  }
}

Note that there's no hard-coded "8" in the code. It looks at the size of the button array to find out how high count can go.

Holding down all the buttons is going to cycle so fast that it will appear all LEDs are on at once.

Awesome! Thank you so much!