Need Help/Direction on how to make my sketch size smaller

Hello

I need some help with making my sketch smaller. I just spent a few hours writing it and then realized it is going to be way too big.

I included a working example of what I am trying to do. My actual sketch is beyond the byte capacity of an Uno.
I am using a TLC5940 and am manually fading the channels.
I have 10 RGBs that fade in and out 10 colors at different intervals using switch case.
I also have functions that turn off the channels for a blackout period for the RGBs.

I know my problem lies in writing out each and every fade functions.

Any help or insight on how to compress this sketch is much appreciated.

This working example is for 2 RGBs and 2 colors.

#include "Tlc5940.h"  


//--------Fade Variables----------------------  

long previousMillis1 = 0;        
long interval1 = 10;                  

long previousMillis2 = 0;        
long interval2 = 10;                  


  
//-------------------Fade Values for Colors-----------------------
//-----------------Red-----

int rfadeValue1 = 0;                                     //Initial fade values
int rfadeValue2 = 0;

//-----------------Green-----

int gfadeValue1 = 0;
int gfadeValue2 = 0;

//-----------------Blue-------

int bfadeValue1 = 0;
int bfadeValue2 = 0;




//-----------Switch Case Variables----------------------

int led1State = 0;
boolean next1 = false;

int led2State = 0;
boolean next2 = false;


//----------------------Count/Time for Blackout-----
long int time1 = 10UL;
unsigned long count1 = 0;

long int time2 = 10UL;
unsigned long count2 = 0;

boolean timeout(unsigned long *marker, unsigned long interval) {         //Timer for Blackout phase
  if (millis() - *marker >= interval) { 
    *marker += interval;    // move on ready for next interval
    return true;       
  } 
  else return false;
}


void setup(){
  
  
  Tlc.init();         
  led1State = 0;
  led2State = 0;
  
  time1 = 1UL;
  time2 = 1UL;
  
       
}

void loop() {

rgbAction();

}




  
  void rgbAction(){ 
  
  //------------------------------1-------------------------
    
    switch (led1State) {
       case 0: 
        {    
      interval1 = 10;          
        blueOn1();
        if (next1){
            next1 = false;
            led1State = 1;
        }
        break; 
      }
      
      
    case 1: 
        {
      interval1 = 5;          
        blueOff1();
        if (next1){
            next1 = false;
            led1State = 2;
        }
        break; 
      }


    case 2: 
         {  
          black1();
          time1 = 20000UL;
        if (timeout(&count1, time1 )) {
            next1 = false;
            led1State = 3;
        }
        break; 
      }


    case 3: 
      {   
      interval1 = 5;        
        magentaOn1();
        if (next1){
            next1 = false;
            led1State = 4;
        }
        break; 
      }
      
      case 4: 
      {   
      interval1 = 5;        
        magentaOff1();
        if (next1){
            next1 = false;
            led1State = 0;
        }
        break; 
      }
      }
    
//=======================================2============    
    
    
    switch (led2State) {
       case 0: 
        { 
      interval2 = 3;          
        magentaOn2();
        if (next2){
            next2 = false;
            led2State = 1;
        }
        break; 
      }
      
      
    case 1: 
        {
      interval2 = 4;          
        magentaOff2();
        if (next2){
            next2 = false;
            led2State = 2;
        }
        break; 
      }


    case 2: 
         {  
          black2();
          time2 = 5000UL;
        if (timeout(&count2, time2 )) {
            next2 = false;
            led2State = 3;
        }
        break; 
      }


    case 3: 
      {   
      interval2 = 4;        
        blueOn2();
        if (next2){
            next2 = false;
            led2State = 4;
        }
        break; 
      } 
      
      case 4: 
        { 
      interval2 = 3;          
        blueOff2();
        if (next2){
            next2 = false;
            led2State = 0;
        }
        break; 
      }
     }
  }



//-----------------------BlackOut Funtions, period when RGB is off----------------


void black1(){
   
        Tlc.set(0, 0);
        Tlc.set(1, 0);
        Tlc.set(2, 0);
        Tlc.update();
   }
   
   void black2(){
   
        Tlc.set(3, 0);
        Tlc.set(4, 0);
        Tlc.set(5, 0);
        Tlc.update();
   }
   
//-------------------------------Blue fades for both RGBs-------   
     
void blueOn1(){  
   unsigned long currentMillis1 = millis();                      
 if(currentMillis1 - previousMillis1 > interval1) { 
    previousMillis1 = currentMillis1;   
  if(bfadeValue1 <= 4000, bfadeValue1 +=5) {
    
    Tlc.set(2, bfadeValue1);                
    
}
 Tlc.update(); 
   }
if(bfadeValue1 >= 4000){
next1 = true;
  }  
 }
  



void blueOff1(){ 
   unsigned long currentMillis1 = millis();
 if(currentMillis1 - previousMillis1 > interval1) { 
    previousMillis1 = currentMillis1;
  if(bfadeValue1 >=0, bfadeValue1 -=5){
  
    Tlc.set(2, bfadeValue1);              
    
    }
 Tlc.update();
    } 
 if(bfadeValue1 <= 0){
next1 = true;
  }
 }
   
//-----#2---------------------

  
void blueOn2(){  
   unsigned long currentMillis2 = millis();                      
 if(currentMillis2 - previousMillis2 > interval2) { 
    previousMillis2 = currentMillis2;   
  if(bfadeValue2 <= 4000, bfadeValue2 +=5) {
    
    Tlc.set(5, bfadeValue2);                 
    
    
}
 Tlc.update(); 
   }
if(bfadeValue2 >= 4000){
next2 = true;
  }  
 }
  



void blueOff2(){ 
   unsigned long currentMillis2 = millis();
 if(currentMillis2 - previousMillis2 > interval2) { 
    previousMillis2 = currentMillis2;
  if(bfadeValue2 >=0, bfadeValue2 -=5){
  
    Tlc.set(5, bfadeValue2);              
    
    }
 Tlc.update();
    } 
 if(bfadeValue2 <= 0){
next2 = true;
  }
 }
   
   
   
//-------------------------------Magenta fades for both RGBs-------


  
void magentaOn1(){  
   unsigned long curentMillis1 = millis();                      
 if(curentMillis1 - previousMillis1 > interval1) { 
    previousMillis1 = curentMillis1;   
  if(rfadeValue1 <= 4000, rfadeValue1 +=5, bfadeValue1 <= 4000, bfadeValue1 +=5) {
    
    Tlc.set(0, rfadeValue1);                
    Tlc.set(2, bfadeValue1); 
    
    
}
 Tlc.update(); 
   }
if(bfadeValue1 >= 4000){
next1 = true;
  }  
 }
  



void magentaOff1(){ 
   unsigned long curentMillis1 = millis();
 if(curentMillis1 - previousMillis1 > interval1) { 
    previousMillis1 = curentMillis1;
  if(rfadeValue1 >=0, rfadeValue1 -=5, bfadeValue1 >=0, bfadeValue1 -=5){
  
    Tlc.set(0, rfadeValue1);              
    Tlc.set(2, bfadeValue1); 
    
    }
 Tlc.update();
    } 
 if(bfadeValue1 <= 0){
next1 = true;
  }
 }
   
//------#2-------

  
void magentaOn2(){  
   unsigned long currentMillis2 = millis();                      
 if(currentMillis2 - previousMillis2 > interval2) { 
    previousMillis2 = currentMillis2;   
  if(rfadeValue2 <= 4000, rfadeValue2 +=5, bfadeValue2 <= 4000, bfadeValue2 +=5) {
    
    Tlc.set(3, rfadeValue2);                
    Tlc.set(5, bfadeValue2); 
    
    
}
 Tlc.update(); 
   }
if(bfadeValue2 >= 4000){
next2 = true;
  }  
 }
  



void magentaOff2(){ 
   unsigned long currentMillis2 = millis();
 if(currentMillis2 - previousMillis2 > interval2) { 
    previousMillis2 = currentMillis2;
  if(rfadeValue2 >=0, rfadeValue2 -=5, bfadeValue2 >=0, bfadeValue2 -=5){
  
    Tlc.set(3, rfadeValue2);              
    Tlc.set(5, bfadeValue2); 
    
    }
 Tlc.update();
    } 
 if(bfadeValue2 <= 0){
next2 = true;
  }
 }

Won't help with size, but these should be unsigned long, all time related elements should be unsigned long as that is what millis(); returns. Keep the math consistent:

long previousMillis1 = 0;
long interval1 = 10;

long previousMillis2 = 0;
long interval2 = 10;

These look like they could all be byte instead of int:
int led1State = 0;

These look a little mixed up - they should be unsigned long:
long int time1 = 10UL;

How big is the code compiling to?
Next step might be to go to a 1284P based board. 128K flash, 16K SRAM.
I offer them in several form-factors, this one is like a Duemilanove:
http://www.crossroadsfencing.com/BobuinoRev17/

@1rulon1

TIP: in the IDE you can do TOOLS->AUTO FORMAT to reindent the code ==> makes the code more readable

Are you running out of space for code, or data, or both ?

That code doesn't really look too long for a Uno.

Whe knows how big the TLC5940 library is? That might be the space killer.

CrossRoads,

Thank you for pointing out my unsigned long issues.

The TLC5940 library is ~2000 bytes.

I eventually will have a few 'settings' that I can switch through with an IR remote.

Right now the initializes, setup and just one Setting is ~30,000 bytes.

I may need to go to something bigger as you suggested.