Flickering LEDs with Shift Register

I have 5 LED Modules daisy chained together with basic 595 shift registers.

I thought I had my code all squared away and I was going to go ahead and jump to external power but thats another thread.

Went to bed, woke up and now my last shift register is behaving oddly.

I have panels 1-4 set to HIGH and panel 5 LOW. I have panels 1-4 connected to a PWM pin for dimming, works great. Panel 5 is not PWM and is grounded as I dont want that to dim.

When I dim panels 1-4, and panel 5 is off randomly half second flickering occurs in the LEDs. It isnt a wiring issue because ive rotated the panels around and its always the 5th panel.

The On/off of panels 1-4 are on one button. the On/Off for panel 5 is on another button.

int datapin = 3;   //pin 14 on the 75HC595
int latchpin = 4;  //pin 12 on the 75HC595
int clockpin = 13; //pin 11 on the 75HC595
int emergencySwitch = 11; //pin for button
int headlightSwitch = 12;
int embuttonPushCounter = 0;   // counter for the number of button presses
int emButtonState = 0;// current state of the button
int hlButtonState = 0;
int hlbuttonPushCounter = 0;
int lasthlButtonState = 0;
int lastemButtonState = 0;     // previous state of the button
int outputEnablePin = 9;
int headlightBrightnessPin = 5;

//How many of the shift registers - change this
#define number_of_74hc595s 5 

//do not touch
#define numOfRegisterPins number_of_74hc595s * 8

boolean registers[numOfRegisterPins];

void setup(){
  pinMode(datapin, OUTPUT);
  pinMode(latchpin, OUTPUT);
  pinMode(clockpin, OUTPUT);
  pinMode(outputEnablePin, OUTPUT);
  pinMode(outputEnablePin, OUTPUT);
  pinMode(emergencySwitch, INPUT_PULLUP);
  pinMode(headlightSwitch, INPUT_PULLUP);


  //reset all register pins
  clearRegisters();
  writeRegisters();
  
//  switchOff(); 
   Serial.begin(9600);
}               


//set all register pins to LOW
void clearRegisters(){
  for(int i = numOfRegisterPins - 1; i >=  0; i--){
     registers[i] = LOW;
  }
} 


//Set and display registers
//Only call AFTER all values are set how you would like (slow otherwise)
void writeRegisters(){
  digitalWrite(latchpin, LOW);

  for(int i = numOfRegisterPins - 1; i >=  0; i--){
    digitalWrite(clockpin, LOW);

    int val = registers[i];

    digitalWrite(datapin, val);
    digitalWrite(clockpin, HIGH);
  }
  digitalWrite(latchpin, HIGH);

}

//set an individual pin HIGH or LOW
void setRegisterPin(int index, int value){
  registers[index] = value;
}


void loop(){

 emergencyLights();
 headLights();
  
}

void headLights()
{
  //Button 2 
   hlButtonState = digitalRead(headlightSwitch);
  // compare the emButtonState to its previous state
  if (hlButtonState != lasthlButtonState) {
    // if the state has changed, increment the counter
    if (hlButtonState == LOW) {
      // if the current state is HIGH then the button
      // went from off to on:
      hlbuttonPushCounter++;
      Serial.print("number of button pushes:  ");
      Serial.println(hlbuttonPushCounter); 
      delay(20);     // delay for button deboucning 
    }         
   if (hlbuttonPushCounter == 0){
        Serial.println("Light Setting 1");
       hlswitchOff();
      } 
      if (hlbuttonPushCounter == 1){
        Serial.println("Light Setting 2");
        steadyWhiteFront();       
      } 

 }
  if (hlButtonState == lasthlButtonState) {
    if (hlbuttonPushCounter == 0){
        Serial.println("Light Setting 1");
        hlswitchOff();
      } 
      if (hlbuttonPushCounter == 1){
        Serial.println("Light Setting 2");
        steadyWhiteFront();      
      } 

  }
    
  
  if (hlbuttonPushCounter >=2) {
    hlbuttonPushCounter = 0;
    Serial.println(hlbuttonPushCounter);
 }  
   lasthlButtonState = hlButtonState;
   
}  

void emergencyLights()
{
  emButtonState = digitalRead(emergencySwitch);
  
  if (emButtonState != lastemButtonState) {
    
    if (emButtonState == LOW) {
      
      embuttonPushCounter++;
      Serial.print("number of button pushes:  ");
      Serial.println(embuttonPushCounter); 
      delay(20);     // delay for button deboucning 
    }         
   if (embuttonPushCounter == 0){
        Serial.println("Light Setting 1");
        switchOff();
      } 
      if (embuttonPushCounter == 1){
        Serial.println("Light Setting 2");
        // do nothing right now     
      } 
      if (embuttonPushCounter == 2){
        Serial.println("Light Setting 3");
        steadyOn();
      }   
      if (embuttonPushCounter == 3){
        Serial.println("Light Setting 4");
        steadySidesOnDim();
      }   

 }
  if (emButtonState == lastemButtonState) {
    if (embuttonPushCounter == 0){
        Serial.println("Light Setting 1");
        switchOff();
      } 
      if (embuttonPushCounter == 1){
        Serial.println("Light Setting 2");
               // do nothing right now
      } 
      if (embuttonPushCounter == 2){
        Serial.println("Light Setting 3");
        steadyOn();
      }   
      if (embuttonPushCounter == 3){
        Serial.println("Light Setting 4");
        steadySidesOnDim();
      }   
  
  if (embuttonPushCounter >=4) {
    embuttonPushCounter = 0;
    Serial.println(embuttonPushCounter);
 }  
   lastemButtonState = emButtonState;
}
}
  
void steadyOn()
{
  int index;
 
  setBrightness(255);
 
  for(index = 0; index <=31; index++)
  {
    setRegisterPin(index, HIGH);
   writeRegisters();
  }
}

void steadySidesOnDim()
{
  int index;
   for(index = 0; index <= 31; index++)
  {
    setBrightness(30);
    setRegisterPin(index, HIGH);	
    writeRegisters();   
      
  }    
}

void hlswitchOff()
{
  int index1;

setHLBrightness(0);
  for(index1 = 32; index1 <= 39; index1 ++)
    {
      setRegisterPin(index1, LOW);
      delay(0);
      writeRegisters();
    }

}


void switchOff()
{
  int index;
  
  for(index = 0; index <= 31; index ++)
    {
      setRegisterPin(index, LOW);
      writeRegisters();
    }
}


void steadyWhiteFront()
  {
    int index;
  
    for(index = 32; index <= 39; index ++)
    {
      setHLBrightness(255);      
      setRegisterPin(index, HIGH);
      writeRegisters();
    }
  }


void setBrightness(byte brightness) // 0 to 255
{
  analogWrite(outputEnablePin, 255-brightness);
}
void setHLBrightness(byte brightness) // 0 to 255
{
  analogWrite(headlightBrightnessPin, 255-brightness);
}
void clearRegisters(){
  for(int i = numOfRegisterPins - 1; i >=  0; i--){
     registers[i] = LOW;
  }
}

For loops that count up are far easier to understand. Why are your counting down?

    int val = registers[i];

    digitalWrite(datapin, val);

Why is it necessary to copy the value?

//set an individual pin HIGH or LOW
void setRegisterPin(int index, int value){
  registers[index] = value;
}

One line functions are a waste of time and effort.

void steadyOn()
{
  int index;
 
  setBrightness(255);
 
  for(index = 0; index <=31; index++)
  {
    setRegisterPin(index, HIGH);
   writeRegisters();
  }
}

I would think that you would write all the values to the registers first, then write the register values to the pins.

The first three code boxes you posted are from another members tutorial. I was having trouble getting my original to work and this variation makes the individual pins addressable. Why they made the functions like that i have no idea

UPDATE:

using an Uno there is no problem, however the micro, which is what i want in my project is having the flickering.... thoughts?

Edit: You ever just start cleaning things up codewise and wala, it works? But i have no idea what I did, so much for learning lol!

Thanks all