Pages: [1]   Go Down
Author Topic: [Noob] RGB led blinks when I tried to dim it  (Read 661 times)
0 Members and 1 Guest are viewing this topic.
Argentina
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
 

  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!


* Rgb esquema.png (39.33 KB, 521x498 - viewed 24 times.)
« Last Edit: October 27, 2012, 07:05:28 pm by monti73 » Logged

0
Offline Offline
God Member
*****
Karma: 39
Posts: 988
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Argentina
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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 smiley

Thanks for your time!
Logged

0
Offline Offline
God Member
*****
Karma: 2
Posts: 854
Arduino rocks!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Argentina
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!!
Logged

Pages: [1]   Go Up
Jump to: