FastLED not working in setup

Hello everyone,
I have a strange problem when running FastLED.h for a WS2812B LED strip.
This code is for a WeMos D1 mini (ESP8266)
Requirement - Check battery voltage on startup and give visual indication via LED strip.

My approach - Call a function in setup called batteryVoltage() which will compute the value in percentage and will light up the strip.

My problem - The batteryVoltage() function when called in setup, calculates the voltage and gives it in percentage, but it doesn't light up the strip except when percentage is <10 where it executes code as expected. In loop, everything works.

The ESPNOWerror function works in both setup and loop.

My original for statement which doesn't work in setup, but works in loop.

if (percentage > 90 && percentage <= 100)
    {
        for(int l = 1; l<NUM_LEDS; l++)
        {
            leds[l] = CRGB (0,255,0);
        }
        FastLED.setBrightness(85);
        FastLED.show();
        delay(1000);
        FastLED.clear(true);
        FastLED.show();
    }

In the batteryVoltage() function, if the for statement is modified like this,

if (percentage > 90 && percentage <= 100)
    {
        for(int l = 1; l<NUM_LEDS; l++)
        {
            leds[l] = CRGB (0,255,0);
        FastLED.setBrightness(85);
        FastLED.show();
        } // in original code FastLED.setBrightness & FastLED.show were outside the loop.
        delay(1000);
        FastLED.clear(true);
        FastLED.show();
    }

the code works in both setup and loop, however there is a small but imperceptible delay between each LED. I can live with that, but I would like to know why/ where my mistake is in the original code.
I know I am making some silly mistake, but I'm unable to find out what mistake that is.

Full code

#include <FastLED.h>

#define NUM_LEDS 14

#define DATA_PIN D3 //data pin of LED Strip

#define inBuiltLED D4

CRGB leds[NUM_LEDS];

void setup()
{
    pinMode(inBuiltLED, OUTPUT);
    FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
        ESPNOWerror();  //works as expected in setup
        batteryVoltage(); //doesn't work in setup, but works in loop
}

void loop()
{}
 void batteryVoltage()
{
    float calibration = 0.20;// error b/w multimeter and voltage divider
    
    float analogValue = analogRead(A0);
   
    float voltage = (((analogValue * (3.3 / 1024) * 1.373)) + calibration); //1.373 is given by R1+R2/R2
    
    int percentage = mapfloat (voltage, 3.3, 4.2); //returns voltage as % with min 3.3V as 0 and max 4.2 V as 100
    
    percentage =92; //for debugging
    if (percentage >= 100)
    {
        percentage = 100; // ensure % value not above 100
    }
    
    if (percentage <= 0)
    {
        percentage = 1; // ensure % value not below 1
    }
    
    //light up LED strip based on percentage
    if (percentage > 90 && percentage <= 100)
    {
        for(int l = 1; l<NUM_LEDS; l++)
        {
            leds[l] = CRGB (0,255,0);
        }
        FastLED.setBrightness(85);
        FastLED.show();
        delay(1000);
        FastLED.clear(true);
        FastLED.show();
    }
    
    else if (percentage > 80 && percentage <= 90)
    {
        for(int l = 1; l<12; l++)
        {
            leds[l] = CRGB (0,255,0);
        }
        FastLED.setBrightness(85);
        FastLED.show();
        delay(1000);
        FastLED.clear(true);
        FastLED.show();
    }
    
    else if (percentage > 70 && percentage <= 80)
    {
        for(int l = 1; l<11; l++)
        {
            leds[l] = CRGB (0,255,0);
        }
        FastLED.setBrightness(85);
        FastLED.show();
        delay(1000);
        FastLED.clear(true);
        FastLED.show();
    }
    
    else if (percentage > 60 && percentage <= 70)
    {
        for(int l = 1; l<9; l++)
        {
            leds[l] = CRGB (0,255,0);
        }
        FastLED.setBrightness(85);
        FastLED.show();
        delay(1000);
        FastLED.clear(true);
        FastLED.show();
    }
    
    else if (percentage > 50 && percentage <= 60)
    {
        for(int l = 1; l<8; l++)
        {
            leds[l] = CRGB (204,255,0);
        }
        FastLED.setBrightness(85);
        FastLED.show();
        delay(1000);
        FastLED.clear(true);
        FastLED.show();
    }
    else if (percentage > 40 && percentage <= 50)
    {
        for(int l = 1; l<7; l++)
        {
            leds[l] = CRGB (255,255,0);
        }
        FastLED.setBrightness(85);
        FastLED.show();
        delay(1000);
        FastLED.clear(true);
        FastLED.show();
    }
    else if (percentage > 30 && percentage <= 40)
    {
        for(int l = 1; l<6; l++)
        {
            leds[l] = CRGB (255,255,0);
        }
        FastLED.setBrightness(85);
        FastLED.show();
        delay(1000);
        FastLED.clear(true);
        FastLED.show();
    }
    else if (percentage > 20 && percentage <= 30)
    {
        for(int l = 1; l<4; l++)
        {
            leds[l] = CRGB (255,0,0);
        }
        FastLED.setBrightness(85);
        FastLED.show();
        delay(1000);
        FastLED.clear(true);
        FastLED.show();
    }
    else if (percentage > 10 && percentage <= 20)
    {
        for(int l = 1; l<3; l++)
        {
            leds[l] = CRGB (255,0,0);
        }
        FastLED.setBrightness(85);
        FastLED.show();
        delay(1000);
        FastLED.clear(true);
        FastLED.show();
    }
    else if (percentage > 0 && percentage <= 10)
    {
        int i = 0;
        while (i < 10)
        {
            leds[1] = CRGB (255,0,0);
            FastLED.setBrightness(85);
            FastLED.show();
            delay(200);
            FastLED.clear(true);
            FastLED.show();
            delay(200);
            i++;
       }
    }
}

float mapfloat (float IN, float V_min, float V_max)
{
        return (((IN-V_min) / (V_max-V_min)) * 100);
}
    
            
void ESPNOWerror()
{
        int i = 0;
        while (i < 10)
        {
            for(int l = 1; l<NUM_LEDS; l++)
            {
                leds[l] = CRGB (255,0,0);
            }   
            FastLED.setBrightness(127);
            FastLED.show();
            delay(200);
            FastLED.clear(true);
            FastLED.show();
            delay(200);   
            i++;
        }
}

Thank you

Why not zero?
Why the "show" for every iteration?

Because the first LED (LED0) is covered in heatshrink.

I didn't want to put this here, but for some reason, when putting it here the code works.
Whereas, when show is after the array, the LED strip doesnt work in setup but works in loop.

Your mistake was that you put

inside the for cycle

Redraw it that way:

Apologies if I was not clear.

This code doesn't work when called in setup, but works in loop. This is how my original code is.

However, FastLED.show(); inside the for loop works and the strip lights up with a very very small delay (hardly a few ms) between each LED.

Please see my original code too. I have written it the way you have corrected it and that doesn't work.
Thank you for replying.
Edit : Have edited original post hoping to offer better clarity on my problem.

Perhaps it is not related to problem, but your batteryVoltage() function is too long and inefficient.

It contains ten almost the same code blocks. Never write the programs this way - by copying the same code many times. You can move similar code to another block after conditions:


void batteryVoltage()
{
    float calibration = 0.20;// error b/w multimeter and voltage divider
    
    float analogValue = analogRead(A0);
   
    float voltage = (((analogValue * (3.3 / 1024) * 1.373)) + calibration); //1.373 is given by R1+R2/R2
    
    int percentage = mapfloat (voltage, 3.3, 4.2); //returns voltage as % with min 3.3V as 0 and max 4.2 V as 100
    
    percentage =92; //for debugging
    if (percentage >= 100)
    {
        percentage = 100; // ensure % value not above 100
    }
    
    if (percentage <= 0)
    {
        percentage = 1; // ensure % value not below 1
    }
    
    //light up LED strip based on percentage
    if (percentage > 10 && percentage <= 100)  {
      int led_cnt =0;
      if (percentage > 90 && percentage <= 100)  led_cnt = NUM_LEDS;
      else if (percentage > 80 && percentage <= 90) led_cnt = 12;
      else if (percentage > 70 && percentage <= 80) led_cnt = 11;
      else if (percentage > 60 && percentage <= 70) led_cnt = 9;
      else if (percentage > 50 && percentage <= 60) led_cnt = 8;
      else if (percentage > 40 && percentage <= 50) led_cnt = 7;
      else if (percentage > 30 && percentage <= 40) led_cnt = 6;
      else if (percentage > 20 && percentage <= 30) led_cnt = 4;
      else if (percentage > 10 && percentage <= 20) led_cnt = 3;
      
      for(int l = 1; l<led_cnt; l++)
        {
            leds[l] = CRGB (0,255,0);
        }
        FastLED.setBrightness(85);
        FastLED.show();
        delay(1000);
        FastLED.clear(true);
        FastLED.show();
     }
     else if (percentage > 0 && percentage <= 10) 
    {
        int i = 0;
        while (i < 10)
        {
            leds[1] = CRGB (255,0,0);
            FastLED.setBrightness(85);
            FastLED.show();
            delay(200);
            FastLED.clear(true);
            FastLED.show();
            delay(200);
            i++;
       }
    }
}

@b707, thank you for the tip. I'm learning to write and tips like these help me make my code writing skills better.
Truly appreciate it.
Thank you.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.