BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Analog RGB Led Strip Fade effects


#1

Hey,
I tried implementing a smart roof light at my house. I had successfully implemented it with the zeRGBa widget and also made it turn on and off using Alexa. Now I want help in added fade in and fade out effects during turn on and turn off. as delay blocks can’t be used here. Could someone help on how to go forward with it.

I have used a NodeMCU and couple of TIP31c mosfets to control them.

CODE


    /*************************************************************
      Download latest Blynk library here:
        https://github.com/blynkkk/blynk-library/releases/latest

      Blynk is a platform with iOS and Android apps to control
      Arduino, Raspberry Pi and the likes over the Internet.
      You can easily build graphic interfaces for all your
      projects by simply dragging and dropping widgets.

        Downloads, docs, tutorials: http://www.blynk.cc
        Sketch generator:           http://examples.blynk.cc
        Blynk community:            http://community.blynk.cc
        Social networks:            http://www.fb.com/blynkapp
                                    http://twitter.com/blynk_app

      Blynk library is licensed under MIT license
      This example code is in public domain.

     *************************************************************
      This example runs directly on NodeMCU.

      Note: This requires ESP8266 support package:
        https://github.com/esp8266/Arduino

      Please be sure to select the right NodeMCU module
      in the Tools -> Board menu!

      For advanced settings please follow ESP examples :
       - ESP8266_Standalone_Manual_IP.ino
       - ESP8266_Standalone_SmartConfig.ino
       - ESP8266_Standalone_SSL.ino

      Change WiFi ssid, pass, and Blynk auth token to run :)
      Feel free to apply it to any other example. It's simple!
     *************************************************************/

    /* Comment this out to disable prints and save space */
    #define BLYNK_PRINT Serial


    #include <ESP8266WiFi.h>
    #include <DNSServer.h>
    #include <WiFiManager.h>
    #include <ESP8266WebServer.h>
    #include <BlynkSimpleEsp8266.h>
    #include <WiFiUdp.h>
    #include <ArduinoOTA.h>
    //#include <SimpleTimer.h>
    #include "WemoSwitch.h"
    #include "WemoManager.h"
    #include "CallbackFunction.h"
    #define VPIN V2  //Use a unique virtual pin for each device using the same token / dashboard

    char auth[] = "******"; //Get token from Blynk

    //on/off callbacks
    void lightOn();
    void lightOff();

    WemoManager wemoManager;
    WemoSwitch *light = NULL;

    boolean LampState = 0;
    boolean SwitchReset = true;   //Flag indicating that the hardware button has been released
    int switchState = 0;

    const int RelayPin = D4;      //Relay switching pin. Relay is pin 12 on the SonOff

    const int RedPin1 = D0; 
    const int RedPin2 = D5; 
    const int GreenPin1 = D1; 
    const int GreenPin1 = D6;
    const int BluePin1 = D2;
    const int BluePin2 = D7;


    SimpleTimer timer;

    int Red = 0;
    int Green = 0;
    int Blue = 0;
    // Your WiFi credentials.
    // Set password to "" for open networks.
    char ssid[] = "****";
    char pass[] = "*****";


    BLYNK_WRITE(V4) // zeRGBa assigned to V4
    {
      // get a RED channel value
      Red = param[0].asInt();
      // get a GREEN channel value
      Green = param[1].asInt();
      // get a BLUE channel value
      Blue = param[2].asInt();
     
      if (LampState)
      { analogWrite(RedPin1, Red);
        analogWrite(GreenPin1, Green);
        analogWrite(BluePin1, Blue);

        analogWrite(RedPin2, Red);
        analogWrite(GreenPin2, Green);
        analogWrite(BluePin2, Blue);
      }
    }
    void setup()
    {
      Serial.begin(115200);
      pinMode(RelayPin, OUTPUT);
      WiFiManager wifi;   //WiFiManager intialization.
      wifi.autoConnect("Roof Light"); //Create AP, if necessary

      wemoManager.begin();
      // Format: Alexa invocation name, local port no, on callback, off callback
      light = new WemoSwitch("Roof Light", 102, lightOn, lightOff);
      wemoManager.addDevice(*light);

      pinMode(RelayPin, OUTPUT);
      pinMode(LED, OUTPUT);
      //pinMode(TacSwitch, INPUT_PULLUP);
      delay(10);
      digitalWrite(RelayPin, LOW);
      digitalWrite(LED, LOW);

      ArduinoOTA.setHostname("Roof Light");
      Blynk.config(auth);
      ArduinoOTA.begin();

      Blynk.virtualWrite(VPIN, HIGH);
      Blynk.syncVirtual(VPIN);

    }

    BLYNK_WRITE(VPIN) {
      if (switchState !=  param.asInt()) {
        if (LampState == 1) {
          lightOff();
        }
        else {
          lightOn();
        }
        switchState = param.asInt();
      }
    }




    void lightOn() {


      Serial.println("Switch 1 turn on ...");
      digitalWrite(RelayPin, HIGH);
     // Setting it to a specific colour.
      analogWrite(RedPin1, 1023);// Red
      analogWrite(RedPin2, 1023);//

      analogWrite(GreenPin1, 425);
      analogWrite(GreenPin2, 425);// Green

      analogWrite(BluePin1, 0);
      analogWrite(BluePin2, 0);// Blue

      LampState = 1;
      Blynk.virtualWrite(VPIN, HIGH);     // Sync the Blynk button widget state
    }
    void lightOff() {
      digitalWrite(RelayPin, LOW);

      Serial.println("Switch 1 turn off ...");

      analogWrite(RedPin1, 0);// Red
      analogWrite(RedPin2, 0);//

      analogWrite(GreenPin1, 0);
      analogWrite(GreenPin2, 0);// Green

      analogWrite(BluePin1, 0);
      analogWrite(BluePin2, 0);// Blue
      LampState = 0;
      Blynk.virtualWrite(VPIN, LOW);      // Sync the Blynk button widget state
    }
    void ToggleRelay() {
      LampState = !LampState;

      if (LampState) {
        lightOn();
      }
      else lightOff();
    }


    void loop()
    {
      wemoManager.serverLoop();
      Blynk.run();
      ArduinoOTA.handle();
      
    }

#2

You are correct that delay() is a bad choice… however, timers can be used to do very similar effects.

http://docs.blynk.cc/#blynk-firmware-blynktimer

This is a very crude but functional example…

int Fadelvl;

BLYNK_WRITE(V39) {  // Button Widget on V39, set to switch
  int LEDFadeFlag = param.asInt();
  if (LEDFadeFlag == 1) {
    FadeLvl = 0;
    timer.setTimer(15, FadeLEDUP, 255);  // runs designated function every 15ms for 255 times, then stops
  } else {
    FadeLvl = 256;
    timer.setTimer(15, FadeLEDDOWN, 255);  // runs designated function every 15ms for 255 times, then stops
  }
}

void FadeLEDUP()
{
  analogWrite(6, FadeLvl);  // LED on pin 6
  FadeLvl++;
}

void FadeLEDDOWN()
{
  analogWrite(6, FadeLvl);  // LED on pin 6
  FadeLvl--;
}

#3

@Gunner Thank you


#4

Because of the way your eyes preceve light brightness, it usually looks better if you dim/brighten the leds on a logarithmic scale. It’s quite a bit more involved to write the code to do that, so definitely try what gunner has suggested first.


#5

Sorry,
but do I have to include a library for the timer and if yes which one?
Or how it runs?


#6

@Hannes.P Are you asking about BlynkTimer? It is built in… but you do need to set it up as in the example shown in the documents

http://docs.blynk.cc/#blynk-firmware-blynktimer

And I have a some topics about timers here…