How to control RGB LED color spectrum with a single potentiometer

So far, I've gotten it to fade from red to green, but what I'm trying to do is to fade from red to green to blue.

If anyone can help, you will be greatly appreciated.

int R = 11; 
int G = 10;
int B = 9;
int Pot = A0;
int RVal;
int GVal;
int BVal;


void setup()
{
  pinMode(R, OUTPUT);
  pinMode(G, OUTPUT);  
  pinMode(B, OUTPUT);
  
 
  Serial.begin(9600);
}


void loop()
{

  RVal = analogRead(Pot);
  RVal = map( RVal, 0, 1023,  255,0);
  
  GVal = analogRead(Pot);
  GVal = map( GVal, 0, 1023,  255, 0);
  
  //BVal = analogRead(Pot);
  //BVal = map( BVal, 0, 1023,  255, 0);
  
  Serial.println(RVal);
  
  analogWrite(R, RVal);
  
  analogWrite(G, 255-GVal);
  
  //analogWrite(B, RVal);
 
  
}

Did you expect the pot value to have changed significantly?

It is a lot easier to select colors using the HSV color scheme, than RGB.

See this post, among others: Arduino RGB LED HSV “Color Wheel” – eduardofv

Divide the pot range into four parts:

0->255: Green fades from 0 to 255 while RED stays at 255.
255->512: Red fades from 255 to 0 while Green stays at 255.
512->786: Blue fades from 0 to 255 while Green stays at 255.
768->1023: Green fades from 255 to 0 while Blue stays at 255.

   int potval = analogRead(Pot);

   if (potval < 255)
   {
      analogWrtite(RedPin, 255);
      analogWrite(GreenPin, potval);
      analogWrite(BluePin, 0);
  }
  else if (potval < 512)
   {
      analogWrite(RedPin, 512 - potval);
      analogWrite(GreenPin, 255);
      analogWrite(BluePin, 0);
  }
.
.
.

This will be tricky...

I think you'll have to figure-out what you want to do first... What do you want to see at minimum? In the middle, at maximum? Etc.?

You'll probably need some if-statements to handle different ranges of the pot differently.

With the 10-bit ADC you can only get 1023 different color combinations.

As you may know, 0-255 can is represented by 8-bits. So a single 24-bit variable (which requires a type-long) can represent any possible color & brightness combination.

Then you do bit manipulation to "extract" the groups of 8-bits for the 3 different colors.

With 24-bits you can count to (you might want to double-check that) (1) so you could simply map 0-1023 to 0- 16,777,216. But of course, you're only going to get 1023 different colors with most values being skipped-over.

And with this "simple" approach, you're going to get discontinuities... When a group of bytes counts 255+1, the number rolls-over and 255 suddenly becomes zero.

(1) This becomes easier if you can think and program in hexadecimal. With 8-bits you can count to FF in hex. Each byte is represented by exactly 2 hex digits. With 2-bytes we can count to FFFF and with 24-bits we can count to FFFFFF. With RGB values each group of 2 hex digits represents a color so in hex we can "see" each color without any math.