Controlling a RGB LED With a Potantiometer

Hi everyone, I’m trying to control a RGB LED with a Potantiometer. I have written a code but It is not working well. Please, Can you help me?

byte led_r = 2;
byte led_g = 3;
byte led_b = 4;
int pot = A0;

int pot_value = 0;



void setup()
{
  Serial.begin(9600);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
}

void loop()
{
pot_value = analogRead(pot);
Serial.println(pot_value);
if(pot_value < 300)
digitalWrite(led_r, HIGH);
else
digitalWrite(led_r, LOW);

if(pot_value < 600)
digitalWrite(led_g, HIGH);
else
digitalWrite(led_g, LOW);

if(600 < pot_value < 900)
digitalWrite(led_b, HIGH);
else 
digitalWrite(led_b, LOW);

if(pot_value > 900){
digitalWrite(led_r, HIGH);
digitalWrite(led_g, HIGH);
digitalWrite(led_b, HIGH);
}
else
{
digitalWrite(led_r,LOW);
digitalWrite(led_g, LOW);
digitalWrite(led_b, LOW);
}

}

It is not working well.

What does it do ?
What should it do ?

Oh, I've noticed a problem in the code. I will fix it and come back for your and my question. Thank you

if(600 < pot_value < 900)

Was that the problem?

If not, it needs to be:

if( pot_value > 600 && pot_value < 900)

The 2 comparisons need to be separate.

@groundFungus I didn’t know that, thank you. Here is the final code and it still doesn’t work. It must do; If the pot’s value under 300, red led will turn on. If the pot’s value between 300-600, green led will turn on. If the pot’s value between 600-900 , blue led will turn on. And If the pot’s value higher than 900, all leds will turn on.

And the problem; The leds don’t work. Just the last code part is working(all led on) and sometimes the blue led winks.

I checked my circuit. It looks good. And I have a pull-down resistor for pot.

byte led_r = 2;
byte led_g = 3;
byte led_b = 4;
int pot = A0;

int pot_value = 0;



void setup()
{
  Serial.begin(9600);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
}

void loop()
{
pot_value = analogRead(pot);
Serial.println(pot_value);

if(pot_value < 300)
digitalWrite(led_r, HIGH);
else
digitalWrite(led_r, LOW);

if( pot_value > 300 && pot_value < 600)
digitalWrite(led_g, HIGH);
else
digitalWrite(led_g, LOW);

if( pot_value > 600 && pot_value < 900)
digitalWrite(led_b, HIGH);
else 
digitalWrite(led_b, LOW);

if(pot_value > 900){
digitalWrite(led_r, HIGH);
digitalWrite(led_g, HIGH);
digitalWrite(led_b, HIGH);
}
else
{
digitalWrite(led_r, LOW);
digitalWrite(led_g, LOW);
digitalWrite(led_b, LOW);
}

}

What do you see when you print pot_value ?

I have a pull-down resistor for pot.

I would be interested in seeing a circuit diagram

The final else overrides all the work you did lighting the appropriate leds earlier in the sketch. e.g. if the pot reads 200, you light the red light, but then immediately turn all leds off.

Stick a delay(2000) before the final if and you'll see what's happening.

The logic using the else was messing you up. Here is a way to do what you want.

const byte led_r = 2;  // made the data type const byte to save some SRAM
const byte led_g = 3;
const byte led_b = 4;
const byte pot = A0;

int pot_value = 0;



void setup()
{
   Serial.begin(9600);
   pinMode(led_r, OUTPUT);  // replaced the pin numbers with their names
   pinMode(led_g, OUTPUT);
   pinMode(led_b, OUTPUT);
}

void loop()
{
   pot_value = analogRead(pot);
   Serial.println(pot_value);

   if (pot_value < 300)
   {
      digitalWrite(led_r, HIGH);
      digitalWrite(led_g, LOW);
      digitalWrite(led_b, LOW);
   }
   if ( pot_value > 301 && pot_value < 600)
   {
      digitalWrite(led_r, LOW);
      digitalWrite(led_g, HIGH);
      digitalWrite(led_b, LOW);
   }

   if ( pot_value > 601 && pot_value < 900)
   {
      digitalWrite(led_r, LOW);
      digitalWrite(led_g, LOW);
      digitalWrite(led_b, HIGH);
   }

   if (pot_value > 901)
   {
      digitalWrite(led_r, HIGH);
      digitalWrite(led_g, HIGH);
      digitalWrite(led_b, HIGH);
   }
}

And I have a pull-down resistor for pot.

Can you show what that means. You, usually, do not use a pull down (or up) for a pot. One end of the pot to Vcc the other end to ground and the wiper to an analog input. No other resistors required.

I’m thankful. I fixed it. @wilbill is right. The problem is the last code part. I removed the else and I added LOW parts to other conditions.

byte led_r = 2;
byte led_g = 3;
byte led_b = 4;
int pot = A0;

int pot_value = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
}

void loop()
{
pot_value = analogRead(pot);
Serial.println(pot_value);

if(pot_value < 300)
{
digitalWrite(led_r, HIGH);
digitalWrite(led_b, LOW);
digitalWrite(led_g, LOW);
}
else
digitalWrite(led_r, LOW);

if( pot_value > 300 && pot_value < 600)
{
digitalWrite(led_g, HIGH);
digitalWrite(led_r, LOW);
digitalWrite(led_b, LOW);
}
else
digitalWrite(led_g, LOW);


if( pot_value > 600 && pot_value < 900)
{
digitalWrite(led_b, HIGH);
digitalWrite(led_r, LOW);
digitalWrite(led_g, LOW);
}
else 
digitalWrite(led_b, LOW);


if(pot_value > 900)
{
digitalWrite(led_r, HIGH);
digitalWrite(led_g, HIGH);
digitalWrite(led_b, HIGH);
}

}

for @groundFungus : If I remove the pot, What will happen? A pull-down resistor for a input pin is a good thing. It blocks noise.