using an Array to change 6 led States

I am working on a Traffic light project and i want to use an array to hold the on-off states for six LEDs (North-South: Red, yellow green; East-west: red, yellow,Green) I Figure it would be easier to change the values in one array than each LED individually when the time comes to change the lights. My Programming skills aren’t the best so I wanted to get some advice on how to properly execute it. I want to do something like this but i am not sure if it is correct or the best way:

int ledPins[] = { 
  2, 3, 4, 5, 6, 7 };
  int pinCount = 6;
byte lightstates [] = {0, 0, 1, 1, 0,0)[

for (int thisPin = 0; thisPin < pinCount; thisPin++) { 
    // turn the pin on or off by depending on the ledstates array:
    digitalWrite(ledPins[thisPin], lightstates);

Here is the sketch so far:

const byte NS_RED = 2;
const byte NS_YELLOW = 3;
const byte NS_GREEN = 4;
const byte EW_RED = 5;
const byte EW_YELLOW = 6;
const byte EW_GREEN = 7;
const byte NS_SENSOR = 8;
const byte EW_SENSOR = 9;
const byte CLOCK = 10;
const byte CLOCKled = 13;
byte NS_state = 0;
byte EW_state = 0;
int CLOCKledState = HIGH;
int CLOCKState;             // the current reading from the input pin
int lastCLOCKState = LOW;
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;
long CycleCounter = 0;

void setup ()
{
  Serial.begin(9600);
  pinMode (2, OUTPUT);  
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode (6, OUTPUT);  
  pinMode(7, OUTPUT);
  pinMode(8, INPUT);
  pinMode(9, INPUT);
  pinMode(10, INPUT);
  pinMode(13, OUTPUT);
  digitalWrite(CLOCKled, CLOCKledState);
  
}

void loop() {
  CheckClock();
  CheckSensors();
  
}

void CheckClock(){
    int reading = digitalRead(CLOCK);

  if (reading != lastCLOCKState) {
  
    lastDebounceTime = millis();
  } 

  if ((millis() - lastDebounceTime) > debounceDelay) {
    
    if (reading != CLOCKState) {
      CLOCKState = reading;

     if (CLOCKState == HIGH) {
        CLOCKledState = !CLOCKledState;
      }
      else CycleCounter ++;
    }
  }

 
  digitalWrite(CLOCKled, CLOCKState);

 
  lastCLOCKState = reading;
}
void CheckSensors(){
  
  NS_state = digitalRead(NS_SENSOR);
  EW_state = digitalRead(EW_SENSOR);
}

lights is sequence: 6 individual lights lamps... store the sequense in an array.. a parallell array to shoe times for each state. N If sensores for then;

loop check directio A; if traffic: loop: a sequence (in array) for green in that direction. check directio B; if traffic: loop: a sequence (in array) for green in that direction. to here..

Array with lights : Bit 00RYGryg (00100100) Red both directions The bit-pattern changes from to next stage at time decided by parallel array

Yes that is the general approach...I think-your English is a little hard to understand. My question is how do I set up an array to hold the six different states, then read from the array and change the LED's all within a for loop. I could program them all individually but there has to be a better way.

Something more like:

for (int i = 0; i < pinCount; i++)
    digitalWrite (ledPins[i], lightstates[i]);

Its traditional to use i, j, k for indices in loops - the name thisPin isn’t
very apt since its not a pin number, its an array index.

You can #define symbol names for the individual entries in the array,

#define NS_red 0
#define NS_yellow 1
#define NS_green 2
...

Then updating the state is

  lightstates [NS_yellow] = .... ;

ie you’ve given names to the indices to make the code readable. You
did this already for pin numbers (NS_RED etc) using const byte.

#define or const byte are different ways of achieving the same thing of course.

I hope this explains my thinking…
Havnt tried it…
Its blocking, but the idea can be used also with nonblocking code

// leds used:  2..7
byte ls[]={B100100,B010100,B001100,B011100,B100010,B100001,B100011}; // lights=ls
//ARRAY     R..R..  .Y.R..  ..GR..  .YGR..  R...Y.  R....G  R...YG
//          START    --- GREEN DIR A ----    ----  GREN DIR B -----
byte sec[]={ 1    ,   1   ,  5    ,   2   ,   1   ,   8   ,   2   }; // time this status, examples

void setup()
{ 
  for (byte i=2; i<=7; i++) pinMode(i,OUTPUT);
  pinMode(8,INPUT_PULLUP);
  pinMode(9,INPUT_PULLUP);
  
}

void loop()
{
   if (digitalRead(8)==HIGH) greenlights(1); // start sequence from byte 1
   setlights(0); // all red
   if (digitalRead(9)==HIGH) greenlights(4); // start sequence from byte 4   
   setlights(0); // all red
}

void greenlights(byte s)
{
  for (byte i=s; i<=s+2; i++) setlights(i);
}

void  setlights(byte j)
{
  byte lights=ls[j];
  byte mask=B100000;
  for (byte pin=2; pin<=7; pin++)
  {
   digitalWrite(pin, lights & mask);
    mask>>1;
    delay(1000*sec[j]);
  }
}

Thanks to both of you! Knut_Ny would you mind explaining the " void setlights (byte j) " section. I have never used mask or bit masking so I get confused at that point. But I think your example is a simpler and more elegant way of what I want to do. Thanks!

Eksample, masking

// leds used:  2..7
byte ls[]={B100100,[b]B010100[/b],B001100,B011100,B100010,B100001,B100011}; // lights=ls
byte sec[]={ 1    ,  [b] 1[/b]   ,  5    ,   2   ,   1   ,   8   ,   2   }; // time this status, examples

.....
   setlights(1); // shown bold bit pattern RYGryg=010100   YELLOW and red
....

void  setlights(byte j) // j is now 1 (points to 010100)
{
  byte lights=ls[j]; // lighths now contain  010100
  byte mask=B100000;
 //  for (byte pin=2; pin<=7; pin++) // result of this loop is:
  
   digitalWrite(2, 010100 & 100000); // bitwise and results 0 - there is no common position containing 1 (HIGH)
    mask>>1; // moves the '1' in mask to the right one position
   digitalWrite(3, 010100 & 010000); // bitwise and results 1 - there is a position containing 1 in both bytes
  mask>>1;
   digitalWrite(4, 010100 & 001000); // bitwise and results 0...
  mask>>1;
   digitalWrite(5, 010100 & 000100); // bitwise and results 1 ..
  mask>>1;
 ... similar for next two..
... you will find the loop a better solution
    delay(1000*sec[j]);  // read fakctor from the parallell array
...