RGB Lights not fading when using the new Blynk

Hey Blynkers,

I need some advice and help. I used the following code and my RGB light fades between all the colors slowly. Looks awesome. But when I try to convert it to Blynk, it just flashes quickly, repeatedly. I think my problem lies with the timers, just can’t figure out what to do. I have not even started using the app , as I want it to continuously run when on, so no virtual pins have been created.

OLD WORKING CODE:


const int red = D8; 
const int green = D7; 
const int blue = D6; 
int r = 255; /* red led value is temporally 255 and it will be the first led to light up */
int b; /* blue led value is temporally 0 */
int g; /* green led value is temporally 0 */
int t = 1000; /* "t" (time) 1000 milliseconds, feel free to change it */
void setup() {/* no setup */}

void loop() {

for (/* no initialization */; r>=0, b<255; b++, r--) /*red -> blue*/
{
analogWrite(red, r);
analogWrite(blue, b);
delay(t);
}
for (/* no initialization */; b>=0, g<255; g++, b--) /*blue -> green*/
{
analogWrite(blue, b);
analogWrite(green, g);
delay(t);
}
for (/* no initialization */; g>=0, r<255; r++, g--) /*green -> red*/
{
analogWrite(red, r);
analogWrite(green, g);
delay(t);
}
}

BLYNK CODE (NOT WORKING):


/* Fill-in your Template ID (only if using Blynk.Cloud) */
#define BLYNK_TEMPLATE_ID "XXXXXXXXXX"
#define BLYNK_DEVICE_NAME "XXXXXXXXXX"
#define BLYNK_AUTH_TOKEN "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
char auth[] = BLYNK_AUTH_TOKEN;
const char* ssid;
const char* pass;
#include <ESP8266WiFi.h>          //ESP STUFF
#include <BlynkSimpleEsp8266.h>   //BLYNK STUFF
#include <WiFiManager.h>          //https://github.com/tzapu/WiFiManager WiFi Configuration Magic
#include <Ticker.h>               //ALSO PART OF WIFI MANAGER
//CREATING OBJECTS:
BlynkTimer timer; // Creating a timer object
Ticker ticker;
WiFiManager wifiManager;
//TIME INT
int fade = 1000; //RGB FADE
/********************************** LDR LIGHT SENSOR SETTINGS START *********************/
//A0 = LDR = V4
#define ldr A0
int lx = analogRead(ldr);
/********************************** LDR LIGHT SENSOR SETTINGS END **********************/
/********************************** RGB SETTINGS START *********************/
//D6 - R            - V10
//D7 - G            - V11
//D8 - B            - V12 
//D6 & D7 & D8 = RGB = Smooth RGB Fade
const int red = D6; /* connected to PWM pin D6 */
const int green = D7; /* connected to PWM pin D7 */
const int blue = D8; /* connected to PWM D8 */
int r = 255; /* red led value is temporally 255 and it will be the first led to light up */
int b; /* blue led value is temporally 0 */
int g; /* green led value is temporally 0 */
//int sec = 50; /* "sec" (time) 1000 milliseconds, feel free to change it for rainbow */
/*************************************** RGB SETTINGS END **********************/
/*************************************** WIFI MANAGER STTINGS START ********************************/
void tick() { //Ticker...
  //toggle state
  int state = digitalRead(BUILTIN_LED); // get the current state of GPIO1 pin
  digitalWrite(BUILTIN_LED, !state); // set pin to the opposite state
}
void configModeCallback(WiFiManager * myWiFiManager) {
  Serial.println("Entered config mode");
  Serial.println(WiFi.softAPIP());
  Serial.println(myWiFiManager->getConfigPortalSSID());
  ticker.attach(0.2, tick);
}
/************************************ WIFI MANAGER SETTINGS END **********************************/
/*********************************** LDR LIGHT SENSOR FUNCTIONS START *********************/
//A0
void sendSensorLDR() {
  //int lx = analogRead(ldr);
  lx = map(lx, 0, 1023, 0, 100);
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V4, lx);
  Serial.print("\n");
  Serial.print("Light lx in % is:");
  Serial.println(lx);
  Serial.print("\n");
  (millis() / fade);
}
/************************************** LDR LIGHT SENSOR FUNCTIONS END ***********************/
/************************************** RGB RAINBOW START ***********************************/
void rainbowRGB() {
  if (lx <= 30); //If it is less than 30%, turn on Rainbow.
  {
    Serial.print("RGB MODE: \n");
    Serial.println("Rainbow\n");
    Serial.println(lx); //Mode: Rainbow
    //delay(sec);
    //Smooth Fade
    for ( /* no initialization */ ; r >= 0, b < 255; b++, r--) /*red -> blue*/ {
      analogWrite(red, r);
      analogWrite(blue, b);
      //delay(sec);
      (millis() / fade);
    }
    for ( /* no initialization */ ; b >= 0, g < 255; g++, b--) /*blue -> green*/ {
      analogWrite(blue, b);
      analogWrite(green, g);
      //delay(sec);
      (millis() / fade);
    }
    for ( /* no initialization */ ; g >= 0, r < 255; r++, g--) /*green -> red*/ {
      analogWrite(red, r);
      analogWrite(green, g);
      //delay(sec);
      (millis() / fade);
    }
    for ( /* no initialization */ ; r >= 0, b < 255; b++, r--) /*red -> blue*/ {
      analogWrite(red, r);
      analogWrite(blue, b);
      //delay(sec);
      (millis() / fade);
    }
    for ( /* no initialization */ ; b >= 0, g < 255; g++, b--) /*blue -> green*/ {
      analogWrite(blue, b);
      analogWrite(green, g);
      //delay(sec);
      (millis() / fade);
    }
    for ( /* no initialization */ ; g >= 0, r < 255; r++, g--) /*green -> red*/ {
      analogWrite(red, r);
      analogWrite(green, g);
      //delay(sec);
      (millis() / fade);
    }
    for ( /* no initialization */ ; r >= 0, b < 255; b++, r--) /*red -> blue*/ {
      analogWrite(red, r);
      analogWrite(blue, b);
      //delay(sec);
      (millis() / fade);
    }
    for ( /* no initialization */ ; b >= 0, g < 255; g++, b--) /*blue -> green*/ {
      analogWrite(blue, b);
      analogWrite(green, g);
      //delay(sec);
      (millis() / fade);
    }
    for ( /* no initialization */ ; g >= 0, r < 255; r++, g--) /*green -> red*/ {
      analogWrite(red, r);
      analogWrite(green, g);
      //delay(sec);
      (millis() / fade);
    }
  }
}
/************************************** RGB RAINBOW END ***************************************/
void setup() {
  // Debug console
  Serial.begin(9600);
  /*************************** WIFI MANAGER SETUP START ********************************/
  //START WIFI MANAGER: Declared object above: WiFiManager wifiManager;                                                                 
  ticker.attach(0.6, tick);
  wifiManager.setTimeout(180);
  wifiManager.setAPCallback(configModeCallback);
  if (!wifiManager.autoConnect("XXXXXXXX")) // <-- PUT YOUR APP NAME HERE IT WILL SHOW THIS NAME WHEN CONNECTING TO WIFI
/*
***********       IMPORTANT: CONNECT TO 192.168.4.1   IN YOUR BROWSER       **********
*/
  {
    Serial.println("failed to connect and hit timeout");
    ESP.reset();
  } //                                                              
  ticker.detach();
  //if you get here you have connected to the WiFi 
  Serial.println("connected...yeey :)");
  /************************************ WIFI MANAGER SETUP END **********************************/
  //WIFI MANAGER WORKAROUND - COMMENT OUT IF HARD CODING THE DEVICE
  ssid = WiFi.SSID().c_str();
  pass = WiFi.psk().c_str();

  //BEGIN STUFF GOES HERE:
  //Blynk.begin(auth, ssid, pass);
  Blynk.config(auth); //try this, uncomment the one above if it doesn't work
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

  //TIMERS
  timer.setInterval(15000L, sendSensorLDR); //LDR SENSOR READINGS SENT TO BLYNK
  timer.setInterval(1000L, rainbowRGB); // change RGB to rainbow in low light

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

I am using a D1 mini.
Any help will be appreciated.
Thanks.

Do you have widgets attached to digital pins?

Pete.

No, not yet.

You need to explain more about your hardware, and your Blynk setup.

Pete.

Thanks Pete, so are you saying there is nothing wrong with the code?

I’ using a D1 mini and the code and hardware setup for the RGB originated here: https://www.instructables.com/RGB-LED-Smooth-Color-Transition-Using-Arduino-UNO/

Blynk has not been set up to receive the LDR readings yet. do think this is where the problem is, if so can you point me in the right direction of what to do?

No, I’m trying to understand you hardware and Blynk setup, as you didn’t include enough information in your original post, but your latest post has confused things even more!.

You say that you’re using a D1 Mini, but have posted an image of an Arduino Uno!

My guess is that the WiFi manager is still trying to connect to blynk-cloud.com (the legacy cloud server) rather than blynk.cloud

What does your serial monitor show?

Pete.

Thanks Pete, I uploaded the correct image to the previous post now.
Blynk connects to the device and serial monitor shows the readings of the LDR, I can post a screenshot when I get home later. Device shows online when opening the new Blynk app.

This was another subject I wanted to ask about at a later stage, can it connect to both?

Don’t post a screenshot of your serial monitor. Copy the data and paste it with triple backticks in the same way as when you post code.

Your Wemos can’t connect to both the legacy and IoT servers at te same time, if that’s what you were asking.

Your two sketches seem significantly different - different pins for R, G & B, different variable names etc. I’ve not dissected your tow pieces of code in detail, but I’m wondering why you haven’t simply added the template ID etc into your old code and made any other necessary adjustments.

Pete.

You are right, the original was on a breadboard, my soldered pins was the wrong way around so I changed it… the other changes were for identification purposes and added the LDR…

I appreciate your assistance.

Here is the serial monitor…

15:43:32.701 -> 0�~?�4�!����OCAG��*wm:[1] AutoConnect
15:43:32.847 -> *wm:[2] Connecting as wifi client...
15:43:32.879 -> *wm:[2] setSTAConfig static ip not set, skipping
15:43:32.912 -> *wm:[1] Connecting to SAVED AP: xxxxxxxxxxxx
15:43:33.345 -> *wm:[1] connectTimeout not set, ESP waitForConnectResult...
15:43:33.600 -> *wm:[1] AutoConnect: SUCCESS
15:43:33.632 -> *wm:[2] Connected in 737 ms
15:43:33.664 -> *wm:[1] STA IP Address: 192.xxxxxxxx
15:43:33.696 -> connected...yeey :)
15:43:33.600 -> *wm:[1] AutoConnect: SUCCESS
15:43:33.632 -> *wm:[2] Connected in 737 ms
15:43:33.664 -> *wm:[1] STA IP Address: 192.xxxxxxxxxx
15:43:33.696 -> connected...yeey :)
15:43:33.728 -> [840]
15:43:33.728 -> ___ __ __
15:43:33.760 -> / _ )/ /_ _____ / /__
15:43:33.791 -> / _ / / // / _ \/ '_/
15:43:33.791 -> /____/_/\_, /_//_/_/\_\
15:43:33.824 -> /___/ v1.0.1 on ESP8266
15:43:33.856 ->
15:43:33.856 -> [994] Connecting to blynk.cloud:80
15:43:34.716 -> [1975] Ready (ping: 287ms).
15:43:48.767 ->
15:43:48.767 -> Light lx in % is:0

But I think I actually fixed my problem… I increased the timer interval to 9000L, because there are 9 loops of 1 second each.

Will test and let you know.