Looking to optimize my code, help needed

Hi everyone,

I am using an RGB LED to create a color changing rainbow sequence. I am making each color fade so that the transition between colors is smoother. This is a project from the DFRduino Beginner kit

I managed to get my code to work but I am looking to optimize it. I have a feeling I can use a for loop or a similar technique to shorten my code. I would appreciate any advice on how to optimize it.

Thank you!

Here's my code:

// Project 5 Exercise-- RGB Light with smooth, fading transition between colors

int redPin=9;
int greenPin=10;
int bluePin=11;

void setup() {
pinMode(redPin,OUTPUT);
pinMode(greenPin,OUTPUT);
pinMode(bluePin,OUTPUT);
}

void loop() {
fadeOn(500,5);
fadeOff(500,5);

fadeOn2(500,5);
fadeOff2(500,5);

fadeOn3(500,5);
fadeOff3(500,5);

fadeOn4(500,5);
fadeOff4(500,5);

fadeOn5(500,5);
fadeOff5(500,5);

fadeOn6(500,5);
fadeOff6(500,5);

fadeOn7(500,5);
fadeOff7(500,5);
}

void colorRGB(int red, int green, int blue){
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}

void fadeOn(int time, int increment){
for(byte value= 0; value < 255; value += increment){
colorRGB(value, 0, 0);
delay(time/(255/5));
}
}

void fadeOff(int time, int decrement){
for(byte value= 255; value > 0; value -= decrement){
colorRGB(value, 0, 0);
delay(time/(255/5));
}
}

void fadeOn2(int time, int increment){
for(byte value= 0; value < 255; value += increment){
colorRGB(0, value, 0);
delay(time/(255/5));
}
}

void fadeOff2(int time, int decrement){
for(byte value= 255; value > 0; value -= decrement){
colorRGB(0, value, 0);
delay(time/(255/5));
}
}

void fadeOn3(int time, int increment){
for(byte value= 0; value < 255; value += increment){
colorRGB(0, 0, value);
delay(time/(255/5));
}
}

void fadeOff3(int time, int decrement){
for(byte value= 255; value > 0; value -= decrement){
colorRGB(0, 0, value);
delay(time/(255/5));
}
}

void fadeOn4(int time, int increment){
for(byte value= 0; value < 255; value += increment){
colorRGB(value, value, 0);
delay(time/(255/5));
}
}

void fadeOff4(int time, int decrement){
for(byte value= 255; value > 0; value -= decrement){
colorRGB(value, value, 0);
delay(time/(255/5));
}
}

void fadeOn5(int time, int increment){
for(byte value= 0; value < 255; value += increment){
colorRGB(0, value, value);
delay(time/(255/5));
}
}

void fadeOff5(int time, int decrement){
for(byte value= 255; value > 0; value -= decrement){
colorRGB(0, value, value);
delay(time/(255/5));
}
}

void fadeOn6(int time, int increment){
for(byte value= 0; value < 255; value += increment){
colorRGB(value, 0, value);
delay(time/(255/5));
}
}

void fadeOff6(int time, int decrement){
for(byte value= 255; value > 0; value -= decrement){
colorRGB(value, 0, value);
delay(time/(255/5));
}
}

void fadeOn7(int time, int increment){
for(byte value= 0; value < 255; value += increment){
colorRGB(value, value, value);
delay(time/(255/5));
}
}

void fadeOff7(int time, int decrement){
for(byte value= 255; value > 0; value -= decrement){
colorRGB(value, value, value);
delay(time/(255/5));
}
}

Do you notice how your functions all look very similar?
Do you have an allergy to code tags?

Why do you have N fadeOn functions that all do the same? You can use one function and call it N times.

Same question for the fadeOff functions.

My read is that sometimes you want to fade just red, just green, just blue or some combination of the three. You could add parameters to the fade functions to tell them what to fade.

void fadeOn(int time, int increment, boolean r, boolean g, boolean b) {
  for (byte value = 0; value < 255; value += increment) {
    colorRGB(r ? value : 0, g ? value : 0, b ? value : 0);
    delay(time / (255 / 5));
  }
}

Then replace the calls like this:

fadeOn4(500,5);  // old way
fadeOn(500,5, true, true, false);  // fade red and green