More efficient way to code this?

There has gotta be a more efficient way to code this. I'm using a mega to fade RGB's through the color spectrum. Right now the code is written for 2 RGB's but I'm going to expand it to 5 and the program is getting pretty long.
[code
/*
 * RGB fade sketch for 2 RGB's 
 * 
*/

//------  define pins
#define red1 2
#define green1 3
#define blue1 4
#define red2 5
#define green2 6
#define blue2 7
#define delayTime 10

//-------  create variables
int red1Val;
int green1Val;
int blue1Val;
int red2Val;
int green2Val;
int blue2Val;

//---------  create outputs
void setup(){
  pinMode(red1,OUTPUT);
  pinMode(green1,OUTPUT);
  pinMode(blue1,OUTPUT);
  pinMode(red2,OUTPUT);
  pinMode(green2,OUTPUT);
  pinMode(blue2,OUTPUT);
}
//----------  fade RGB's through the color spectrum
void loop(){
  
  int red1Val = 255;
  int blue1Val = 0;
  int green1Val = 0;
  for( int i = 0 ; i < 255 ; i += 1 ){
    green1Val += 1;
    red1Val -= 1;
    analogWrite( green1, 255 - green1Val );
    analogWrite( red1, 255 - red1Val );

    delay( delayTime );
  }
 
  red1Val = 0;
  blue1Val = 0;
  green1Val = 255;
  for( int i = 0 ; i < 255 ; i += 1 ){
    blue1Val += 1;
    green1Val -= 1;
    analogWrite( blue1, 255 - blue1Val );
    analogWrite( green1, 255 - green1Val );

    delay( delayTime );
  }
 
  red1Val = 0;
  blue1Val = 255;
  green1Val = 0;
  for( int i = 0 ; i < 255 ; i += 1 ){
    red1Val += 1;
    blue1Val -= 1;
    analogWrite( red1, 255 - red1Val );
    analogWrite( blue1, 255 - blue1Val );

    delay( delayTime );
  }
//----------  RGB 2  ---------------//   
  int red2Val = 255;
  int blue2Val = 0;
  int green2Val = 0;
  for( int i = 0 ; i < 255 ; i += 1 ){
    green2Val += 1;
    red2Val -= 1;
    analogWrite( green2, 255 - green2Val );
    analogWrite( red2, 255 - red2Val );

    delay( delayTime );
  }
 
  red2Val = 0;
  blue2Val = 0;
  green2Val = 255;
  for( int i = 0 ; i < 255 ; i += 1 ){
    blue2Val += 1;
    green2Val -= 1;
    analogWrite( blue2, 255 - blue2Val );
    analogWrite( green2, 255 - green2Val );

    delay( delayTime );
  }
 
  red2Val = 0;
  blue2Val = 255;
  green2Val = 0;
  for( int i = 0 ; i < 255 ; i += 1 ){
    red2Val += 1;
    blue2Val -= 1;
    analogWrite( red2, 255 - red2Val );
    analogWrite( blue2, 255 - blue2Val );

    delay( delayTime );
  }
}
]

Please explain. Your comments are extremely sparse. Describe the desired actions in detail.

i < 255 Not interested in full brightness?

use sub-functions

// -----------------------------------------------------------------------------
void fade (
    uint8_t pinA,
    uint8_t pinB )
{
  int valA = 0;
  int valB = 255;
  for (int i = 0 ; i < 255 ; i += 1 ){
    valA += 1;
    valB -= 1;
    analogWrite (pinA, 255 - valA );
    analogWrite (pinB, 255 - valB );

    delay (delayTime );
  }
}

//----------  fade RGB's through the color spectrum
void loop (){
    fade (green1, red1);
    fade (blue1, green1);
    fade (red1, blue1);

    fade (green2, red2);
    fade (blue2, green2);
    fade (red2, blue2);
}

“You do not need to call pinMode() to set the pin as an output before calling analogWrite().”

https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/

So basically I’m printing a few mushrooms on my 3d printer to put in my backyard landscaping. I’m planning on putting some RGB’s in them so the fade through the color spectrum. I’m guessing it’s better to create an array and call a function to fade each RGB individually but using an array is something I’m not very familiar with.

If I’m trying to be efficient with the code would it be best to create an array or should I just continue to type out the code in the same manner that it is currently being written?

Also would “i <= 255” fix the problem with the RGB’s not goint to full brightness?

Thanks

“i < 256” is simpler to write.

OP, the index 'i' is not the only one with problems - valA and valB also don't cover the range 0-255 correctly in the loop. Do the math.