Hello All, I have a Question regarding referencing more than 1 pixel at a

Hello All, I have a Question regarding referencing more than 1 pixel at a time. I am currently messing around with the Blink example from FastLED. It was a simple enough example and I used what it taught me to write this code: http://pastebin.com/raw.php?i=1hnCfy54

I’m sure its pretty crude and could be improved upon 1000 fold. But my question is can it be made shorter? Rather than Referencing each pixel one by one is there a way to reference the 4 pixels in a group such as defining a variable for each section.

For the most part the code does what I want it to do but my concern is memory. I have a few more Animations to write and if this code is any indication in size then I fear I may not have enough memory.

Once again Thanks for your help!!!

Here is the LED Setup I am using… Maybe it will help you visualize what I’m doing. The LEDs are not wired yet so I can’t Light them up… My Apologies.

The first, simplest way to make it shorter would be to use ‘for’ loops: https://www.arduino.cc/en/Reference/For

Instead of:

leds[0] = CRGB::Red;
leds[1] = CRGB::Red;
leds[2] = CRGB::Red;
leds[3] = CRGB::Red;

Use:

for(int i = 0; i < 4; i++) {
leds[i] = CRGB::Red;
}

Hey Erin, in case you have many repeating code blocks a for loop would be the solution. https://www.arduino.cc/en/Reference/For

An example:

for (int i=0; i < 4; i++) { leds[i] = CRGB::Red; }

Sets the first 4 leds (index 0-3) to red.

Sadly, there isn’t - at least not in the library by default (yet… I have a plan…). However, you could do something like this:

void set(CRGB *someleds, int start, int count, const CRGB & color) {
for(int i = start; i < (start+count); i++) { someleds[i] = color; }
}

Then you can do:

set(leds, 0, 4, CRGB::Red);
FastLED.delay(50);
set(leds, 0, 4, CRGB::Black);
set(leds, 4, 4, CRGB::OrangeRed);
FastLED.delay(50);

Telepathy, @Jason_Coon ?! :wink:

Don’t forget to check out all of the examples you can find online and play around with them. There’s a LOT of stuff you can do with very little code and the examples provided with FastLED are the best place to start.

Checking out the examples “Multiple” folder would be a good start. From there you have a few options depending on how you want to wire them up.

This is possible:
leds[0] = leds[1] = leds[2] = leds[3] = CRGB::Red;

A better option:
fill_solid(leds, 4, CRGB(0,0,255); // Fill 4 pixels with blue, starting at leds[0]
or
fill_solid(leds+5, 10, CRGB::Blue); // Fill 10 pixels with blue, starting at leds[5]

Another option would be a loop:
for (int x=0; x<5; x=x+1) { leds[x]= CHSV(0,255,255); }

I type too slow haha :stuck_out_tongue:

Thank you all. I’m going to Play around with all of those options Right now.

BTW, we’re going to want to see photos and video of your Guy-Manuel helmet when it’s finished.

@marmil
I do Plan to post tons of photos when it done… lol

Psst: https://github.com/FastLED/FastLED/wiki/RGBSet-Reference

See also https://plus.google.com/102282558639672545743/posts/a3VeZVhRnqG

doesn’t fill_solid almost do this? just pass one more parameter…

The new CRGBSet/CRGBArray stuff ends up being better - to set all the lads to a color you can just say leds = CRGB::Blue; or if you want to set a portion of the less to a color leds(5,8) = CRGB::Blue. Of course, where the new stuff starts becoming really useful will be for doing more complex manipulations of subsets of led data.

I finally have a few hours to mess around with some code. I plan to try this new method. I can’t wait!!!