RGB LED code change

How do i change this code so instead of;
red, yellow, green, blue to red,
It will start with the colour yellow and then fade in and out of the remaining colours and finish at red?

/*
 * Program: rgbled.ino
 * Author: Julian White
 * Created: 10/13/2012
 * Description: Use single 10K potentiometer to cycle through all
 * hues in the color wheel. The RGB LED is wired as common anode
 * (+5V). 330 ohm resistor wired in series with LED anode.
 */
 
int redPin = 9;
int greenPin = 11;
int bluePin = 10;
int potPin = A0;
int val = 0;

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

    Serial.begin(9600);
}

void loop()
{
    //Read the potentiometer (color wheel).
    val = analogRead(potPin);
    Serial.println(val);
    
    //Red-orange-yellow-green spectrum
    if ( val <= 341 )
    {
        //We now know the val is between 0 and 341.
        //Map the val to 0-255 for the LED.
        val = map(val, 0, 341, 0, 255);
        
        //Higher val turns red down.
        //Lower val turns red up (common anode).
        analogWrite(redPin, val);
        
        //Write the complimentary amount to the green pin.
        //The mixed color values add up to 255.
        //This keeps the LED at constant brightness.
        analogWrite(greenPin, 255-val);
        
        //Turn blue pin off.
        analogWrite(bluePin, 255);
    }
    
    //Green-teal-blue spectrum
    else if ( val <= 683 )
    {
        //We now know the val is between 341 and 683.
        //Map the val to 0-255 for the LED.
        val = map(val, 341, 683, 0, 255);
        
        //Higher val turns green down.
        //Lower val turns green up (common anode).
        analogWrite(greenPin, val);
        
        //Write the complimentary amount to the blue pin.
        //The mixed color values add up to 255.
        //This keeps the LED at constant brightness.
        analogWrite(bluePin, 255-val);
        
        //Turn red pin off.
        analogWrite(redPin, 255);
    }
    
    //Blue-indigo-purple-red spectrum.
    else
    {
        //We now know the val is between 683 and 1023.
        //Map the val to 0-255 for the LED.
        val = map(val, 683, 1023, 0, 255);
        
        //Higher val turns blue down.
        //Lower val turns blue up (common anode).
        analogWrite(bluePin, val);

        //Write the complimentary amount to the red pin.
        //The mixed color values add up to 255.
        //This keeps the LED at constant brightness.
        analogWrite(redPin, 255-val);

        //Turn green pin off.
        analogWrite(greenPin, 255);
    }
    
    //Sampling rate.
    delay(20);
}/code]

Hi, so you want:

Yellow, green, blue, red? i.e. missing out orange?

Try changing

val = map(val, 0, 341, 0, 255);

to

val = map(val, 0, 341, 128, 255);

Paul

Hi Paul,
Basically yes.
Instead of red to start, and go through the colours to end at red. I wish it to start at yellow, then go through the colours and end at red.
If that makes sense.

Just spotted an error in one of the comments in that sketch

330 ohm resistor wired in series with LED anode.

This is not the correct way to wire up a common anode rgb led. You should really have three resistors, one on each cathode, and no resietor on the anode. The resistor on the red cathode needs to be a little higher than those on the blue and green cathodes. Otherwise all the colour will probably always be heavy on the red side. This is because the red led inside the rgb led will have a lower forward voltage than the green or blue leds.

Anyway, did my suggestion work?