G+_Brent Vrieze Posted February 24, 2018 Share Posted February 24, 2018 FastLED Question, I'm trying to do some WS2812b programing with an Arduino. I am setting several individual strands of WS2812b and would like to use some of the new parts of the library specifically the following which automatically loops over the string changing the color. (this compiles) //sets all pixels in "ledsB1" to white for( CRGB &pixel : ledsB1 ) { pixel = CRGB::White; } This defines the string of LEDs for what I entered above. #define NUM_LEDS_B_STRIP 9 CRGB ledsB1[NUM_LEDS_B_STRIP]; I would like to create a function(see below)that takes in a string of LEDs and turns them the color I declare. (this does not compile) //takes an LED array to set the color. void letterBlack( CRGB ledStrip[] ) { //test of experimental code easy way to turn entire array black does not work for( CRGB &pixel : ledStrip ) { pixel = CRGB::Black; } } The compile error is: (FYI the ^ points to the l in ledStrip now where you see it here) Bomb-Botz_Sign:82: error: 'begin' was not declared in this scope for( CRGB &pixel : ledStrip ) { ^ Bomb-Botz_Sign:82: error: 'end' was not declared in this scope exit status 1 'begin' was not declared in this scope Anyone have an idea on how to fix this. My C++ programing ended 10 years ago and I am stumped. Thanks Brent Link to comment Share on other sites More sharing options...
G+_Telford Dorr Posted February 25, 2018 Share Posted February 25, 2018 FOR loops typically have three parameters: for (i = 0; i < 5; ++i) While you don't need all three parameters, you do need the ";" punctuation. Maybe you're thinking of a WHILE loop? while (i < 5 ) Link to comment Share on other sites More sharing options...
G+_Bob Shields Posted February 25, 2018 Share Posted February 25, 2018 Telford Dorr - the 2 parameter for loop available in most modern Object-Oriented languages is referred to as a "for each" loop, and is designated by a different separator (':' vs ';'). The first example could be read as "for each CRGB object in the ledsB1 array, assign the pixel variable the address of the next item and process the code in the {}". Brent Vrieze - the reason the first loop works it that the compiler knows the length of the ledsB1 array from its declaration (NUM_LEDS_B_STRIP = 9). But with the letterBlack function, the input ledStrip array could be any length from 0 to infinity, so the compiler doesn't know how many times to run the loop. Possible solutions: void letterBlack( CRGB ledStrip[], int numLeds ) { for (int i = 0; i < numLeds; i++) { ledStrip = CRGB::Black; } } or, if you don't like "indexing", try: for (CRGB &pixel = ledStrip; pixel < &ledStrip[numLeds]; pixel++) { pixel = CRGB::Black; } But the best way is to just use the FastLED library function: fill_solid(ledStrip, numLeds, CRGB::Black); Link to comment Share on other sites More sharing options...
G+_Brent Vrieze Posted February 25, 2018 Author Share Posted February 25, 2018 Bob Shields I think your suggestion to use fill_solid is the best answer. The code I showed came from this page https://github.com/FastLED/FastLED/wiki/RGBSet-Reference. It is marked as under development but thought I would try it out. It also mentions the need for C++11 support. I have no idea what that is. As far as the syntax of a standard for loop thanks for the info but the code I shared is not standard and if my background were in C++ and not Java would be able to tell you why there is a colon instead of a semi-colon in there. I also must say I stopped writing code 10 years ago opting for system admin and network admin work. This is a blast from the past trying to write for a micro-controller. Thanks github.com - FastLED Link to comment Share on other sites More sharing options...
G+_William L. DeRieux IV Posted February 25, 2018 Share Posted February 25, 2018 Brent Vrieze You need C++11 support because the for-each loop was not added to the compiler until that version. en.wikipedia.org - Foreach loop - Wikipedia https://en.wikipedia.org/wiki/C%2B%2B11 The 11 indicates that it is the standard as revised, by ISO, in 2011. Without C++11 support you would need this code (and it does makes sense why it complained about missing begin/end) for(CRGBSet::iterator pixel=ml.begin(), end=ml.end(); pixel != end; ++pixel) { (*pixel) = CRGB::Black; } PS: that syntax is "standard", it just requires a "newer standard" PPS: You said your background is in Java (in the Java spec the for-each loop variable is read-only and Java does not support pointers, only reference types)....so this would not have worked in Java. Link to comment Share on other sites More sharing options...
G+_Brent Vrieze Posted February 27, 2018 Author Share Posted February 27, 2018 William L. DeRieux IV Thanks you gave the answer for sure. I now know the different C++ versions and that they added the new for loop syntax. Link to comment Share on other sites More sharing options...
Recommended Posts