Pages: [1] 2 3 4   Go Down
Author Topic: Help on random led coloring?  (Read 4336 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 34
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am new to arduino and was wondering how to make an led flash one random color for a project I am doing. I have looked at the page on the arduino site and other ones too but when I set it to show one color, at random and stay on that color, it always shows the same color, no matter what, making it not very random. Please help! [smiley=cheesy.gif]
Logged

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 318
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

A single LED will only ever emit one color of light (within a very small window of variation.) If you want multiple colors, you need a so-called multicolor LED, which are really just multiple LEDs in the same package - you basically need to power (and/or control) each LED separately.

These multicolor LEDs are available in lots of two- and three-color combinations, i.e. red/green, and red/green/blue. With any multi-color LED, you can get a whole range of colors between the various endpoints by fading each color appropriately. For instance, if you have a red/blue LED, you can get purple light by powering both equally - or, purplish-red light by powering the red at full power, and the blue at a smaller current (or PWM duty cycle).

So, to give you the short answer, if you want lots of colors, go get an RGB LED, and feed each color with it's own PWM signal, from one of the PWM pins on your arduino. Then, apply the logic you've already seen in the examples you talked about (i.e. fading) to blend the colors. Here is an example of such an LED:

http://www.sparkfun.com/commerce/product_info.php?products_id=9264

Note that it has a single cathode, and an anode for each color.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 34
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry, I guess I should have mentioned that I am dealing with an rgb led. I want it to show one random color but that one random color is always the same.
Logged

Washington
Offline Offline
Jr. Member
**
Karma: 0
Posts: 90
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

well for a big mix of random colors you could create a random value generator for the pwm of each rgb pin. http://arduino.cc/en/Reference/RandomSeed
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 34
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yes, but I want it to show just one random color and stop.
Logged

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 388
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you post your code so we can see how you are using randomSeed()? It is the key to what you want to do. Don't forget to use the code button
  • when you post code.
« Last Edit: September 12, 2009, 11:41:31 pm by TBAr » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 34
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm using this code:
#define PIN_RED            9    
#define PIN_GREEN         10    
#define PIN_BLUE          11    
void color(byte v)
{
  digitalWrite(PIN_RED,    v       & 0x01);
  digitalWrite(PIN_GREEN, (v >> 1) & 0x01);
  digitalWrite(PIN_BLUE,  (v >> 2) & 0x01);
}
void setup()
{
  pinMode(PIN_RED,   OUTPUT);  
  pinMode(PIN_GREEN, OUTPUT);  
  pinMode(PIN_BLUE,  OUTPUT);
  randomSeed(analogRead(0));
}
void loop()
{
  color(random(100));
}


I got it off a website and changed it a little so it doesn't change colors all the time but no matter what, my led just shows white and it sort of blinks rapidly so all you see is this white light that is flashing extremely fast
« Last Edit: September 13, 2009, 08:57:28 am by ikestarm » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25797
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void loop()
{
 color(random(100));
}
What happens if you move the "color(random(100));" into "setup()", and leave "loop();" empty?

That'll stop the flashing.

The white light is probably your eye "averaging" the rapidly-changing colours caused by "loop ()", well, looping and picking a new colour every time.

BTW "random (smiley-cool;" should work just as well.
« Last Edit: September 13, 2009, 11:26:20 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 34
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

that worked, but how do I get it to be a tatally random color not one of eight?
« Last Edit: September 13, 2009, 12:53:11 pm by ikestarm » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25797
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Then you need a random number in the range 0.. 224-1, and use PWM pins for the LEDs and "analogWrite". (actually, you'd probably get away with a number in the range 0..215-1, but you'd still need "analogWrite", scaled appropriately)
« Last Edit: September 13, 2009, 01:25:05 pm by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 34
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry, I'm still quite new to arduino and I don't really get what you're saying. Could you please show mw an example?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25797
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you use a single binary bit to light an LED, the best you can manage is eight distinct states.
LEDs are either ON or OFF (HIGH or LOW).
The Arduino has pins which are PWM-capable (for the Duemilanova these are 3, 5 ,6, 9, 10 and 11, accesible via "analogWrite").
A PWM signal gives a fixed-frequency, variable mark:space ratio which approximates an analogue signal; the higher the mark (HIGH) time, the brighter a connected LED will appear.
So, if you have a LED connected to, say, pin 3, "analogWrite (3, 128);" will give a roughly 50:50 on/off signal, so the LED will appear less bright than if you'd executed "analogWrite (3, 255);"
(the relationship is not linear, but that's complicating things)

So, for your example: (uncompiled, untested)
Code:
const byte PIN_RED     = 3;
const byte PIN_GREEN = 5;
const byte PIN_BLUE    = 6;

void color(unsigned long v)
{
 analogWrite(PIN_RED,    v  & 255);
 analogWrite(PIN_GREEN, (v >> 8) & 255);
 analogWrite(PIN_BLUE,  (v >> 16) & 255);
}
void setup()
{
 pinMode(PIN_RED,   OUTPUT);  
 pinMode(PIN_GREEN, OUTPUT);  
 pinMode(PIN_BLUE,  OUTPUT);
 randomSeed(analogRead(0));
 color((unsigned long)random(0x01000000));
}
void loop()
{
}


« Last Edit: September 13, 2009, 03:20:31 pm by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 34
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks, the code worked, and I understand it now. smiley Now do you think at any one color it will know the value of the blue, red, and green pins so I can do something else with it?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25797
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Not quite sure what you want to do, but there are several things you could do:
Code:
void color(unsigned long v)
{
 byte red = v & 255;
 byte green = (v >> 8) & 255;
 byte blue = (v >> 16) & 255;
 analogWrite(PIN_RED,    red);
 analogWrite(PIN_GREEN, green);
 analogWrite(PIN_BLUE,  blue);
 // red green blue values are available for other uses
}

or you could declare "red" "green" "blue" with global scope, or call "random (256);" three times, and pass separate values to your LED lighting function.
Lots of different ways.
« Last Edit: September 14, 2009, 04:34:48 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 34
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, I did that, and then I did this code to test it:

Code:
const byte PIN_RED     = 9;
const byte PIN_GREEN = 10;
const byte PIN_BLUE    = 11;

void color(unsigned long v)
{
 byte red = v & 255;
 byte green = (v >> 8) & 255;
 byte blue = (v >> 16) & 255;
 analogWrite(PIN_RED,    red);
 analogWrite(PIN_GREEN, green);
 analogWrite(PIN_BLUE,  blue);
 // red green blue values are available for other uses
}


void setup()
{
Serial.begin(9600);
 pinMode(PIN_RED,   OUTPUT);  
 pinMode(PIN_GREEN, OUTPUT);  
 pinMode(PIN_BLUE,  OUTPUT);
 randomSeed(analogRead(0));
 color((unsigned long)random(0x01000000));
}
void loop()
{
  if(PIN_RED > 100);{
    Serial.print("redled on ");
  }
    
    if(PIN_RED < 100);{
     Serial.print("redled off ");
    }
      delay(3000);
}


Unfortunately, the serial monitor just says "led on led off" every 3 seconds. Why does it do that?
Logged

Pages: [1] 2 3 4   Go Up
Jump to: