The new Raspberry Pi B Model B+ is here - Pick one up in the store and check our detailed guide!

How to address each LED alone [LPD8806]

by JustAnotherPerson on Sun Sep 30, 2012 8:34 pm

Well, I've had my LED belt set for a good while now, and everyone at my school just loves it. But I've never understood how to program each LED on it's own. I mean, like make it so i can set all 30 (lost 2 of em) to a specific color. If more detail is needed, ask away :). Thank you all for the help
JustAnotherPerson
 
Posts: 3
Joined: Sun Sep 30, 2012 8:28 pm

Re: How to address each LED alone [LPD8806]

by adafruit_support_bill on Mon Oct 01, 2012 4:47 am

To set a single pixel, use the "setPixelColor(pixel, color)" function. The 'pixel' parameter is the number of the pixel you want to set and 'color' is the RGB color you want to set it to. The "Color(r, g, b)" function is used to create the RGB color value.

For example:

Code: Select all | TOGGLE FULL SIZE
strand.setPixelColor(5, strand.Color(255, 0, 0)); // set pixel #5 Red
strand.setPixelColor(6, strand.Color(0, 255, 0)); // set pixel #6 Green
strand.setPixelColor(7, strand.Color(0, 0, 255)); // set pixel #7 Blue
strand.setPixelColor(8, strand.Color(255, 255, 255)); // set pixel #8 White
strand.setPixelColor(9, strand.Color(0, 0, 0)); // set pixel #9 Black
User avatar
adafruit_support_bill
 
Posts: 27961
Joined: Sat Feb 07, 2009 9:11 am

Re: How to address each LED alone [LPD8806]

by JustAnotherPerson on Tue Oct 02, 2012 5:50 pm

adafruit_support wrote:To set a single pixel, use the "setPixelColor(pixel, color)" function. The 'pixel' parameter is the number of the pixel you want to set and 'color' is the RGB color you want to set it to. The "Color(r, g, b)" function is used to create the RGB color value.

For example:

Code: Select all | TOGGLE FULL SIZE
strand.setPixelColor(5, strand.Color(255, 0, 0)); // set pixel #5 Red
strand.setPixelColor(6, strand.Color(0, 255, 0)); // set pixel #6 Green
strand.setPixelColor(7, strand.Color(0, 0, 255)); // set pixel #7 Blue
strand.setPixelColor(8, strand.Color(255, 255, 255)); // set pixel #8 White
strand.setPixelColor(9, strand.Color(0, 0, 0)); // set pixel #9 Black


That seems fairly simple. How I missed it, I don't know. But how would I go about making some code like what came with the demos, so it actually rotates around the belt, or makes a scanning motion like the police scanner?
JustAnotherPerson
 
Posts: 3
Joined: Sun Sep 30, 2012 8:28 pm

Re: How to address each LED alone [LPD8806]

by adafruit_support_bill on Tue Oct 02, 2012 6:59 pm

how would I go about making some code like what came with the demos

Have you read the code that comes with the demos? You will notice that it mostly makes use of the "for" statement to create the patterns. The 'for' statement is documented here: http://arduino.cc/en/Reference/For
User avatar
adafruit_support_bill
 
Posts: 27961
Joined: Sat Feb 07, 2009 9:11 am

Re: How to address each LED alone [LPD8806]

by JustAnotherPerson on Tue Oct 02, 2012 7:17 pm

adafruit_support wrote:
how would I go about making some code like what came with the demos

Have you read the code that comes with the demos? You will notice that it mostly makes use of the "for" statement to create the patterns. The 'for' statement is documented here: http://arduino.cc/en/Reference/For

I've looked at the code, mostly. I was mainly confused by the
Code: Select all | TOGGLE FULL SIZE
void colorChase(uint32_t c, uint8_t wait);
above the loop
JustAnotherPerson
 
Posts: 3
Joined: Sun Sep 30, 2012 8:28 pm

Re: How to address each LED alone [LPD8806]

by TheSniper on Sun Jan 27, 2013 1:21 am

Found this post.
along with suggested code.
How would one not only address each LED indivually but also set its brightness in the same line?
Code: Select all | TOGGLE FULL SIZE
strand.setPixelColor(5, strand.Color(255, 0, 0)); // set pixel #5 Red
strand.setPixelColor(6, strand.Color(0, 255, 0)); // set pixel #6 Green
strand.setPixelColor(7, strand.Color(0, 0, 255)); // set pixel #7 Blue
strand.setPixelColor(8, strand.Color(255, 255, 255)); // set pixel #8 White
strand.setPixelColor(9, strand.Color(0, 0, 0)); // set pixel #9 Black


I found another post that says to control the brightness you use the setPixelColor() 0=off and 255 is MAX
How do I find the setPixelColor() syntax / options?

Thans in Advance!
TheSniper
 
Posts: 2
Joined: Sun Jan 27, 2013 1:13 am

Re: How to address each LED alone [LPD8806]

by adafruit_support_bill on Sun Jan 27, 2013 7:55 am

How would one not only address each LED indivually but also set its brightness in the same line?

Just as in the code fragment you posted. The first argument is the pixel number, the second argument is the color.

The color is composed of 3 parts: Red, Green and Blue. By adjusting the values of the R, G & B between 0 (off) and 255(maximum intensity) you can control the color of the pixel.
User avatar
adafruit_support_bill
 
Posts: 27961
Joined: Sat Feb 07, 2009 9:11 am

Re: How to address each LED alone [LPD8806]

by TheSniper on Sun Jan 27, 2013 8:44 am

Yes I think I understand the color property.
But say I want RED then the next time I want red at at half the intensity or even .25 the intensity. are you saying the code would look like this.

[code] strand.setPixelColor(0, strand.Color(255, 0, 0)); // set pixel #0 100%FULL Red
strand.setPixelColor(1, strand.Color(127, 0, 0)); // set pixel #1 50% intensity Red
strand.setPixelColor(2, strand.Color(65, 0, 0)); // set pixel #5 25% intensity Red[/code
TheSniper
 
Posts: 2
Joined: Sun Jan 27, 2013 1:13 am

Re: How to address each LED alone [LPD8806]

by adafruit_support_bill on Sun Jan 27, 2013 9:21 am

Yes - mostly. The catch is that neither the leds nor your retinas respond linearly. You will have to experiment a bit to find the value the "looks" like half intensity.
User avatar
adafruit_support_bill
 
Posts: 27961
Joined: Sat Feb 07, 2009 9:11 am

Re: How to address each LED alone [LPD8806]

by MarkEMarkEMark on Sun Jan 27, 2013 10:29 am

You can also hijack the 'hsv2rgb' function from the advanced belt kit.

Copy that function out to your code, and then use something like...


Code: Select all | TOGGLE FULL SIZE
long color, hue, saturation, level;

hue = 0; //0 - 1536 where 0=red 512=green 1024=blue
saturation = 255; //0 - 255 where 255 is full colour, and 0 is white
level = 255; 0 - 255: where 255 is brightest, and 0 is dimmest

color = hsv2rgb(hue, saturation, level);
strand.setPixelColor(0, strand.Color(color >> 16, color >> 8, color));


with this, you can just set the brightness with level, no matter what hue you choose.

No guesswork!! :D
MarkEMarkEMark
 
Posts: 33
Joined: Thu Dec 20, 2012 8:33 am

Re: How to address each LED alone [LPD8806]

by mohawkpiper on Mon Jan 28, 2013 3:44 am

adafruit_support wrote:Yes - mostly. The catch is that neither the leds nor your retinas respond linearly. You will have to experiment a bit to find the value the "looks" like half intensity.


talking about the colors not looking linear...
a bias function will fix this.

here is one i use quite often, it is written to be used in a for loop though, for creating a fading "trail" of pixels after the original full brightness one. but it can be re-written for a single pixel.

Code: Select all | TOGGLE FULL SIZE
uint32_t fade_color(uint32_t color, int decay, int current, float BIAS) {
    //Decompose
    byte g = color >> 16 & 0x7f;
    byte r = color >> 8 & 0x7f;
    byte b = color & 0x7f;
   
    //do our math
    float thisDecay = pow( (((float)decay) - current) / decay, log(BIAS)/log(0.5) );
    g = (byte) ((float) g) * thisDecay;
    r = (byte) ((float) r) * thisDecay;
    b = (byte) ((float) b) * thisDecay;

    //recompose and return
    return (g << 16) + (r << 8) + b;
}


obviously 'color' is the original color passed into it.
'decay' is how many pixels are in your fading "trail".
'current' is the pixel in that trail that you are on.
'BIAS' is the bias you would like.
i found a bias of 0.1 or 0.15 works well to make it look pretty linear in fading it.

NOTE: the thisDecay function is pretty slow, especially for longer "tails", so if speed in your LEDs is what you need, this isnt the best way to do it. I couldnt figure out something that worked pretty fast that did something similar. :(
User avatar
mohawkpiper
 
Posts: 71
Joined: Sat Oct 13, 2012 2:28 am