Hi everyone, this is my first project (very simple) with Arduino. I don't speak english, i will do my best to explain myself.
My idea is to use a potenciometer to gradually change a RGB Led (4 pins) from pure red, to pure green and to pure blue. I've almost done that but the change is not smooth, one color always starts to blink (on and off) until is fully turned on.
Here is my code and the circuit (attached):
What I've done is divide by 3 the 1023 values of the pot, and in each third I use PWN to graddualy turn off one color and On other color.
int pinR = 6;
int pinG = 5;
int pinB = 3;
void setup() {
pinMode (pinR, OUTPUT); //Pin R
pinMode (pinG, OUTPUT); //Pin G
pinMode (pinB, OUTPUT); //Pin B
}
void loop()
{
int potey = analogRead(A0); //Read the potenciometer
int encender; //variable to gradually turn ON the led
int apagar; //Variable to gradually turn OFF the led
if(potey<=341) // first 1/3 of the potenciometer. (1023/3 = 341)
{
encender=map(potey, 0, 341, 0, 255); // Map the first 341 analog values to 255 PWN values
apagar=map(potey, 341, 0, 0, 255); // The inverse of the upper line, to dim off the Led. I don't if this is right...
analogWrite(pinB, apagar); //Blue led from full On to full Off
analogWrite(pinG, encender); //Green led from full Off to full On
analogWrite(pinR, 0); // No red.
}
else if(potey>=342 && potey<=682)
{
encender=map(potey, 342, 682, 0, 255);
apagar=map(potey, 682, 342, 0, 255);
analogWrite(pinB, 0);
analogWrite(pinG, apagar);
analogWrite(pinR, encender);
}
else
{
encender=map(potey, 683, 1023, 0, 255);
apagar=map(potey, 1023, 683, 0, 255);
analogWrite(pinB, encender);
analogWrite(pinG, 0);
analogWrite(pinR, apagar);
}
}
I' dont know if this is the section to post it this. Let me know if I am wrong.
Thanks in advance!