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