RGB Color fade code help -- VERY SIMPLE

Hi guys i was wondering if there was a way to make this code i have fade through the colors slower... please help Thanks

// attiny85 RGB led rainbow fade

const int redPin = 3;  
const int grnPin = 4;  
const int bluPin = 1; 


void setup()
{
  pinMode(redPin, OUTPUT);     
  pinMode(grnPin, OUTPUT);     
  pinMode(bluPin, OUTPUT);     
}

void loop()
{
redtoyellow();
yellowtogreen();
greentocyan();
cyantoblue();
bluetomajenta();
majenatored();
}


void redtoyellow()
{
   digitalWrite(redPin, HIGH);
   digitalWrite(bluPin, LOW);

  // fade up green
  for(byte i=1; i<100; i++) {
    byte on  = i;
    byte off = 100-on;
    for( byte a=0; a<100; a++ ) {
      digitalWrite(grnPin, HIGH);
      delayMicroseconds(on);
      digitalWrite(grnPin, LOW);
      delayMicroseconds(off);
    }
  } 
}



void yellowtogreen()
{
   digitalWrite(grnPin, HIGH);
   digitalWrite(bluPin, LOW);

  // fade down red
  for(byte i=1; i<100; i++) {
    byte on  = 100-i;
    byte off = i;
    for( byte a=0; a<100; a++ ) {
      digitalWrite(redPin, HIGH);
      delayMicroseconds(on);
      digitalWrite(redPin, LOW);
      delayMicroseconds(off);
    }
  } 
}


void greentocyan()
{
   digitalWrite(grnPin, HIGH);
   digitalWrite(redPin, LOW);

  // fade up blue
  for(byte i=1; i<100; i++) {
    byte on  = i;
    byte off = 100-on;
    for( byte a=0; a<100; a++ ) {
      digitalWrite(bluPin, HIGH);
      delayMicroseconds(on);
      digitalWrite(bluPin, LOW);
      delayMicroseconds(off);
    }
  } 
}



void cyantoblue()
{
   digitalWrite(bluPin, HIGH);
   digitalWrite(redPin, LOW);

  // fade down green
  for(byte i=1; i<100; i++) {
    byte on  = 100-i;
    byte off = i;
    for( byte a=0; a<100; a++ ) {
      digitalWrite(grnPin, HIGH);
      delayMicroseconds(on);
      digitalWrite(grnPin, LOW);
      delayMicroseconds(off);
    }
  } 
}


void bluetomajenta()
{
   digitalWrite(bluPin, HIGH);
   digitalWrite(grnPin, LOW);

  // fade up red
  for(byte i=1; i<100; i++) {
    byte on  = i;
    byte off = 100-on;
    for( byte a=0; a<100; a++ ) {
      digitalWrite(redPin, HIGH);
      delayMicroseconds(on);
      digitalWrite(redPin, LOW);
      delayMicroseconds(off);
    }
  } 
}



void majenatored()
{
   digitalWrite(redPin, HIGH);
   digitalWrite(grnPin, LOW);

  // fade down blue
  for(byte i=1; i<100; i++) {
    byte on  = 100-i;
    byte off = i;
    for( byte a=0; a<100; a++ ) {
      digitalWrite(bluPin, HIGH);
      delayMicroseconds(on);
      digitalWrite(bluPin, LOW);
      delayMicroseconds(off);
    }
  } 
}

Oh and any tips on the code would be greatly appreciated :slight_smile: Thanks

As all faders do the same it is easier to use one function. To fade slower you could use delays.

I did not test it, but this one should do:

// attiny85 RGB led rainbow fade

const int redPin = 3;  
const int grnPin = 4;  
const int bluPin = 1; 

void fade(int pin, boolean fadeIN, int delayTime)
{
  // fade up (or down) pin
  for(byte i=1; i<100; i++) 
  {
    byte on  = i;
    if (!fadeIN) on = 100 -i; // if fadeOUT than inverse on and off
    byte off = 100-on;
    
    for( byte a=0; a<100; a++ ) 
    {
      digitalWrite(pin, HIGH);
      delayMicroseconds(on);
      digitalWrite(pin, LOW);
      delayMicroseconds(off);
    }
    delay(delayTime);
  } 
  if (fadeIN) 
  {
    digitalWrite(pin, HIGH);
  } else {
    digitalWrite(pin, LOW);
  }
}


void setup()
{
  pinMode(redPin, OUTPUT);     
  pinMode(grnPin, OUTPUT);     
  pinMode(bluPin, OUTPUT);    
  digitalWrite(redPin, LOW);
  digitalWrite(grnPin, LOW);
  digitalWrite(bluPin, LOW);
  fade(redPin,true,15);     // red  
}

void loop()
{
  fade(grnPin,true,15);     // red to yellow
  fade(redPin,false,15);    // yellow to green
  fade(bluPin,true,15);     // green to cyan
  fade(grnPin,false,15);    // cyan to blue
  fade(redPin,true,15);     // red to majenta
  fade(bluPin,false,15);     // majenta to red
}

Thank you for the reply, this code that you wrote does the right thing but it fades very choppy. Is there any way to fix that or is it because of the delay?

Never mind I figured something out that works perfect. Thanks for your help though.
My variation...

// attiny85 RGB led rainbow fade

const int redPin = 3;  
const int grnPin = 4;  
const int bluPin = 1; 

void fade(int pin, boolean fadeIN, int delayTime)
{
  // fade up (or down) pin
  for(byte i=2; i<200; i++) 
  {
    byte on  = i;
    if (!fadeIN) on = 200 -i; // if fadeOUT than inverse on and off
    byte off = 200-on;
    
    for( byte a=0; a<200; a++ ) 
    {
      digitalWrite(pin, HIGH);
      delayMicroseconds(on);
      digitalWrite(pin, LOW);
      delayMicroseconds(off);
    }
    delay(delayTime);
  } 
  if (fadeIN) 
  {
    digitalWrite(pin, HIGH);
  } else {
    digitalWrite(pin, LOW);
  }
}


void setup()
{
  pinMode(redPin, OUTPUT);     
  pinMode(grnPin, OUTPUT);     
  pinMode(bluPin, OUTPUT);    
  digitalWrite(redPin, LOW);
  digitalWrite(grnPin, LOW);
  digitalWrite(bluPin, LOW);
  fade(redPin,true,0);     // red  
}

void loop()
{
  fade(grnPin,true,0);     // red to yellow
  fade(redPin,false,0);    // yellow to green
  fade(bluPin,true,0);     // green to cyan
  fade(grnPin,false,0);    // cyan to blue
  fade(redPin,true,0);     // red to majenta
  fade(bluPin,false,0);     // majenta to red
}