fade transition from one predefined color to another predefined color. RGB led.

hi guys,
so a little bit of a problem. Im using a couple of potentiometers to send values to processing which would eventually trigger of music outputs. i wont get into details of the project cuz that would take me too long.
Anyway, now while i rotate this potentiometer, i need the colors of this 12V rgb led strip to change with a fade transition. For this particular fade transition, i found a function online and used it, and it works more or less fine, only problem is that when i integrate this code, into my existing serial.write type of code, it slows everything down, because of which the output is at least delayed by 3-4 seconds. i got rid of the color fade function and used the regular analogWrite to switch from one color to the other and everything started working fast again. So im guessing the problem is with the fade function...could you guys please have a look at this and guide me...im attaching the entire arduino code.

proxValue & proxValue2 are essentially the values of the IR proximity sensors im using.

int sensorValue;
  int sensorValueLed;
  int proxValue;
  int proxValue2;
  
  
  int prevR = 0, prevG = 0, prevB = 0; // all of the previous RGB values
  int const Red = 3; //pin 3 
  int const Blue = 6; // pin 4
  int const Green = 5; // pin 5

  
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  pinMode(A0, INPUT);
  pinMode(A4, INPUT);
  pinMode(8, INPUT);
  pinMode(10, INPUT);
  
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  int sensorValueLed = analogRead(A4);
  int proxValue = digitalRead(8);
  int proxValue2 = digitalRead(10);
  
  if(proxValue == 1 && proxValue2 == 0){ //for human side
    if( sensorValue < 250 ){ // for first type of sound
      
      if( sensorValueLed < 250 ){
        Serial.write('1');
        delay(50);
        RGB(0,0,255);
        //analogWrite(Red, 255);
        //analogWrite(Green, 255);
        //analogWrite(Blue, 255);
      }
      
      else if( sensorValueLed > 250 && sensorValueLed < 500 ){
        Serial.write('2');
        delay(50);
        RGB(0,0,255);
        //analogWrite(Red, 255);
        //analogWrite(Green, 255);
        //analogWrite(Blue, 255);
      }
      
      else if( sensorValueLed > 500 && sensorValueLed < 750 ){
        Serial.write('3');
        delay(50);
        RGB(0,0,255);
        //analogWrite(Red, 255);
        //analogWrite(Green, 255);
        //analogWrite(Blue, 255);
      }
      
      else if( sensorValueLed > 750  ){
        Serial.write('4');
        delay(50);
        RGB(0,0,255);
        //analogWrite(Red, 255);
        //analogWrite(Green, 255);
        //analogWrite(Blue, 255);
      }
    }
    else if( sensorValue > 250 && sensorValue < 500 ){ // for second type of sound
      
      if( sensorValueLed < 250 ){
        Serial.write('5');
        delay(50);
        RGB(0,0,255);
        //analogWrite(Red, 255);
        //analogWrite(Green, 255);
        //analogWrite(Blue, 255);
      }
      
      else if( sensorValueLed > 250 && sensorValueLed < 500 ){
        Serial.write('6');
        delay(50);
        RGB(0,0,255);
        //analogWrite(Red, 255);
        //analogWrite(Green, 255);
        //analogWrite(Blue, 255);
      }
      
      else if( sensorValueLed > 500 && sensorValueLed < 750 ){
        Serial.write('7');
        delay(50);
        RGB(0,0,255);
        //analogWrite(Red, 255);
        //analogWrite(Green, 255);
        //analogWrite(Blue, 255);
      }
      
      else if( sensorValueLed > 750  ){
        Serial.write('8');
        delay(50);
        RGB(0,0,255);
        //analogWrite(Red, 255);
        //analogWrite(Green, 255);
        //analogWrite(Blue, 255);
      }
    }else if( sensorValue > 500 && sensorValue < 750 ){ // for third type of sound
      
      if( sensorValueLed < 250 ){
        Serial.write('a');
        delay(50);
        RGB(0,0,255);
        //analogWrite(Red, 255);
        //analogWrite(Green, 255);
        //analogWrite(Blue, 255);
      }
      
      else if( sensorValueLed > 250 && sensorValueLed < 500 ){
        Serial.write('b');
        delay(50);
        RGB(0,0,255);
        //analogWrite(Red, 255);
        //analogWrite(Green, 255);
        //analogWrite(Blue, 255);
      }
      
      else if( sensorValueLed > 500 && sensorValueLed < 750 ){
        Serial.write('c');
        delay(50);
        RGB(0,0,255);
        //analogWrite(Red, 255);
        //analogWrite(Green, 255);
        //analogWrite(Blue, 255);
      }
      
      else if( sensorValueLed > 750  ){
        Serial.write('d');
        delay(50);
        RGB(0,0,255);
        //analogWrite(Red, 255);
        //analogWrite(Green, 255);
        //analogWrite(Blue, 255);
      }
    }else if( sensorValue > 750 ){ // for fourth type of sound
      
      if( sensorValueLed < 250 ){
        Serial.write('e');
        delay(50);
        RGB(0,0,255);
        //analogWrite(Red, 255);
        //analogWrite(Green, 255);
        //analogWrite(Blue, 255);
      }
      
      else if( sensorValueLed > 250 && sensorValueLed < 500 ){
        Serial.write('f');
        delay(50);
        RGB(0,0,255);
        //analogWrite(Red, 255);
        //analogWrite(Green, 255);
        //analogWrite(Blue, 255);
      }
      
      else if( sensorValueLed > 500 && sensorValueLed < 750 ){
        Serial.write('g');
        delay(50);
        RGB(0,0,255);
        //analogWrite(Red, 255);
        //analogWrite(Green, 255);
        //analogWrite(Blue, 255);
      }
      
      else if( sensorValueLed > 750  ){
        Serial.write('h');
        delay(50);
        RGB(0,0,255);
        //analogWrite(Red, 255);
        //analogWrite(Green, 255);
        //analogWrite(Blue, 255);
      }
    }
  }else if(proxValue == 0 && proxValue2 == 1){
    
    Serial.write('i');
  
  }
  else if(proxValue == 0 && proxValue2 == 0){
    
    Serial.write('0');
        RGB(0,0,255);
        //analogWrite(Red, 255);
        //analogWrite(Green, 255);
        //analogWrite(Blue, 255);
  
  }
}

void RGB(int R, int G, int B) {
for (int i = 0; i <= 255; i++)
{
if (i >=  prevR - R && prevR < R) {
 
  analogWrite(Red, prevR + i);
}
if (i >= prevG - G && prevG < G) {
 
  analogWrite(Green, prevG + i);
 
}
if (i >= prevB - B && prevB < B) {
 
  analogWrite(Blue, prevB + i);
 
}
//delay(10);
//}
//for (int i = 0; i <= 255; i++)
//{
if (i >= R - prevR && prevR > R) {
 
  analogWrite(Red, prevR - i);
}
if (i >= G - prevG && prevG > G) {
 
  analogWrite(Green, prevG - i);
 
}
if (i >= B - prevB && prevB > B) {
 
  analogWrite(Blue, prevB - i);
 
}
delay(10);
}
delay(10);
analogWrite(Red, R);
analogWrite(Green, G);
analogWrite(Blue, B);
prevR = R;
prevG = G;
prevB = B;
}

Why all the delays? Get rid of them.

Look at the blink without delay example and write your code like that. A sort of fade without delay if you will. This involves using what is know as a state machine, there links will help you:-

My page

Robin2's page

thanks mike.. il have a look and get back!

Hi,

I suspect your problem is that code was designed to fade between colours at its own pace, hence all the delay() calls.

I'm thinking all you need is code that simply follows your pot, at whatever angle, rate or direction you turn it.

That code would also be a tiny fraction of the size of the code you found & posted!

I'm not near my pc at the moment, but if you would like to see what I mean I can give you something to get you started later, if you like.

Paul

Hey mike, so I removed all the delays but the serial.write is still too slow. I checked the blink without delay examples but I'm not sure if it would help me much cuz I don't really need delays in my code. I jus wrote the 50 ms delay because somewhere I read that it helps to make the code more stable when you are writing values to processing...again..I just follow stuff I find online. I don't really have a programming background. When I remove the rgb function at the end of the code, then my serial write becomes swift again..does that mean that the function Is not written properly or I haven't integrated the function into the code in the right way?

The RGB function looke like it is called in the right way but what is it doing exactly? It is that function that is causing the delay with the looping 255 times most of which is not necessary. You also seem to be fading the colours one at a time not all together. You need to rethink this function.
The "distance" between the current colour value and the required one should be calculated for all three colours then the largest is found, this is the number of steps you need to take with an increment of one. The other two colours will then have fractional increments.

Also increase the baud rate.