Go Down

Topic: [Noob] RGB led blinks when I tried to dim it (Read 749 times) previous topic - next topic

monti73

Oct 28, 2012, 01:57 am Last Edit: Oct 28, 2012, 02:05 am by monti73 Reason: 1
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.
Code: [Select]
 

 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!

billroy

Code looks good.  I don't see a problem with it.

Consider printing out potey to see if it's getting a wild value sometimes.

The light output behavior of the LED may be more predictable if there is an appropriate resistor on each leg instead of just the cathode.

What happens on the LED if you sweep the value of potey from 0 to 1023 in a for loop, instead of reading from the pot?

If it's a noisy pot reading somehow glitching the LED, you might try reading the pot until you get the same value twice in a row.


-br

dhenry

Quote
Thanks in advance!


Because of different forward voltage drops for those diodes (red leds tend to have the lowest forward drop), your set-up will not allow you to turn on blue / green leds when the red led is on.

You should consider using 3 separate resistors for those leds if you intend to turn some of them on at the same time.

monti73



Because of different forward voltage drops for those diodes (red leds tend to have the lowest forward drop), your set-up will not allow you to turn on blue / green leds when the red led is on.

You should consider using 3 separate resistors for those leds if you intend to turn some of them on at the same time.



Thanks for the answers. I'm using only one led (RGB with for pins) and it have one cathode, thats why there is only one resistor. The blinking appears only with the Green and Blue led as you say, so that could be the problem!. I also used the For instead of the pot and the blinking stays there. This was just a practice for fun, now that I'm know that the problem is not the code but the LED, I could go on with other projects :)

Thanks for your time!

Oracle

Quote
Thanks for the answers. I'm using only one led (RGB with for pins) and it have one cathode, thats why there is only one resistor.


Current is constant in a series circuit, so it doesn't matter where the resistor is as long as it's within the same series loop.   Put the 3 resistors between the LED anodes and the arduino pins.

Good luck with your project.

monti73


Quote
Thanks for the answers. I'm using only one led (RGB with for pins) and it have one cathode, thats why there is only one resistor.


Current is constant in a series circuit, so it doesn't matter where the resistor is as long as it's within the same series loop.   Put the 3 resistors between the LED anodes and the arduino pins.

Good luck with your project.


I can not believe how I did not think of that  :smiley-mr-green: ahah. I'm going to try it. Thanx!!

Go Up