-
1The basics
tpHSV Layer[15][10];
First, load up one of the NeoPixel examples and make sure your strip works. This way you get all the initializers you need as well.
NeoPixel library can work with Hue, Saturation and Value. To make it easier to program, we create a custom type that can hold these values:
struct tpHSV{ uint16_t H; byte S; byte V; };
We then create a matrix of these values, here 15x10;
tpHSV Layer[15][10];
We call it a "Layer" since we will later combine multiple of pictures (layers) to creat a final flattened picture. (Just as you do with layers in photoshop). To paint on one of these pictures we simply:
Layer[0][0].H=1 Layer[0][0].S=255 Layer[0][0].V=255 Layer[1][0].H=1 Layer[1][0].S=255 Layer[1][0].V=255
making two pixels red, with full saturation and full brightness. This code can be put in the Setup() portion of your code.
Now the NeoPixel library doesnt know we have made a matrix LEDs, so we need to convert our X-Y coordinates of the layer to a linear number which corresponds to the correct LED on our physical board. You can put the following function in your code:
int XYtoI(int x, int y){ int ret; if(x==0){ ret=9-y; return ret; }else{ ret=10*x; if((x%2) == 0) {// Is X even ret=ret+(9-y); }else{ ret=ret+(y); } return ret; } }
This function assumes your number 0 led is in the bottom left corner, and number 1 is the one above it, and so on until we reach LED number 9 on top. Then it assumes that LED number 10 is to the right of 9 and number 11 is below number 10. And so the led numbers snake on.
Now we can put the following code in our main loop:
strip.clear(); for(int Y=0;Y<10;Y++){ for(int X=0;X<15;X++){ if(Layer[X][Y].H>0){ strip.setPixelColor(XYtoI(X,Y), strip.gamma32(strip.ColorHSV(Layer[X][Y].H,Layer[X][Y].S,Layer[X][Y].V))); } } } strip.show();
This code only draws pixels that have a hue other than 0. This way all H0 pixels will be "transparent" to what we draw below.
To limit the speed at which the LED strip is being updated we will add the following in our declarations:
unsigned long UpdateTimer;
and change our loop to:
if(millis()-UpdateTimer>float(1000)/float(120)){ strip.clear(); for(int Y=0;Y<10;Y++){ for(int X=0;X<15;X++){ if(Layer[X][Y].H>0){ strip.setPixelColor(XYtoI(X,Y), strip.gamma32(strip.ColorHSV(Layer[X][Y].H,Layer[X][Y].S,Layer[X][Y].V))); } } } strip.show(); UpdateTimer=millis(); }
This way we will not update the led strip faster than 120 Hz (Times per second).
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.