RGB LED strip manual, random, and fade control

Hopefully this helps someone. A super useful project for LED strip lights. Great for any application that uses RGB strip lights. I’ve used this behind my TV and now will be using it in my shower as some cool custom mood lighting.

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

  Blynk lets you create beautiful drag-and-drop visual interfaces
  for your projects in 5 minutes. And it works with almost every
  hardware out there.

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

  Blynk library is licensed under MIT license
 *************************************************************

  You’ll need:
   - Blynk App (download from AppStore or Google Play)
   - Arduino UNO or similar microcontroller board
   - Decide how to connect to Blynk
     (USB, Ethernet, Wi-Fi, Bluetooth, ...)

  There is a bunch of great example sketches included to show you how to get
  started. Think of them as LEGO bricks  and combine them as you wish.
  For example, take the Ethernet Shield sketch and combine it with the
  Servo example, or choose a USB sketch and add a code from SendData
  example.
 *************************************************************

 Let’s control your RGB LED strip light with Blynk!
 In this example we'll use ESP8266-01, 3.3v power supply, 12v RGB LED strip, 12v power supply, npn transistors, and resistors of appropriate values

 10 Step guide:

  1. Connect ESP8266 to 3.3v power supply
  2. Connect LED power lead to 12v power supply and connect both power supply grounds
  3. Connect each colors' ground lead through individual NPN transistors to ground
  4. Attach TX pin of esp to red led ground lead transistors' base through appropriate resistor
  5. Attach gpio2 pin of esp to green led ground lead transistors' base through appropriate resistor
  6. Attach gpio0 pin of esp to blue led ground lead transistors' base through appropriate resistor


 In the Blynk App:
  7. Create New Project
  8. Email yourself Auth Token. You can do it later at any time
  9. Add 2 Button Widgets and 3 Slider Widgets. Assign buttons to V0 and V4. Assign sliders to V1, V2, and V3
  10. Press Play icon. Enjoy Blynking!


 *************************************************************/


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


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

int redPin = 1;     //Create variable for esp8266 TX pin (gpio1)
int greenPin = 0;   //Create variable for esp8266 gpio0 pin
int bluePin = 2;    //Create variable for esp8266 gpio2 pin

int fadeButtonState;          //Variable to store the state of button attached to V0
int lastFadeButtonState = 0;  //Variable to store the prior state of button attached to V0
int redBrightness = 0;            //
int redFadeAmount = random(1,6);  //
int greenBrightness = 0;          //   Variables to set starting brightness and speed of fading for LED strip
int greenFadeAmount = random(1,6);//
int blueBrightness = 0;           //
int blueFadeAmount = random(1,6); //


char auth[] = "AuthTokenHere";
char ssid[] = "YourWiFiName";
char pass[] = "WiFiPassword";


BlynkTimer timer; //Create timer object

void setup()
{
   // Debug console
   //Serial.begin(9600);
   
   Blynk.begin(auth, ssid, pass);
   timer.setInterval(15,randomFade);  //Set time interval to run the randomFade function every x milliseconds. Adjust as necessary to see faster or slower fade.
}

void loop()
{
   Blynk.run();
   timer.run();   //Continuously run the timer 
}

BLYNK_WRITE(V0) //Fade Button
{
   fadeButtonState = param.asInt();  //Stores the state of button each time V0 is pressed
}

void randomFade()  //function called by the timer to repeat every x milliseconds
{
  
   if(fadeButtonState == 0 && lastFadeButtonState == 1)  //Turns the LED strip off when the button goes from 'ON' to 'OFF'
      {
         analogWrite(redPin,0);
         analogWrite(greenPin,0);
         analogWrite(bluePin,0);
         redBrightness = random(0,1023);
         greenBrightness = random(0,1023);       //Resets starting color for the next time button turns 'ON'
         blueBrightness = random(0,1023);
         lastFadeButtonState = fadeButtonState;  //Stores the latest state of the button
      }

   if(fadeButtonState == 1)        //Begins to fade LED strip when V0 button is 'ON'
      {
         analogWrite(redPin, redBrightness);
         redBrightness += redFadeAmount;     //Steps up or down the brightness by the random amount stored in the FadeAmount variable

         analogWrite(greenPin, greenBrightness);
         greenBrightness += greenFadeAmount;

         analogWrite(bluePin, blueBrightness);
         blueBrightness += blueFadeAmount;

         ////////////////////////////////////

         if (redBrightness > 1023)   //Reverses red fade direction from brighter to dimmer after changing fade speed
            {
               redBrightness -= redFadeAmount;
               redFadeAmount = random(1,6);
               redFadeAmount = -redFadeAmount;  //Changes fade amount variable to a negative number to begin dimming the LED
            }

         if (redBrightness <= 0)     //Reverses red fade direction from dimmer to brighter after changing fade speed
            {
               redBrightness -= redFadeAmount;
               redFadeAmount = random(1,6);  
            }

         /////////////////////////////////////   

         if (greenBrightness > 1023)  //Reverses green fade direction from brighter to dimmer after changing fade speed
            {
               greenBrightness -= greenFadeAmount;
               greenFadeAmount = random(1,6);
               greenFadeAmount = -greenFadeAmount;
            }

         if (greenBrightness <= 0)    //Reverses green fade direction from dimmer to brighter after changing fade speed
            {
               greenBrightness -= greenFadeAmount;
               greenFadeAmount = random(1,6);  
            }

         /////////////////////////////////////

         if (blueBrightness > 1023)   //Reverses blue fade direction from brighter to dimmer after changing fade speed
            {
               blueBrightness -= blueFadeAmount;
               blueFadeAmount = random(1,6);
               blueFadeAmount = -blueFadeAmount;
            }

         if (blueBrightness <= 0)     //Reverses blue fade direction from dimmer to brighter after changing fade speed
            {
               blueBrightness -= blueFadeAmount;
               blueFadeAmount = random(1,6);  
            }

         /////////////////////////////////////
               
         lastFadeButtonState = fadeButtonState;  //Stores the latest state of the button
      }
}

BLYNK_WRITE(V1) //Red Slider - Manually adjust red LEDs in strip
{
   int redValue = param.asInt();  // Stores the value of the slider on V1 as an integer
   analogWrite(redPin, redValue); //Writes value of 'redValue' (set by V1) as analog value to redPin (esp8266 TX pin)
}

BLYNK_WRITE(V2) //Green Slider - Manually adjust green LEDs in strip
{
   int greenValue = param.asInt();   // Stores the value of the slider on V2 as an integer
   analogWrite(greenPin, greenValue);//Writes value of 'greenValue' (set by V2) as analog value to greenPin (esp8266 gpio2 pin)
}

BLYNK_WRITE(V3) //Blue Slider - Manually adjust blue LEDs in strip
{
   int blueValue = param.asInt();    // Stores the value of the slider on V3 as an integer
   analogWrite(bluePin, blueValue);  //Writes value of 'blueValue' (set by V3) as analog value to bluePin (esp8266 gpio0 pin)
}

BLYNK_WRITE(V4) //Random Color Button  -  Turns LED strip to random color when 'ON' and turns LED strip off when 'OFF'
{
   int randomButtonState = param.asInt();  //Stores state of button (ON or OFF)
   
   int randomRed = random(0,1023);    //
   int randomGreen = random(0,1023);  //Selects random brightness for each color
   int randomBlue = random(0,1023);   //

   if (randomButtonState == 0)        //Turns LED strip off when 'OFF'
      {
         analogWrite(redPin,0);
         analogWrite(greenPin,0);
         analogWrite(bluePin,0);
      }

   if (randomButtonState == 1)        //Turns LED strip to random color when 'ON'
      {
         analogWrite(redPin,randomRed);
         analogWrite(greenPin,randomGreen);
         analogWrite(bluePin,randomBlue);  
      }
}

3 Likes