LED not working properly

I am working on a little totem tower with 6 sets of 3 LED and 1 button. I have it set up, for now, so that every button press, one random light turns either on or off depending on its previous state.

problem is that once lights from light[4] and light[5] turn on, my program says they are always off but it leaves them on.

I’m not quite sure what my problem is or if I was able to explain it well enough, let me know if you know what could be causing this.



int light[7] = {12,10,8,6,4,2,22};

long randNum;
long tempNum = 0;

int BTN = 26;
int BTNstate = 0;

void setup() {
  // put your setup code here, to run once:
  for (int i = 0; i < 7; i++){
    pinMode(light[i], OUTPUT);
    //pinMode(light[i], INPUT);
  }

  pinMode(BTN, INPUT);
  
  Serial.begin(9600);
}

void loop() {

 // Serial.println("starting countdown");
  BTNstate = digitalRead(BTN);
  
  // put your main code here, to run repeatedly:
  /*for (int i = 0; i < 7; i++){
    digitalWrite(light[i], HIGH);
  }
  delay(1000);*/
  if (BTNstate == HIGH){
    Serial.println("starting countdown");
    randomLight(); 
  }
}

void randomLight(){ 
  //light flicker in a randomized patter for 10 seconds
  //for (int i = 0; i <=100; i++){
    randNum = random(7);

    Serial.print("random num is ");
    Serial.println(randNum);
  
    /*if (tempNum == randNum){
      if (randNum == 6)
        randNum -= 1;
      else if (randNum >= 0)
        randNum += 1;      
    }*/
      if (digitalRead(light[randNum]) == LOW){
        digitalWrite(light[randNum], digitalRead(!light[randNum]));
        Serial.print("IF light ");
        Serial.print(light[randNum]);
        Serial.print(" is ");
        Serial.println(digitalRead(light[randNum]));
      }
      else{
        digitalWrite(light[randNum], LOW);
        Serial.print("ELSE light ");
        Serial.print(light[randNum]);
        Serial.print(" is ");
        Serial.println(digitalRead(light[randNum]));
      }
    delay(1000);

    tempNum = randNum;
 // }
}

Two things, see how your code goes into italics?
That is caused by the forum software mistaking some of your code for text formatting instructions.
Edit your post select all your code and press the </> icon at the top of your reply box.

Second we need to see a schematic of how you have wired things up. In imbedded computing code is useless without knowing how the hardware is configured.a photograph would also be helpful but is no substitute for a schematic.

Also what Arduino do you have.

Hi, @Nestoring1
Welcome to the forum.

Please read the post at the start of any forum , entitled “How to use this Forum”.
OR

Then look down to “code problems” about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks… Tom… :grinning: :+1: :coffee: :australia:

In your code, what happens when randNum = 7 ?

I will post a picture of my wiring tomorrow since it’s a little late, sorry about the code format, first post on forum

I think

digitalWrite(light[randNum], digitalRead(!light[randNum]));

was supposed to be

digitalWrite(light[randNum], !digitalRead(light[randNum]));

although given you already know the answer is LOW at this point in the code, it would be clearer to put

digitalWrite(light[randNum], HIGH);

It won’t.

https://www.arduino.cc/reference/en/language/functions/random-numbers/random/

“long”? Seriously?