Knowledge / Fundamental Concepts Needed - ESP 32 RGB LED Strip Project

Hi All,

I need some help understanding whats going on. I can see I am missing a fundamental concept here, any helpful explanations would be appreciated.

I am making a LED Light and I have most of what i want but due to my limited understanding I cannot go further. My LED light is working as intended with Button for RED, GREEN, OFF, BRIGHTNESS, using a slider to light up x many LEDS and Using a Slider to Change Colour. Im super happy with all i have learnt, what i want to add is the Rainbow effect when I click a button.

So what I don’t understand is how can I get blynk to run a loop?

I started with something simple so i can learn.
Adding a button to Turn IT on RED - cool got it
Adding a Butting to turn it on Green - also good etc etc now i want to add a Rainbow effect and I can see the effect is not looping though!? - if I click my button on blynk many many times it will move through the effect slowly. why is it not running / looping that code. Usually you call the function in Void Loop but since its only Blynk.run(); how can I run the rainbow effect?
if I call the Function to the Void Main its run Continuously (thats Expected) how does Blynk control this?

I hope this makes sense. If you can help me understand thank you.
see below details

Hardware model ESP32 Dev Kit V1
communication type. WIFI
Smartphone OS + version 2.26.3
Blynk server
Blynk Library version 0.61

/*
Developer:      Kyle Jardine
Created Date:   2020-05-15
Modified Date:  2020-05-25
*/
//Libraries
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include "FastLED.h"
#define NUM_leds 60
#define LED_TYPE    WS2812
#define COLOR_ORDER GRB
CRGB leds[NUM_leds];
#define DATA_PIN 2
int data= 255;
int r,g,b;
int BRIGHTNESS = 255;
int POT = 1;

//Blynk 
char auth[] = "XX";
char ssid[] = "XX";
char pass[] = "XX"; 

void setup()
{ 
  FastLED.clear();
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_leds).setCorrection(TypicalLEDStrip);
  FastLED.setBrightness(BRIGHTNESS);
}
 
void loop()
{
  Blynk.run();
}


//FUNCTIONS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
void simple_ON (){
   //FastLED.clear();
   for(int led = 0; led < NUM_leds; led++) { 
   leds[led] = CRGB::Red; 
        }
   FastLED.setBrightness(BRIGHTNESS);
   FastLED.show();
}

void simple_ON_2 (){
   //FastLED.clear();
   for(int led = 0; led < NUM_leds; led++) { 
   leds[led] = CRGB::Green; 
        }
   FastLED.setBrightness(BRIGHTNESS);
   FastLED.show();
}

void RUN() {    //NUmber of LEDDS
        int val = data;
        int numLedsToLight = map(val, 0, 1023, 0, NUM_leds);

        // First, clear the existing led values
        FastLED.clear();
        FastLED.setBrightness(BRIGHTNESS);
        for(int led = 0; led < numLedsToLight; led++) { 
            //CHange_COLOUR();
            leds[led] = CRGB::Blue;
        }
        FastLED.show();
    }

//Brightness Function
void BRIGHT(int BRIGHTNESS)
{
  int B = map(BRIGHTNESS, 0, 1023, 0, 255);
  FastLED.setBrightness(BRIGHTNESS);
  FastLED.show();
}

void Turn_Off() {
   //FastLED.clear();
   for(int led = 0; led < NUM_leds; led++) { 
   leds[led] = CRGB::Black; 
        }
   FastLED.show();
}

void CHange_COLOUR() {
    int val1 = data;
    int hue = map(val1, 0, 1023, 0, 255);
    for (int i = 0; i < NUM_leds; i++) {
    leds[i] = CHSV(hue, 255, BRIGHTNESS);      
  }
  FastLED.show();
}


void AWESOME() {
    //beatsin16 is a function on the FastLED library generating sinwave, (5) is bpm, (0,255) is value range.
    //value range will create the breathing effect 
    uint8_t gHue = 0; 
    int pos = beatsin16(5,0,192); // generating the sinwave 
    fill_solid(leds, NUM_leds, CHSV( gHue, 255, pos)); // CHSV (hue, saturation, value);
    FastLED.show();
    EVERY_N_MILLISECONDS(100) {gHue++;} 
    // shifting the HUE value by incrementing every millisecond this creates the spectrum wave
}


//BLYNK DIGITAL INPUTS
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
BLYNK_WRITE(V1) //read Button value   
{
 int V_1 = param.asInt();  // assigning incoming value from pin V1 to a variable
 if (V_1 == 1) {
  simple_ON() ;
 }
}


BLYNK_WRITE(V2) //read Button value   
{
 int V_2 = param.asInt();  // assigning incoming value from pin V1 to a variable
 if (V_2 == 1) {
  simple_ON_2() ;
 }
}


BLYNK_WRITE(V3) //read slider value   
{
data = param.asInt();  // assigning incoming value from pin V2 to a variable
RUN();
}

BLYNK_WRITE(V4) // Read Button Value for On and off 
{
  int V_4 = param.asInt(); 
  if(V_4 == 1){
    Turn_Off();
  }
}

BLYNK_WRITE(V5) //read slider value   
{
BRIGHTNESS = param.asInt();  // assigning incoming value from pin V2 to a variable
BRIGHT(BRIGHTNESS);
}

BLYNK_WRITE(V6) //read slider value   - Change Colour
{
data = param.asInt();  // assigning incoming value from pin V2 to a variable
CHange_COLOUR();
}

BLYNK_WRITE(V7) // Read Button Value for On and off 
{
  int V_7 = param.asInt(); 
  if(V_7 == 1){
    AWESOME();
  }
}

Crude method i use is setting a flag.

int flag = 1;

void setup() {
timer.setInterval(100, rainbow_effect); // change the timer interval according to your need
}

void rainbow_effect() {
if (flag == 0) {
// rainbow effect code goes here.
}
}

BLYNK_WRITE(V1) {
effect = param.asInt();
if (effect == 0) {
flag = 0;// this will allow the lines in the rainbow effect function
} else {
flag = 1; //this will block the lines in the rainbow effect function
}
}

This is just a snippet code. You need modify/add for your use.
There are definitely better ways of doing it i guess. But this should work.

Thank you - will try this today.
Do you still keep the main loop like this?

void loop()
{
  Blynk.run();
}

Yes. This doesn’t change.

100%
:wink:

void loop () {
Blynk.run();
timer.run(); // I forgot to mention this. 
}

Your loop should look like this…

1 Like

Thanks @Madhukesh - Your suggestion is working for me!
Need to think about this more - seems like a little bit of black magic.

Thank you so much, my objective has been achieved.
I can carry on with my project

1 Like