int r = 11; // red pin no.

int g = 10; // green pin no.
int b = 9; // blue pin no.


#define delayTime 20

int rVal = 0; // initial red value
int gVal = 0; // initial green value
int bVal = 0; // initial blue value

int i =0;



void setup() {
 
  analogWrite(r,255);
  pinMode(r,OUTPUT); // initialize red pin as output pin
  pinMode(g,OUTPUT); // initialize green pin as output pin
  pinMode(b,OUTPUT); // initialize blue pin as output pin

 
 Serial.begin(9600); // begin serial communication at 9600 Rate

}


void loop() 
 
 {

 if (Serial.available() > 0)  // if data is available for processing

 {
   

int data = Serial.read() - 48; 
 
 
 if ( data == 1) {
   
   
   analogWrite(r,255);
   analogWrite(g,0);
   analogWrite(b,0);
   
 }
 
 else if (  data == 2)

 
 {
   analogWrite(r,0);
   analogWrite(g,255);
   analogWrite(b,0);
 }
   
   
   
  else if (  data == 3)

 
 {
   analogWrite(r,0);
   analogWrite(g,0);
   analogWrite(b,255);
 }
   
    else if (  data == 4)

 
 {
   analogWrite(r,255);
   analogWrite(g,255);
   analogWrite(b,255);
 }
   
   else if ( data== 0) {
     
      analogWrite(r,0);
   analogWrite(g,0);
   analogWrite(b,0);
   
  
   
   }
   // 5 for yellow color
   
 else if (data ==5) {
   
   analogWrite(r,255);
   analogWrite(g,255);
   analogWrite(b,0);
   
  
   
    
 }
 
  else if (data ==6) {
   
   analogWrite(r,255);
   analogWrite(g,0);
   analogWrite(b,255);
   
   
  
   
    
 }
 
 
 // 7 for any color given its rgb value
     else if (data ==7) {
       
      
       
       
      int data = Serial.read();
       
          analogWrite(r,data);
   analogWrite(g,0);
   analogWrite(b,0);
       
       
     }
     
     else if ( data == 9 ) 
{
  
  int r1  = Serial.parseInt();
  int g1 =Serial.parseInt();
  int b1 =Serial.parseInt();
  
  
  
  
  analogWrite(r,r1);
  
    analogWrite(g,g1);
   analogWrite(b,b1);
  
}


else if ( data == 8) {
  
  fade();
  
  
  
  
}
   
 }
 

   
  
}

int  fade ()

{
 
  int redVal = 255;
  int blueVal = 0;
  int greenVal = 0;
  for( int i = 0 ; i < 255 ; i += 1 ){
    
    if ( Serial.available()  > 0 ) {
      
      return(0);
      
    }
    greenVal += 1;
    redVal -= 1;
    analogWrite( g,  greenVal );
    analogWrite( r,  redVal );

    delay( delayTime );
  }
 
  redVal = 0;
  blueVal = 0;
  greenVal = 255;
  for( int i = 0 ; i < 255 ; i += 1 ){
    
     if (Serial.available()  > 0 ) {
      
      return(0);
      
    }
    blueVal += 1;
    greenVal -= 1;
    analogWrite( b,  blueVal );
    analogWrite( g,  greenVal );

    delay( delayTime );
  }
 
  redVal = 0;
  blueVal = 255;
  greenVal = 0;
  for( int i = 0 ; i < 255 ; i += 1 ){
    
     if (Serial.available()  > 0 ) {
      
      return(0);
      
    }
    redVal += 1;
    blueVal -= 1;
    analogWrite( r,  redVal );
    analogWrite(b,  blueVal );

    delay( delayTime );
  }
}



