Code optimization?

ok not so much optimization, shorten it?.

void ShiftByteOut(byte chip, byte b)
{  

   byte bp = B00000000;
   byte bp2 = B00000000;   
   digitalWrite(latchPin, LOW);       
   if (chip==1)
     {
     shiftOut(dataPin, clockPin, MSBFIRST, bp2);
     shiftOut(dataPin, clockPin, MSBFIRST, bp2);      
     shiftOut(dataPin, clockPin, MSBFIRST, b);
     } 
    if (chip==2)
     {
      shiftOut(dataPin, clockPin, MSBFIRST, bp2);
      shiftOut(dataPin, clockPin, MSBFIRST, b);      
      shiftOut(dataPin, clockPin, MSBFIRST, bp2);
     } 
     if (chip==3)
     {
      shiftOut(dataPin, clockPin, MSBFIRST, b);
      shiftOut(dataPin, clockPin, MSBFIRST, bp2);      
      shiftOut(dataPin, clockPin, MSBFIRST, bp2);
     }        
    digitalWrite(latchPin, HIGH);     
}

Easiest way to shorten this code? thanks :slight_smile:

Shorten the source, or shorten the generated code?
Why bother?

for example, if i daisyed them up 5, 10, 50? how do i make this routine work with 5 not 3....

ok i think i worked it out myself... brb

#define MaxShiftRegisters 4 //actually 3 but works with this for loop.

void ShiftByteOut(byte chip, byte b)
{  
   byte bp = B00000000;
   byte bp2 = B00000000;   
   digitalWrite(latchPin, LOW);  
   for(byte n2=0; n2<MaxShiftRegisters; n2++)  
   {
      Serial.print("n2 ");
      Serial.println(n2);
      if (chip==n2)
         shiftOut(dataPin, clockPin, MSBFIRST, b);
       else
         shiftOut(dataPin, clockPin, MSBFIRST, bp2);
   }     
   digitalWrite(latchPin, HIGH);     
}