RGB Fading How can I work better?


// Wemos D1 mini + RGB LED

#define BLYNK_PRINT Serial


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

char auth[] = "YourAuthToken";

char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";

    
    int Red = 15;           
    int Green = 13;           
    int Blue = 12;



void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
}

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



BLYNK_WRITE(V1)
{
  int pinValue = param.asInt();
if (pinValue==1){
  

for(int i=0; i<=50; i++)
{analogWrite(Red, i);
     delay(4);   
}

for(int i=0; i<=150; i++)
{analogWrite(Green, i);
     delay(4);   
}

for(int i=0; i<=255; i++)
{analogWrite(Blue, i);
     delay(4);   
}
} else {//break
  } 

}

It would help if you explained a bit more about what you are trying to achieve, and what ā€˜better’ would look like from your perspective.

Is this your entire code? I’m asking because you haven’t defined the R, G & B pins as outputs in your void setup. I guess it could work without, I’ve never tried it.
Also, if you were using an Arduino then the PWM values for each output pin go from 0-255, but with an ESP8266/Wemos they go 0-1024 (or should that be 254 and 1023?), so your LED will only fade up to 25% brightness at best with your current code.

The way that you’re doing your fade up at the moment is to do the red first, followed by the green then the blue, which I guess doesn’t look very pretty.
If you were fading all from zero to the same number (1023 for example) then you could do red up by one step, followed by green, then blue - all within the same For/Next loop.
I’m guessing that you’re fading up to different brightnesses because that gives the colour you need, but that’s just a guess because of your lack of background information.

Depending on how you’ve connected the LED to your board, you could use resistors of different values on the three different pins to achieve the colour you want when all three outputs are at 1024. This would allow you to do a single For/Next to your desired colour.

If you’re looking for more flexibility then maybe look at sliders for each colour, or using the ZeRGBa widget, but these wouldn’t give you the automatic fade-up.

Also, be careful of the delay() in each of your loops, especially if you’re increasing the number of For/Next iterations to 1023.

Pete.

Figure 255 is just an example
I use a transistor tip 31 with 1k resistor + rgb streb led
The faded color appears red then green then blue
I would like to view the faded at once
An orange example uses the following values
Red 950
Green 100
Blue 0

So, using your example of

you’d need to create a loop that went around 951 times (0-950). You need 9.5 times more red than green, and no blue. Every time you go around the loop once you increment the red by 1 and after you’ve been around 9.5 times you increment the green by 1 as well. If you’re using integers then you can’t do the .5 bit, but using floating point variables isn’t a great idea in this scenario, so live with the rounding and do 10:1 steps.

To make it dynamic you could do this a variety of ways, but I’d probably start by finding the biggest number out of the R, G & B then dividing the other colours into that number to give me the step ratios. I’d probably always make the loop run 1024 times for simplicity and increment each of the colours when that colour’s step increment value was met.
Quite a bit of coding to give you the desired effect, but worth it I guess.

This isn’t really a Blynk issue, except that you need to take care that the fade code doesn’t block the Blynk run cycle, so you might need to look at the use of timers (not really my area of speciality, as I don’t use Blynk in the same way as most people, so never need to run Blynk code on my Wemos).

Pete.

Sorry I do not speak English very well

All this seems useless
Do you have the code?

:frowning:

Search Google. There can be hundreds of different ways of fading RGB LEDs. What you are looking for is NOT Blynk specific.

I searched for a long time and tried many libraries but did not work properly thanks for help

Sorry I took the time to reply then!

No, it’s not me that needs to fade the LEDs. You either need to write and test it yourself and confirm that it meets your requirements, or get someone to do that for you. This forum probably isn’t the place to find that person, because as we’ve pointed out, this isn’t a Blynk issue.

Pete.

I work it is not this isn’t a Blynk issue.
but maybe someone did a similar job but in a different way

https://www.google.com/search?newwindow=1&ei=Fap1WpqhC4bojwOgrJiICg&q=arduino+ESP8266+rgb+color+cycle+faders

Now, if you want to use a Blynk Widget to control your RGB LED, try the zeRGBa.

I don’t know what happened to the Example of it… I am sure there was one at one time…

But I have done a few things with it myself using both a Physical RGB and a ā€œVirtual RGBā€ LED Widget, three sliders, a Labeled Display and the zeRGBa.

This is running on an Arduino, so adjustments will have to be made for the ESP8266; as in the aforementioned 0-1023 for PWM. Also my RGB LED is common cathode (GND) and directly connected to pins with current limiting resistors.

#define RedP 15  // Set RED RGB pin.
#define GrnP 13  // Set GREEN RGB pin.
#define BluP 12  // Set BLUE RGB pin.
WidgetLED vRGB(V9);  // Set virtual RGB widget.
int rrr, ggg, bbb;  // Set RED BLUE GREEN channels

// In setup
pinMode(RedP, OUTPUT);  // Set RED on RGB LED.
pinMode(GrnP, OUTPUT);  // Set GREEN on RGB LED.
pinMode(BluP, OUTPUT);  // Set BLUE on RGB LED.



//===== RGB Sliders - BLYNK Functions =====
BLYNK_WRITE(V20)  // RED slider.
{
  rrr = param.asInt(); // get a RED channel value.
  Blynk.virtualWrite(V23, rrr);  // Red LED intensity
  RGBprocess();  // Goto pin data to HEX conversion funtion.
}  // END Blynk Function

BLYNK_WRITE(V21)  // GREEN slider.
{
  ggg = param.asInt(); // get a GREEN channel value.
  Blynk.virtualWrite(V24, ggg);  // Green LED intensity
  RGBprocess();  // Goto pin data to HEX conversion funtion.
}  // END Blynk Function

BLYNK_WRITE(V22)  // BLUE slider.
{
  bbb = param.asInt(); // get a BLUE channel value.
  Blynk.virtualWrite(V25, bbb);  // Blue LED intensity
  RGBprocess();  // Goto pin data to HEX conversion funtion.
}  // END Blynk Function



//===== zeRGBa Widget  - BLYNK Function =====
BLYNK_WRITE(V2)
{
  rrr = param[0].asInt(); // get a RED channel value.
  ggg = param[1].asInt(); // get a GREEN channel value.
  bbb = param[2].asInt(); // get a BLUE channel value.
  Blynk.virtualWrite(V20, rrr);  // Red slider position.
  Blynk.virtualWrite(V23, rrr);  // Red LED intensity.
  Blynk.virtualWrite(V21, ggg);  // Green slider position.
  Blynk.virtualWrite(V24, ggg);  // Green LED intensity.
  Blynk.virtualWrite(V22, bbb);  // Blue slider position.
  Blynk.virtualWrite(V25, bbb);  // Blue LED intensity.
  Blynk.run();
  RGBprocess();
}  // END Blynk Function



//===== Physical RGB LED Control and HEX conversion =====
void RGBprocess() {  // Pin data to HEX conversion funtion.
  analogWrite(RedP, rrr);  // Write to RED RGB pin.
  analogWrite(GrnP, ggg);  // Write to GREEN RGB pin.
  analogWrite(BluP, bbb);  // Write to BLUE RGB pin.
  String strRED = String(rrr, HEX);  // Convert RED DEC to HEX.
  if (rrr < 16) {
    strRED = String("0" + strRED);  // Buffer with 0 if required.
  }  // END if
  String strGRN = String(ggg, HEX);  // Convert GREEN DEC to HEX.
  if (ggg < 16)  {
    strGRN = String("0" + strGRN);  // Buffer with 0 if required.
  }  // END if
  String strBLU = String(bbb, HEX);  // Convert BLUE DEC to HEX.
  if (bbb < 16)  {
    strBLU = String("0" + strBLU);  // Buffer with 0 if required.
  }  // END if
  String HEXstring = String("#" + strRED + strGRN + strBLU);  // Combine HEX fragments.
  Blynk.run();
  HEXstring.toUpperCase();  // Change HEX value to all upper case for ease of visuals.
  Blynk.setProperty(V8, "color", HEXstring);  // Change background colour of HEX Data Label.
  Blynk.virtualWrite(V8, HEXstring);  // Display HEX data.
  Blynk.setProperty(V9, "color", HEXstring);  // Send formatted HEX colour to vRGB.
  Blynk.virtualWrite(V9, 255);  // Activate vRGB.
}  // END Blynk Function

I think what the OP was trying to achieve was to be able to define R, G & B values (maybe via sliders or ZeRGBa) then have a way of fading up to those values when the LED strip is turned on.

I have a similar setup at home with LED strips that come on at pre-defined times and it occurred to me recently that fading up in brightness would be nicer than suddenly switching on. I kicked the idea around in my head for a while and realised that it’s not that easy when you really want to maintain the correct colour ratios through the fade up. I did a quick google search at the time and didn’t see any pre-built code, so added it to my ā€œthings to look at on a rainy dayā€ list.
I’ll be back home in the UK soon, so I guess there’ll be plenty of rainy days on the horizon, but it’s a slightly different scenario for me as I use MQTT messages to set the R, G & B values and another to handle the on/off bit. Handling the fade via MQTT isn’t the way to go, so I’d need to do it via code running on the Wemos, but first reading the MQTT message values then using those for the parameters of the fade calculation.
I doubt the OP would have the skills to take anything I wrote and modify it to work for his needs.

Pete.

Based on answers and begging so far… I agree :wink:

I have done some LED fading routines with BlynkTimer, but it can be quite code intensive, *3 for RGB. But that simple Google search link showed dozens of options… and I have tried many others myself in the past.

I guess I am biased… I figure if I had to actually work at self learning ā€œprogramming how toā€, so should others :smiley:

1 Like

Thanks I found the Solution for this

AH… it looks like you are trying to code a simulated flame?