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
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);
}
}
}
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 If it doesn't work. I'll post it here again! Thanks Paul
I introduced a delay (delay(100) 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 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);
}
}
}
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);
}
}