1

So I'm quite new to the whole Arduino coding but constructed a sketch that does work, but I think it doesn't work properly. So I'd love to get some advice on how to improve it.

This script is currently running on a Wemos ESP8266 chip attached to a 1 meter WS2812 144 LED strip.

My questions:

  1. The loop is set to break when fadein is higher then 254. When I try to break it at fadein == 255 the loop doesn't work anymore as it the fadein value directly jumps to 255. Why?

  2. When I set the delay to zero the led flicker due to too fast updating. however, this flickering continues even after the for loop is broken, which makes me conclude that the for loop is still running even when the break is applied.

    Of course this is no longer an issue when the delay is set higher (as it is supposed to be) but it still seems a waste of processing power to keep the for loop active while it's not doing anything new anymore how can i completely stop the loop from being executed?

  3. How can this be done differently / more effective?

The code

#define FASTLED_ALLOW_INTERRUPTS 0                                    //fix jitter in led strip
#include <FastLED.h>                                                  //add fastled lib
#define NUM_LEDS 144                                                  //set number of leds
#define DATA_PIN D4                                                   //set data pin leds

CRGB ledStrip[NUM_LEDS];                                              //init led strip
int fadeIn = 8;                                                       //init fade in counter
int led = 0;                                                          //init led counter

void setup() { 
  Serial.begin(115200);                                               //setup comm channel
  FastLED.addLeds<WS2811,DATA_PIN, RGB>(ledStrip, NUM_LEDS);          //setup ledstrip config
}

void loop()                    
{
for (int led = 0; led < NUM_LEDS; led++) {                            //move led up the strip till max count is reached
    ledStrip[led] = CHSV( 0+led*1.77, 255, 225);                      //set animated led hue value to make a rainbow
    FastLED.show();                                                   //show led color
    ledStrip[led] = CHSV( 0+led*1.77, 255, fadeIn);                   //set non animated led hue to same hue value while increasing the fade value
    delay(3);
    if (fadeIn > 254) {                                               //if the fadeIn value exceed 254
      break;                                                          //stop led animation
    }
}

    if (led = 144 && fadeIn < 254) {                                   //when the 144th led is reached and the fadein value is less then 255
      fadeIn+=16;                                                      //increase fadeIn value with 16
    }
    if (led = 144 && fadeIn < 255) {                                   //when the 144th led is reached and fadeIn is smaller then 255 show debug messages
      Serial.println(led);                                             //print led counter value
      Serial.println(fadeIn);                                          //print fadein value
    }
    if (fadeIn > 255) {                                                 //if the fadeIn value exceed 255
      fadeIn = 255;                                                     //clamp fadeIn value to 255

    }


} 
  • +1 for properly formatted code .... if i could, i'd give you another +1 for the comments being out of the way of the code – jsotola Sep 6 '18 at 20:55
3
  1. Consider the sequence of values of "fadeIn". It starts at 8 and in incremented by 16. So: 8, 24, ... 248, 264, ... . It is never exactly 255. So you never break out of your "for" loop.

  2. There is only 1 delay in your example code. It is difficult to associate the described behavior in your question and the location of the delay.

  3. Arduino programming is actually C and C++ programming. Developed at Bell Labs, this language has been around for decades and there are countless books on the subject. Rather than rewriting your program (someone might yet still do this) I would recommend leaning C programming and if you want to write Arduino library software then C++ programming as well.

  • correct, i wanted to be flexible with the increment value. therefor this if was added at the end: if (fadeIn > 255) { //if the fadeIn value exceed 255 fadeIn = 255; //clamp fadeIn value to 255 } – nixx Sep 6 '18 at 13:15
  • As you are new to stack exchange I would recommend the following: Ask one question at a time. And do not hesitate editing your question to make the question better. This is not a forum. The questions should stand on their own and not depend on the discussion. Be careful not to ask open ended questions nor opinions. These types of questions usually get flagged and people will not answer them. When writing your questions keep in mind others will search for your question and the answers posted. So make the question concise and easy to understand. – st2000 Sep 6 '18 at 13:22
  • thanks for the suggestions. I'll keep them in mind with future posting I know there are books and I'm reading as much as I can online, but to do a full course on programming to get 1 led function to run would be a bit unbalanced for me (plus i'm a graphics man, not a heavy coder). so I was hoping to get some advice on this 'simple' function that looks to be constructed properly from all references I found but somehow doesn't seem to work like it should – nixx Sep 6 '18 at 21:02
2

This is doing an assignment, not the comparison you are expecting

if (led = 144

Need == for the comparison

  • 1
    I believed so too originally, but when I try this the function breaks and the fadeIn never increases.. so I left the led = 0 in it as led == 0 doesn't work – nixx Sep 6 '18 at 21:00
  • okay.. figured this one out.. it's never 144 as I have 144 leds but it start counting at 0 thus never reached 144 but only 143 as max – nixx Sep 6 '18 at 22:24
0

You close your for() loop right after the if statement that contains the break. No where in that for() loop do you change fadeIn, so there is nothing to break out of.

If you move the } that closes the for loop to below the code that changes fadeIn, then the break has an opportunity to be executed:

void loop() {
  for (int led = 0; led < NUM_LEDS; led++) {                            //move led up the strip till max count is reached
    ledStrip[led] = CHSV( 0+led*1.77, 255, 225);                      //set animated led hue value to make a rainbow
    FastLED.show();                                                   //show led color
    ledStrip[led] = CHSV( 0+led*1.77, 255, fadeIn);                   //set non animated led hue to same hue value while increasing the fade value
    delay(3);
    if (fadeIn > 254) {                                               //if the fadeIn value exceed 254
      break;                                                          //stop led animation
    }

    if (led = 144 && fadeIn < 254) {                                   //when the 144th led is reached and the fadein value is less then 255
      fadeIn+=16;                                                      //increase fadeIn value with 16
    }
    if (led = 144 && fadeIn < 255) {                                   //when the 144th led is reached and fadeIn is smaller then 255 show debug messages
      Serial.println(led);                                             //print led counter value
      Serial.println(fadeIn);                                          //print fadein value
    }
    if (fadeIn > 255) {                                                 //if the fadeIn value exceed 255
      fadeIn = 255;                                                     //clamp fadeIn value to 255
    }
  }
}
  • well, that the funny thing.. when i tried this before (or use your code) the loop doesn't run at all somehow. I thought i needed to keep those last 3 if's out of the loop – nixx Sep 6 '18 at 20:58
  • There are other bugs, pointed out in the other answers, such as the assignment operator used in place of a comparison operator. I was only noting the bug of a loop break condition not able to be met if the condition doesn’t change within the loop. – jose can u c Sep 6 '18 at 21:19
0

Ok, thanks for all your help, found the solution to make it stable. I think I got thrown off by the fact the led = 144 did (magically) seem to work outside the loop but led == 144 didn't work at all.

turns out i was counting the leds wrong (it's from 0-143 instead of 1-144) and that made all the other pieces fall into place.

tnx

#define FASTLED_ALLOW_INTERRUPTS 0                                    //fix jitter in led strip
#include <FastLED.h>                                                  //add fastled lib
#define NUM_LEDS 144                                                  //set number of leds
#define DATA_PIN D4                                                   //set data pin leds

CRGB ledStrip[NUM_LEDS];                                              //init led strip
int fadeIn = 8;                                                       //init fadein value
int fadeIncr = 16;                                                    //init fadein increments

void setup() { 
  Serial.begin(115200);                                               //setup comm channel
  FastLED.addLeds<WS2811,DATA_PIN, RGB>(ledStrip, NUM_LEDS);          //setup ledstrip config
}

void loop()                    
{
for (int led = 0; led < NUM_LEDS; led++) {                            //move led up the strip till max count is reached
    ledStrip[led] = CHSV( 0+led*1.77, 255, 225);                      //set animated led hue value to make a rainbow
    FastLED.show();                                                   //show led color
    ledStrip[led] = CHSV( 0+led*1.77, 255, fadeIn);                   //set non animated led hue to same hue value while increasing the fade value
    delay(1);
    if (led == 143) {                                                  //when the 144th led is reached and the fadein value is less then 255
      fadeIn+=(fadeIncr);                                              //increase fadeIn value with 16
      fadeIn=constrain(fadeIn, 0, 255);                                // clip value range
        Serial.println(led);                                           //print led counter value
        Serial.println(fadeIn);                                        //print fadein 
    } 
    if (fadeIn == 255) {                                               //if the fadeIn value exceed 255
      break;                                                           //stop led animation
    } 
  }
} 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.