120 LED FADE IN & FADE OUT with ESP8266-01

Hello Blynkers ,

I am asking if there is a better attempt for FADE IN or FADE OUT .
This part of my sketch is working fine but I must have a least a delay of 300 ms.
is that correct ?

void Fade_IN(){ //fade in from min to max in increments of 20
    Blynk.virtualWrite(vPIN_FADE_IN, 1);
  for (double FadeValue = 0 ; FadeValue <= 1043.46; FadeValue += 20.46) {
    analogWrite(LED_120, FadeValue);
    delay(400);  //wait for 400 ms to see the dimming effect
    Blynk.virtualWrite(vPIN_LED,int (FadeValue/4));

    terminal.println();
    terminal.println();
    terminal.println();
    terminal.println("Manual MODE is ON");
    terminal.println("Fade-IN in PROGRESS ");
    terminal.print("Fade-IN = ");
    terminal.print(FadeValue*100/1023);
    terminal.println(" % ");
    terminal.println();
    terminal.println();
    terminal.flush();

    Blynk.virtualWrite(vPIN_STEP_V,FadeValue);
    Blynk.virtualWrite(vPIN_SLIDER,FadeValue);      
      }
    terminal.println("Fade-IN COMPLETED ");
    terminal.print("Fade-IN = 100.00 ");
    terminal.println(" % ");
    terminal.println();
    terminal.println();
    terminal.flush();
    Blynk.virtualWrite(vPIN_FADE_IN, 0);
    }

Yes, there is a better way using SimpleTimer!

#include <SimpleTimer.h>

void doNothing();

void loop()
{
  for(int i=0;i<255;i++)
  {
    Serial.println("Fading ...");
    hourStrip[10] = CRGB(i,i,i);
    //delay(400);
    timer.setTimeout(400, doNothing);
  }
}

void doNothing()
{
}

That should do the trick without blocking. Delay() halts the Arduino CPU (or ESP in this case). Blynk will surely disconnect that way.

hi
@Lichtsignaal
the LED I am using are simple white LEDs not RGB
also I think Blynk timer can do the same job

BlynkTimer timer;

Till now i don’t have this issue . I am using a local server.
as you mentioned arrays my question is how to use it at different light intensity ?
i mean that PWM is not linearly proportional to emitted light

  for (double FadeValue = 0 ; FadeValue <= 1043.46; FadeValue += 20.46) {
    analogWrite(LED_120, FadeValue);

can I use analogWrite with array and how I can do it.(0 ----> 1023)
Thanks

1 Like

SimpleTimer is the same as BlynkTimer.

The RGB led’s don’t matter, the principle is the same. PWM has calculations for duty cycle and how much light it emits. I bet the internet is full of that info :wink:

And yes, you can use analogWrite with that like you wrote. That should work fine :slight_smile:

Here is a better interpretation of LED Fade in & Fade out that may help someone .
also reducing the load on server without dropping your Blynk connection while these loops are running and that you’re having to wait until it reconnects

WidgetLED led1(vPIN_vv);

/* 
  FadeValue table to compensate for the nonlinearity of human vision.
  Used in brightness to make 'dimming' look more natural. 
  Exponential function used to create values below : 
  x from 0 - 255 : y = round(pow( 2.0, x+64/40.0) - 1)   
*/
 
const byte FadeValue[] = {
    0,   1,   1,   2,   2,   2,   2,   2,   2,   3,   3,   3,   3,   3,   3,   3,
    3,   3,   3,   3,   3,   3,   3,   4,   4,   4,   4,   4,   4,   4,   4,   4,
    4,   4,   4,   5,   5,   5,   5,   5,   5,   5,   5,   5,   5,   6,   6,   6,
    6,   6,   6,   6,   6,   7,   7,   7,   7,   7,   7,   7,   8,   8,   8,   8,
    8,   8,   9,   9,   9,   9,   9,   9,   10,  10,  10,  10,  10,  11,  11,  11,
    11,  11,  12,  12,  12,  12,  12,  13,  13,  13,  13,  14,  14,  14,  14,  15,
    15,  15,  16,  16,  16,  16,  17,  17,  17,  18,  18,  18,  19,  19,  19,  20,
    20,  20,  21,  21,  22,  22,  22,  23,  23,  24,  24,  25,  25,  25,  26,  26,
    27,  27,  28,  28,  29,  29,  30,  30,  31,  32,  32,  33,  33,  34,  35,  35,
    36,  36,  37,  38,  38,  39,  40,  40,  41,  42,  43,  43,  44,  45,  46,  47,
    48,  48,  49,  50,  51,  52,  53,  54,  55,  56,  57,  58,  59,  60,  61,  62,
    63,  64,  65,  66,  68,  69,  70,  71,  73,  74,  75,  76,  78,  79,  81,  82,
    83,  85,  86,  88,  90,  91,  93,  94,  96,  98,  99,  101, 103, 105, 107, 109,
    110, 112, 114, 116, 118, 121, 123, 125, 127, 129, 132, 134, 136, 139, 141, 144,
    146, 149, 151, 154, 157, 159, 162, 165, 168, 171, 174, 177, 180, 183, 186, 190,
    193, 196, 200, 203, 207, 211, 214, 218, 222, 226, 230, 234, 238, 242, 248, 255,
};

  BLYNK_WRITE(vPIN_FADE_IN) {
    for (int  i = 0 ; i <= 255; i += 16) {
    analogWrite(LED_120, FadeValue[i]*4);
    Blynk.virtualWrite(vPIN_vv,FadeValue[i]);
      }
    analogWrite(LED_120, 1023);
    Blynk.virtualWrite(vPIN_vv,FadeValue[255]);
    Blynk.virtualWrite(vPIN_FADE_IN,FadeValue[255]);
    }

  BLYNK_WRITE(vPIN_FADE_OUT) {
    for (int  i = 255 ; i >= 0; i -= 16) {
    analogWrite(LED_120, FadeValue[i]*4);
    Blynk.virtualWrite(vPIN_vv,FadeValue[i]);
      }
    analogWrite(LED_120, 0);
    Blynk.virtualWrite(vPIN_vv,FadeValue[0]);
    Blynk.virtualWrite(vPIN_FADE_OUT,FadeValue[0]);
    }

or better

const byte FadeValue[] = {0,3,4,6,8,11,15,20,27,36,48,63,83,110,146,193,255,};

  BLYNK_WRITE(vPIN_FADE_IN) {
    for (int  i = 0 ; i <= 16; i ++) {
    analogWrite(LED_120, FadeValue[i]*4);
    delay(40);  //wait for 40 ms to see the dimming effect
          }
    Blynk.virtualWrite(vPIN_vv,255);
    Blynk.virtualWrite(vPIN_FADE_IN,0);
    }

  BLYNK_WRITE(vPIN_FADE_OUT) {
    for (int  i = 16 ; i >= 0; i --) {
    analogWrite(LED_120, FadeValue[i]*4);
    delay(40);  //wait for 40 ms to see the dimming effect
         }
    Blynk.virtualWrite(vPIN_vv, 0);
    Blynk.virtualWrite(vPIN_FADE_OUT, 0);
    }