Pages: [1]   Go Down
Author Topic: Flowing RGB LED's  (Read 928 times)
0 Members and 1 Guest are viewing this topic.
Michigan
Offline Offline
Newbie
*
Karma: 1
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all.

After my most recent code writing session I thought I'd share it as it might come in handy to someone hopefully. As I'm a newby to this, if anyone has any suggestions on how to shorten, clean up, simplify, etc. the code, that would be appreciated.

https://www.facebook.com/photo.php?v=10100299380466341&l=8667494813163255863

What I wanted to do was fade a large amount of RGB LED's in a controlled fashion (brightness and duration from color to color). I'm not worried about using this as display signage or anything like that (no need to control rows, columns, make text, etc).

Using three PWM channels on the Arduino Uno I'm running three banks of RGB LED's in a 'flowing' fashion. My original plan was to use NPN transistors for control of each leg but 12 LED's at max draw are around a 50 milliamp draw. So I will likely just wire them straight to the output channel for the final revision. I don't have the resistors in the Fritzing drawing but I'm using 200 ohm resistors to each leg of each LED. There are three different color wires coming out of the Arduino, but they don't represent the LED color. Just a way to stay organized.

The basic idea is that each output from the Arduino will rise and fall in it's intensity. One potentiometer is used to control the max brightness and another to modify a delay to slow down the loop. Because the brightness is controlled by limiting the max PWM duty cycle, at minimum brightness all the values are the same for a short time so a separate value is used and then mapped against to ensure the cycle is maintained even if the max brightness goes down then comes back up again.

Thanks all.

Code:

// Integer declaration
int LoopCount = 0;
int sensorValueA0;
int sensorValueA1;

int Led3 = 3;
int Led9 = 9;
int Led10 = 10;

int brightness3 = 21;
int brightness9 = 137;
int brightness10 = 254;

int fadeAmount3 = 1;
int fadeAmount9 = 1;
int fadeAmount10 = 1;

int MaxBrightness3;
int MaxBrightness9;
int MaxBrightness10;

// Program Setup. Runs once
void setup() {}

// Looping Routine. Runs forever
void loop() {
 
  // Checks for first Loop
  if (LoopCount == 0) sensorValueA0 = analogRead(A0);
  sensorValueA0 = map(sensorValueA0, 0, 1023, 0, 100);
 
  // Checks for second loop
  // Additional loops between reads for better performance
  if (LoopCount == 4) sensorValueA1 = map(analogRead(A1), 0, 1023, 21, 254);
 
  // Increases loop counter
  LoopCount = LoopCount + 1;
 
  // Resets loop counter
  if (LoopCount == 8) LoopCount = 0;

  brightness3 = brightness3 + fadeAmount3;
  brightness9 = brightness9 + fadeAmount9;
  brightness10 = brightness10 + fadeAmount10;

  if (brightness3 == 20 || brightness3 == 255)
   fadeAmount3 = -fadeAmount3 ;
   
  if (brightness9 == 20 || brightness9 == 255)
   fadeAmount9 = -fadeAmount9 ;
     
  if (brightness10 == 20 || brightness10 == 255)
   fadeAmount10 = -fadeAmount10 ;

  MaxBrightness3 = map(brightness3, 20, 255, 20, sensorValueA1);
 
  MaxBrightness9 = map(brightness9, 20, 255, 20, sensorValueA1);
 
  MaxBrightness10 = map(brightness10, 20, 255, 20, sensorValueA1);

  analogWrite(Led3, MaxBrightness3);

  analogWrite(Led9, MaxBrightness9);

  analogWrite(Led10, MaxBrightness10);

  delay(sensorValueA0);

}



* LEDs.jpg (254.33 KB, 1005x650 - viewed 35 times.)
Logged

Valencia, Spain
Offline Offline
Faraday Member
**
Karma: 144
Posts: 5358
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

My original plan was to use NPN transistors for control of each leg but 12 LED's at max draw are around a 50 milliamp draw.

Use the transistors. 50mA is too much for an Arduino pin and could damage it.

(the datasheet says damage starts to happen at 40mA)

Logged

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

Offline Offline
Newbie
*
Karma: 4
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Paul,

You asked for suggestions to simplify your code.  I'm not sure why you implinted the following "LoopCount"ing code at the top of your loop():


Code:
loop(){
// Checks for first Loop
  if (LoopCount == 0) sensorValueA0 = analogRead(A0);
  sensorValueA0 = map(sensorValueA0, 0, 1023, 0, 100);
 
  // Checks for second loop
  // Additional loops between reads for better performance
  if (LoopCount == 4) sensorValueA1 = map(analogRead(A1), 0, 1023, 21, 254);
 
  // Increases loop counter
  LoopCount = LoopCount + 1;
 
  // Resets loop counter
  if (LoopCount == 8) LoopCount = 0;


I suggest you simplify things a abit like so:

Code:
loop()
{
sensorValueA0 = map(analogRead(A0), 0, 1023, 0, 100);
sensorValueA1 = map(analogRead(A1), 0, 1023, 21, 254);


If you simplify your code this way you might notice a bit less of a lag from when you read analogRead(A1) to when you update the LEDs based on its value. 
Logged

Offline Offline
Newbie
*
Karma: 4
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think fungus was on to something here...

When you state "12 LED's at max draw are around a 50 milliamp draw. So I will likely just wire them straight to the output channel for the final revision."

I really cant tell how you've wired this up in your video, its probably a good idea to update your Fritzing drawing to show how the 12 LEDs are connected - 3 Banks x 4 LEDs in parallel?  You say you have resistors on each LED leg, is that in addition to ones you put on the Common Legs?



« Last Edit: March 05, 2013, 05:14:34 pm by Skorn » Logged

Michigan
Offline Offline
Newbie
*
Karma: 1
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

For whatever reason I was thinking the Uno did 200ma per pin. That woulda been bad.  smiley-mr-green

I will update the drawing to reality and add the proper resistors as well.

I did the LoopCount to make the program run faster. The serial read and printin I had at one point could have been causing the slow down though. When I adjust the pots there is no perceptible lag.

Logged

Pages: [1]   Go Up
Jump to: