Jump to content

Does this look right, I m trying to program a certain amount of leds on a ws2812 strip, I tried c...


G+_Donald Kloss
 Share

Recommended Posts

I am by no means an expert, but that loop looks rather redundant and it is not really done very well....

 

I would have thought that adafruit would write code that made sense (or at least would have had better documentation), but I guess not.

 

I think you can find a way to write that so it can be followed more easily.

Link to comment
Share on other sites

Donald Kloss I don't know what you mean by "different letters".

Do you mean that you took adafruit's code and modified it ?

 

Are you trying to test 8 leds and have each one sweep through all RGB values, or something like that ?

 

as David Peach said it would help to see the actual adafruit code sample (as well as your entire code)

Link to comment
Share on other sites

Donald Kloss You only committed .gitattributes

I see that you posted the code on pastebin...

 

Like I said, it is hard to determine what you are trying to accomplish (just by looking at the code). You might want to add some comments (ie documentation) as to what is going on and to state what you are doing.

 

From what you said earlier: "what im trying to do is static LED colors for each LED on the strip".

 

I think you could just set the led colors directly without using a nested for loop the way that you have (this is a non-standard use of a for loop), for this application you should probably use a while loop instead and set your colors inside of that for each pixel, possibly even making use of a function to handle setting the colors by passing in the relevant data.

Link to comment
Share on other sites

If I'm understanding the code correctly, it will loop through all the pixels and change them in a chase pattern.

Assuming RGB; the first pass will go blue, blue, blue, red, green, light blue, orange.

Following loops will move that pattern down the string of lights. I haven't had the opportunity to test the lights myself (hoping to get some soon), so I'm not sure whether the beginning lights will stay blue as they were originally set, or default to off since so value was given.

Link to comment
Share on other sites

You mentioned static colors. Do you want them all the same color or different colors? If different colors but static, I think the easiest way would be just something like.....

pixels.setPixelColor(0, pixels.Color(0,0,255));

pixels.setPixelColor(1, pixels.Color(0,255,255));

pixels.setPixelColor(3, pixels.Color(255,0,255));

pixels.setPixelColor(4, pixels.Color(0,255,0));

...

...

pixels.setPixelColor(9, pixels.Color(255,255,255));

 

If instead you want them all the same color, you could do it like above or with one for loop:

for(int a=0;a

pixels.setPixelColor(a, pixels.Color(0,0,255));

}

pixels.show();

 

 

...

 

Like I said, no experience so I don't know if this would even work or if I'm understanding it correctly, but hopefully it helps.

Link to comment
Share on other sites

A recommendation on code commenting: the most useful comment is a block comment before a section of code that explains, in general terms, what the block is trying to accomplish. Unless it's already done elsewhere. include a description of the data or parameters used by the code block and what it is supposed to contain (including value ranges, if appropriate). This makes it easier for future maintainers of your code (including you) to figure out down the road what you were thinking when you wrote the code.

 

What you don't want to do is try to describe individual lines of code. Someone reading the listing can determine that from the preceding comment block.

 

Regarding the code: if (a) you're trying to make all LEDs the same color, set the color values outside of the for loop, then inside the for loop write the same data n times, where n = number of LEDs in the chain. if (b) you're trying to have each LED a different color, use the index value from the for loop (in this case, i) to select the color from an array of fixed color values and then write those values to the LED chain. The LED order is the same as the data table order.

 

In either case, a single for loop should be all that is needed.

 

Hope this helps.

Link to comment
Share on other sites

I don't have any ws2812's to test with so ... Need to get some.

Still not sure what your trying to do but I modded your loop. This "should" create a lite chase with 2 pixels off. It loops thru the 'for' loop then repeats the main loop. Only one var, 'a', start of the 7 pixels.

Good Luck.

Opps, fixed count.

 

void loop() {

 

// For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one.

//This "should" cause a lite chase with 2 off pixels

 

for(int a = 0; a < NUMPIXELS; a++)

{

// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255

pixels.setPixelColor(a, pixels.Color(0,0,255)); // Moderately bright green color.

pixels.setPixelColor((a+1)%10, pixels.Color(0,0,255));

pixels.setPixelColor((a+2)%10, pixels.Color(0,0,255));

pixels.setPixelColor((a+3)%10, pixels.Color(140,45,14));

pixels.setPixelColor((a+4)%10, pixels.Color(0,255,0));

pixels.setPixelColor((a+5)%10, pixels.Color(95,224,233));

pixels.setPixelColor((a+6)%10, pixels.Color(255,128,0));

pixels.setPixelColor((a+7)%10, pixels.Color(0,0,0));

pixels.setPixelColor((a+8)%10, pixels.Color(0,0,0));

pixels.setPixelColor((a+9)%10, pixels.Color(0,0,0));

 

pixels.show(); // This sends the updated pixel color to the hardware.

 

delay(delayval); // Delay for a period of time (in milliseconds).

 

}

}

Link to comment
Share on other sites

If you're feeling adventurous. Add an array of the static colors and then index thru them and assign. Problem with coding, there's always another way :).

 

int delayval = 500; // delay for half a second

int colorsRGB[NUMPIXELS][3] = { {0,0,255}, {0,0,255}, {0,0,255},

{140,45,14}, {0,255,0}, {95,224,233},

{255,128,0}, {0,0,0}, {0,0,0}, {0,0,0} };

 

void loop() {

 

//This "should" cause a lite chase with 3 off pixels

 

for(int pixelStart = 0; pixelStart < NUMPIXELS; pixelStart++)

{

// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255

for(int pixel = 0; pixel < NUMPIXELS; pixel++)

{

pixels.setPixelColor((pixelStart + pixel) % 10,

pixels.Color(colorsRGB[pixel][0],

colorsRGB[pixel][1],

colorsRGB[pixel][2] ));

}

 

pixels.show(); // This sends the updated pixel color to the hardware.

 

delay(delayval); // Delay for a period of time (in milliseconds).

 

}

}

Link to comment
Share on other sites

 Share

×
×
  • Create New...