Arduino Examples #1 Make An RGB Led Randomly Flash Different Colors

 by qazwsx755
rgb led.jpg
In this instructable I will show you how to get an rgb led to randomly flash different colors using an Arduino. If you have any questions, comments or suggestions for other Arduino examples please feel free to leave a comment and I will write back as soon as i can.
 
Remove these adsRemove these ads by Signing Up

Step 1: Get The Hardware Required

rgb led.jpg
Hardware Required
1-arduino
1-330ohm resistor
1-common cathode or common anode rgb led
1-breadboard
5-jumper wires
03coupr says: Apr 17, 2012. 3:56 AM
Hi, great tutorial. Just a quick question could I get this to work with an FSR (Force sensitive resistor) changing the colours to the amount of pressure is given.

Many thanks
lasko25 in reply to 03couprOct 21, 2012. 10:51 PM
Yes, you can definitely do this.
Just think of a way to map the values from the pressure sensor on color.
Example > red is 0-100, green is 100-200, blue is 200-300.
That would be static switches, if you want more dynamic color smoothing then it will be a bit trickier, but absolutely doable.
geoffb37 says: Oct 19, 2012. 10:50 AM

sketch_oct19a.cpp: In function 'void loop()':
sketch_oct19a:42: error: expected `)' before 'if'
sketch_oct19a:43: error: expected `)' before 'if'
sketch_oct19a:44: error: expected `)' before 'analogWrite'


maewert, your code verifys these errors???
can you assist?
learning curve says: Apr 11, 2012. 12:28 AM
Hi there, i was hoping to add a potentiometer to this sketch, i am really new to writing code so please bare with me :) i have used potentiometers before but for controlling servo motors and not LED's, any help would be greatly appreciated
maewert says: Dec 2, 2010. 6:43 AM
Nice instructable. 
A quick review of the code seems to show there there are no 'break' statements between the 'case's.  So once the random number picks one case all of the following case statements would be executed as well.  Wouldn't this repeat the same random sequence (i.e. after the yellow is shown it is always followed by cyan, megenta, then white)? 
(Also note that random(6) returns numbers from 0 to 5 but does not return 6 itself.)
This might still look nice but an even more colorful more random display may result if you drove each color using a PWM pin and for each you set to random(255) values like so:


////////////////////////////////////////////////////////////////////////////////////////////
int a = 1000; //this sets how long the LED stays one color for
int red = 11; //this sets the red led pin
int green = 10; //this sets the green led pin
int blue = 9; //this sets the blue led pin

void setup()
{ //this sets the output pins
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(blue, OUTPUT);
}

void loop()
{
  analogWrite(blue, random(255);
  analogWrite(red, random(255);
  analogWrite(green, random(255);
  delay(a);
}
////////////////////////////////////////////////////////////////////////////////////////////

(I didn't compile this so it may be buggy)

Just a thought. 

Best Wishes.
qazwsx755 (author) in reply to maewertDec 2, 2010. 2:33 PM
I just modified this Instructable by adding the break statements and changed that number 6 to 7 in the code even though it worked fine and didn't repeat the same sequence before without the break statements.Thanks for the advice.
maewert in reply to qazwsx755Dec 4, 2010. 4:18 PM
Cool.

You can aso give this one a try.  It slowly fades betweens random colors:

int red = 11; //this sets the red led pin
int green = 10; //this sets the green led pin
int blue = 9; //this sets the blue led pin

int redNow;
int blueNow;
int greenNow;
int redNew;
int blueNew;
int greenNew;

void setup()
{ //this sets the output pins
 pinMode(red, OUTPUT);
 pinMode(green, OUTPUT);
 pinMode(blue, OUTPUT);
 redNow = random(255);
 blueNow = random(255);
 greenNow = random(255);
 redNew = redNow;
 blueNew = blueNow;
 greenNew = greenNow;
}

#define fade(x,y) if (x>y) x--; else if (x<y) x++;

void loop()
{
 analogWrite(blue, blueNow);
 analogWrite(red, redNow);
 analogWrite(green, greenNow);
 redNew = random(255);
 blueNew = random(255);
 greenNew = random(255);
// fade to new colors
 while ((redNow != redNew) ||
  (blueNow != blueNew) ||
  (greenNow != greenNew))
 {
  fade(redNow,redNew)
  fade(blueNow,blueNew)
  fade(greenNow,greenNew)
  analogWrite(blue, blueNow);
  analogWrite(red, redNow);
  analogWrite(green, greenNow);
  delay(20);
 }
}
 
Djandco in reply to maewertNov 2, 2011. 3:20 PM
Hi,
I am very new to the Uno, and to programming for that matter!
Could I ask nicely how you could incorporate a flickering LED to Pin 8?
I don't know how to run two routines without causing one to stop :-(
I can read up on things but its a pity there are no local night classes for absolute beginners!
maewert in reply to DjandcoNov 2, 2011. 7:00 PM
Djandco,

Often we can combine two sample programs by adding what each one has in the Setup and the loop. This only works if the two routines do not 'step on' each other, which I mean that each routine uses different pins and variables, etc.

I don't have any code that implements 'flickering' but I'll try to make something up :-). Two ways come to mind, we can use random numbers to set the brightness and the duration of the LED if we use Pulse Width Modulation like we did for setting the colors of the RGB LED. Second way would be to not use PWM but to simply use random numbers to set the duration of on and off for the LED. Pin 8 is one of the Arduino pins that does NOT support PWM. We'd have to use pin 5 or 6 if we wanted to use PWM. Lets try it with pin 8 and not use PWM.

Give this a shot:!

int red = 11; //this sets the red led pin
int green = 10; //this sets the green led pin
int blue = 9; //this sets the blue led pin
int flickerLED = 8; // flicker on pin 8

int redNow;
int blueNow;
int greenNow;
int redNew;
int blueNew;
int greenNew;
int flickertime;

void setup()
{ //this sets the output pins
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(flickerLED,OUTPUT);
redNow = random(255);
blueNow = random(255);
greenNow = random(255);
redNew = redNow;
blueNew = blueNow;
greenNew = greenNow;
}

#define fade(x,y) if (x>y) x--; else if (x
void loop()
{
analogWrite(blue, blueNow);
analogWrite(red, redNow);
analogWrite(green, greenNow);
redNew = random(255);
blueNew = random(255);
greenNew = random(255);
// fade to new colors
while ((redNow != redNew) ||
(blueNow != blueNew) ||
(greenNow != greenNew))
{
fade(redNow,redNew)
fade(blueNow,blueNew)
fade(greenNow,greenNew)
analogWrite(blue, blueNow);
analogWrite(red, redNow);
analogWrite(green, greenNow);

flickertime = random(20);
digitalWrite(flickerLED,HIGH);
delay(flickertime);
digitalWrite(flickerLED,LOW);
delay(20-flickertime);
}
}
Djandco in reply to maewertNov 4, 2011. 3:24 PM
Although I have just looked at your code again and those lines are not in it?

When you copy text from the forum and paste it you don't seem to get what you think you were going to get!

So, I must appologise because it was not your code that had the fault it was me.

Thanks
Djandco in reply to maewertNov 4, 2011. 3:18 PM
AH!
No idea why this worked but it did :-)
I just chuffed off the following

redNew = redNow;
blueNew = blueNow;
greenNew = greenNow;

form the Void Loop as it was already in the Void Setup.
Now all I have to do is understand why it worked!

This is a whole new world :-0

And I have found the door, all I need to do now is go through the door and see how far down the rabbit hole goes.........................................................
Djandco in reply to maewertNov 4, 2011. 1:15 PM
Thanks meawert,

This is where I come unstuck :-(
The code generates the following:

Maewert.cpp: In function 'void loop()':
Maewert:42: error: expected `)' before 'if'
Maewert:43: error: expected `)' before 'if'
Maewert:44: error: expected `)' before 'analogWrite'
Maewert:53: error: expected `}' at end of input

But I have no idea how to fix it.

Just so I can look things up on the net, what is this code exactly? C? C+? or is it specific like Python?

I honestly do apreciate your time trying to show me the way!

Thanks
AzurusNova in reply to maewertJan 23, 2011. 12:29 PM
Not a bad little sketch there, now I have to figure out how to build a mosfet setup to bridge the PWM into a 12V system so I can control my room lights now. :P
maewert in reply to AzurusNovaJan 24, 2011. 6:59 AM
Have you checked out the other instructibles like this one:
http://www.instructables.com/id/RGB-Color-Controllable-High-Power-LED-Room-%2b-Spot-/step6/circuit-operation/

I have not worked with high power LEDs but I recently purchased 60 RGB LEDs and I have lots of superbright LEDs left over from other projects.  I'm thinking about doing something like this as well only using a number of individual color LEDs.  Maybe something that would shine behind my flat panel monitors on the rather stark white walls of my office would be nice.  Six LEDs of each color would be 18 LEDs, each taking about 20mA will still be less than 1/2 amp, easily handled with three 2N2222A transistors.  Could easily double the number of LEDs. 

Might be nice to add a bit of color :-)
Yakumo in reply to maewertJan 1, 2011. 1:20 PM
thanks maewert thats what i was looking for ^^
Hitman227 says: Dec 6, 2010. 4:25 AM
Nice 'ible.
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!