RGB led code problem

:slight_smile: Hello, I wondered if someone could explain to me what is wrong with the following piece of code. I am using an RGB led controlled by a pot. As I turn the pot, colours change for the values that are set. However, each colour seems to either pulsate rapidly, slowly or vary in brightness. Any help much appreciated :slight_smile:

int potpin = 4;  // analog pin used to connect the potentiometer// left on the original "2" this still works????
int val; 
#define LED 9
#define LED 10
#define LED 11

void setup()
{
  pinMode(LED, OUTPUT);   // sets the pins as output
}

void loop() {
  
  val = analogRead(potpin);	// reads the value of the potentiometer

  {
    if (val < 170) 
    {

        analogWrite(9 , 255);
    }
    else
    {

          analogWrite(9 , 0);
    }
  
  if((val >= 170) && (val < 340))
  {
    
     analogWrite(10 , 255);
  }
  else
  {

     analogWrite(10 , 0);
    
  }
  
   if((val >= 340) && (val < 510))
  {
    
    analogWrite(11 , 255); 
  }
  else
  {

    analogWrite(11 , 0); 
  }
  
   if((val >= 510) && (val < 680))
  {


    analogWrite(9 , 255);
  }
  else
  {

     analogWrite(9 , 0);
  }
  
   if((val >= 680) && (val < 850))
  {


     analogWrite(10 , 255);
  }
  else
  {

     analogWrite(10 , 0);
  }
  
   if((val >= 850) && (val < 1024))
  {


     analogWrite(11 , 255);
  }
  else
  {

     analogWrite(11 , 0);
    
  }
  }
}
#define LED 9
#define LED 10
#define LED 11

  pinMode(LED, OUTPUT);   // sets the pins as output

Set which pin as output? Not all 3 of them.

Perhaps a small delay after reading the potentiometer value would help the flicker.

Well, I have 3 legs of the RGB going into 9,10 & 11 (common cathode?). I've been fiddling with different pieces of code and simply wanted predefined colours to change with the pot values. As I posted this, I through a delay in after each change and there's no flicker, so far so good but I'm going to throw this in with a piece of code that may not allow for a delay after each colour change! hmmmmmm :cold_sweat: If it doesn't work. I'll post it here again! Thanks Paul :slight_smile:

I introduced a delay (delay(100):wink: for each colour change and that smooths out the problem with the colours pulsing. However, because of the delay the colours don't change as quick as the value of the pot changes. Does anyone have a solution for this?? i.e, fast change, no flicker :slight_smile: Thanks

int potpin = 4;  // analog pin used to connect the potentiometer// left on the original "2" this still works????
int val; 
#define LED 9
#define LED 10
#define LED 11

void setup()
{
  pinMode(LED, OUTPUT);   // sets the pins as output
}

void loop() {
  
  val = analogRead(potpin);	// reads the value of the potentiometer

  {
    if (val < 170) 
    {

        analogWrite(9 , 255);
        delay(100);
    }
    else
    {

          analogWrite(9 , 0);
    }
  
  if((val >= 170) && (val < 340))
  {
    
     analogWrite(10 , 255);
     delay(100);
  }
  else
  {

     analogWrite(10 , 0);
    
  }
  
   if((val >= 340) && (val < 510))
  {
    
    analogWrite(11 , 255);
   delay(100); 
  }
  else
  {

    analogWrite(11 , 0); 
  }
  
   if((val >= 510) && (val < 680))
  {


    analogWrite(9 , 255);
    analogWrite(10 , 255);
    delay(100);
  }
  else
  {

     analogWrite(9 , 0);
     analogWrite(10 , 0);
  }
  
   if((val >= 680) && (val < 850))
  {


     analogWrite(9 , 255);
     analogWrite(11 , 255);
     delay(100);
  }
  else
  {

     analogWrite(9 , 0);
     analogWrite(11 , 0);
  }
  
   if((val >= 850) && (val < 1024))
  {


     analogWrite(10 , 255);
     analogWrite(11 , 255);
     delay(100);
  }
  else
  {

     analogWrite(11 , 0);
     analogWrite(10 , 0);
     }
  }
}

I introduced a delay (delay(100)) for each colour change

Why? You only need one per reading?

I cant seem to get the colours to stop flickering without having the delay after each colour. =(
if I only need one delay, where should I put Paul?

if I only need one delay, where should I put Paul?

You shouldn't put me anywhere, especially not down. Put the delay at the end of loop().

:slight_smile: Ha! I see the error of my grammer Paul but not the error of my ways unfortunately :slight_smile:

   if((val >= 850) && (val < 1024))
  {


     analogWrite(10 , 255);
     analogWrite(11 , 255);
     
  }
  else
  {

     analogWrite(11 , 0);
     analogWrite(10 , 0);
  
     }
     delay(100);
  }
}

So i put it at the end of the loop but some of the colours don't work! sorry to be a pain!

:slight_smile: Ok, I took another approach and while I'm sure there are better ways to implement this, it appears to work perfectly. Also there's no need for a delay. Here it is for anyone interested......

int val; 
int redPin = 9;   // Red LED,   connected to digital pin 9
int grnPin = 11;  // Green LED, connected to digital pin 11
int bluPin = 10;  // Blue LED,  connected to digital pin 10
int redVal ;   
int grnVal ;
int bluVal ;

void setup()
{
  pinMode(9, OUTPUT);   // sets the pins as output
  pinMode(10, OUTPUT);   
  pinMode(11, OUTPUT);
}

void loop() {
  
  val = analogRead(4);	// reads the value of the potentiometer

  {
    if (val < 170) 
    {
	redVal = 255;
        grnVal = 0;
        bluVal = 255;
    }
   
  
  if((val >= 170) && (val < 340))
  {
        redVal = 255;
        grnVal = 0;
        bluVal = 102;
   }
 
  
   if((val >= 340) && (val < 510))
   {
        redVal = 255;
        grnVal = 0;
        bluVal = 0;
    }
  
  
   if((val >= 510) && (val < 680))
   {
        redVal = 255;
        grnVal = 51;
        bluVal = 0;
    }
 
  
   if((val >= 680) && (val < 850))
   {
        redVal = 255;
        grnVal = 153;
        bluVal = 0;
    }
  
  
   if((val >= 850) && (val < 1024))
   {
        redVal = 255;
        grnVal = 255;
        bluVal = 0;
    }
 
  analogWrite(redPin, redVal);   // Write values to LED pins
  analogWrite(grnPin, grnVal); 
  analogWrite(bluPin, bluVal); 
    
   }

  }
pinMode(9, OUTPUT);   // sets the pins as output
  pinMode(10, OUTPUT);   
  pinMode(11, OUTPUT);

Since you went to the trouble of giving the pins nice names, maybe you should use them.

I don't see any reason for "redVal" etc to have global scope.

:slight_smile: I see! Thanks for pointing that out :slight_smile: I'll learn one of these days!!!
So for void setup I still have to throw this in it seems?...

void setup()

  {   }

No, you need to throw in a :

void setup()
{
  pinMode(redPin, OUTPUT);
  pinMode(grnPin, OUTPUT);   
  pinMode(bluPin, OUTPUT);
}

How come it works without that?