[SOLVED] Master Brightness

I am working on a project and need to control the brightness of the 3 leds RGB via the slider widget and cant seem to figure out how to write the code. Can some one edit my code. It is working perfectly but not the the slider and when I use widget led I am getting an error login timeout.

This is the working code
/**************************************************************
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
     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 ESP8266 chip.

   You need to install this for ESP8266 development:
     https://github.com/esp8266/Arduino

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

   Change WiFi ssid, pass, and Blynk auth token to run :)

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

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define redpin 14
#define greenpin 12
#define bluepin 13
#define redvirt 5
#define grnvirt 6
#define bluevirt 7
#define witvirt 8
#define yellvirt 9
#define cyanvirt 10
#define vilvirt  14

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "8374e7ea0b454xxxxxxxxxxxxxxxxx";

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, "CxxxxxxxxWi-Fi", "xxxxxxxx");

  pinMode(redpin, OUTPUT);
  pinMode(bluepin, OUTPUT);
  pinMode(greenpin, OUTPUT);
  digitalWrite(redpin, LOW);
  digitalWrite(greenpin, LOW);
  digitalWrite(bluepin, LOW);
}


BLYNK_WRITE(V0) // At global scope (not inside of the function)
{
  if ( param.asInt() == 1 )
  {
    digitalWrite(redpin, HIGH);
   
  }
  else
  {
    digitalWrite(redpin, LOW);
   
  }
}

BLYNK_WRITE(V1) // At global scope (not inside of the function)
{
  if ( param.asInt() == 1 )
  {
    digitalWrite(greenpin, HIGH);
   
  }
  else
  {
    digitalWrite(greenpin, LOW);

  }
}

BLYNK_WRITE(V2) // At global scope (not inside of the function)
{
  if ( param.asInt() == 1 )
  {
    digitalWrite(bluepin, HIGH);
  }
  else
  {
    digitalWrite(bluepin, LOW);
  }
}

BLYNK_WRITE(3) // At global scope (not inside of the function)

{
  analogWrite(redpin, param.asInt());
  analogWrite(bluepin, param.asInt());
  analogWrite(greenpin, param.asInt());
}


BLYNK_WRITE(V4) // At global scope (not inside of the function)
{
  if ( param.asInt() == 1 )
  {
    analogWrite(redpin, 1);
    analogWrite(greenpin, 1);
    analogWrite(bluepin, 1);
  }
  else
  {
    analogWrite(redpin, 0);
    analogWrite(greenpin, 0);
    analogWrite(bluepin, 0);
  }
}

BLYNK_WRITE(V11) // At global scope (not inside of the function)
{
  if ( param.asInt() == 1 )
  {
    digitalWrite(redpin, HIGH);
    digitalWrite(greenpin, HIGH);
    digitalWrite(bluepin, HIGH);
  }
  else
  {
    digitalWrite(redpin, LOW);
    digitalWrite(greenpin, LOW);
    digitalWrite(bluepin, LOW);
  }
}


BLYNK_WRITE(V12) // At global scope (not inside of the function)
{
  if ( param.asInt() == 1 )
  {
    digitalWrite(redpin, LOW);
    digitalWrite(greenpin, LOW);
    digitalWrite(bluepin, LOW);
  }
  else
  {
    digitalWrite(redpin, LOW);
    digitalWrite(greenpin, LOW);
    digitalWrite(bluepin, LOW); //off
  }
}

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

And Dashboard

I’m not sure what you are trying to accomplish here, can you maybe explain a bit? What is it the brightness slider has to do, does it need to tune down all led’s, the virtual ones or the real ones?

I also see you have a bunch of virtual leds connected to physical pins, but you don’t use them.

What kind of hardware are you using btw? Is this an ESP?

First I would use analogWrites throughout.
Have three variables assigned to red,green and blue levels, so that every change is known in each virtual call.
In the brightness call use the slider level to scale these previously set values and send those new vaues out.
analogWrite(redPin, redVal * (param.asInt/1023)); etc.
Once you have that working, advance from there.

.

1 Like

The slider has to control the 3 leds brightness which are connected to gpio 12-14 and the slider is hooked up to a virtual pin. I am using the esp8266 esp-05 and those virtual leds are going to light up with a physical led but when I tried to write the code it gives me an error in serial monitor login timeout

The code for the leds
BLYNK_WRITE(V1)
{
Digitalwrite(redpin, HIGH)
Blynk.Virtualwrite(redvirt, HIGH)
}
else
{
Digitalwrite(redpin, LOW)
Blynk. Virtualwrite(redvirt, LOW)

Take a look at …
https://github.com/blynkkk/blynk-library/blob/master/examples/Widgets/LED/LED.ino#L31

It involves WidgetLED led1(1); etc

That should sort driving your virtual leds.

1 Like

Thank you for the fast reply from you, Ok I will try the example but what about the brightness control?

Ok … As I said, make all writes analogWrites with values 0-1023. Get rid off digitalWrites.
Add variables …
int redValue
int greenValue
int blueValue

At every line where an analogWrite occurs, assign the related red/green/blueValue variables the value of the param.asInt.
This way, at any stage, you will know the present levels of the rgb settings.

When it comes to the Master brightness level, it can adjust the known levels by manipulating red/green/blueValue and send these out to adjust all three levels.

So, the master level slider returns a 0-255 level.

Each level will be adjust by the ratio param / 255.

ie analogWrite(redpin,redValue * (param/255)); and same for green/bluepin;

Any help?

I tried the code but is giving me an error error:invalid use of member function (did you forget the () ?)

Am I writing it wrong ?

Insted of writing a variable for every led I wrote one to control the brighness

analogWrite(greenpin, brightnessval * (param.asInt/1023));

This is the crrent code

/**************************************************************
   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
     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 ESP8266 chip.

   You need to install this for ESP8266 development:
     https://github.com/esp8266/Arduino

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

   Change WiFi ssid, pass, and Blynk auth token to run :)

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

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define redpin 14
#define greenpin 12
#define bluepin 13
#define redvirt 5
#define grnvirt 6
#define bluevirt 7
#define witvirt 8
#define yellvirt 9
#define cyanvirt 10
#define vilvirt  14
#define delayTime 100
#define brightnessval 13
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "8374e7ea0b4541f085b0d0f59861b2df";

int mode ;

void setup()
{

  mode = 0 ; // Start in a known state

  Serial.begin(9600);
  Blynk.begin(auth, "Clayton Camilleri Wi-Fi", "clayton13");

  pinMode(redpin, OUTPUT);
  pinMode(bluepin, OUTPUT);
  pinMode(greenpin, OUTPUT);
  digitalWrite(redpin, LOW);
  digitalWrite(greenpin, LOW);
  digitalWrite(bluepin, LOW);
}




  BLYNK_WRITE(V0) // At global scope (not inside of the function)
  {
    if ( param.asInt() == 1 )
    {
     analogWrite(redpin, brightnessval * (param.asInt/1023))() ;

    }
    else
    {
      analogWrite(redpin, brightnessval * (param.asInt/1023))() ;

    }
  }

  BLYNK_WRITE(V1) // At global scope (not inside of the function)
  {
    if ( param.asInt() == 1 )
    {
      analogWrite(greenpin, brightnessval * (param.asInt/1023))() ;

    }
    else
    {
     analogWrite(greenpin, brightnessval * (param.asInt/1023))() ;

    }
  }

  BLYNK_WRITE(V2) // At global scope (not inside of the function)
  {
    if ( param.asInt() == 1 )
    {
      analogWrite(bluepin, brightnessval * (param.asInt/1023))();
    }
    else
    {
      analogWrite(bluepin, brightnessval * (param.asInt/1023))(); 
    }
  }

  BLYNK_WRITE(3) // At global scope (not inside of the function)

  {
    analogWrite(redpin, brightnessval * (param.asInt/1023))();
    analogWrite(greenpin, brightnessval * (param.asInt/1023))();
    analogWrite(bluepin, brightnessval * (param.asInt/1023))(); 
  }


  BLYNK_WRITE(V4) // At global scope (not inside of the function)
  {
    if ( param.asInt() == 1 )
    {
      analogWrite(redpin, 1);
      analogWrite(greenpin, 1);
      analogWrite(bluepin, 1);
    }
    else
    {
      analogWrite(redpin, 0);
      analogWrite(greenpin, 0);
      analogWrite(bluepin, 0);
    }
  }

  BLYNK_WRITE(V11) // At global scope (not inside of the function)
  {
    if ( param.asInt() == 1 )
    {
      analogWrite(redpin, brightnessval * (param.asInt/1023))();
      analogWrite(greenpin, brightnessval * (param.asInt/1023))();
      analogWrite(bluepin, brightnessval * (param.asInt/1023))();
    }
    else
    {
      analogWrite(redpin, brightnessval * (param.asInt/1023))();
      analogWrite(greenpin, brightnessval * (param.asInt/1023))();
      analogWrite(bluepin, brightnessval * (param.asInt/1023))(); 
    }
  }


  BLYNK_WRITE(V12) // At global scope (not inside of the function)
  {
    if ( param.asInt() == 1 )
    {
      digitalWrite(redpin, LOW);
      digitalWrite(greenpin, LOW);
      digitalWrite(bluepin, LOW);
    }
    else
    {
      digitalWrite(redpin, LOW);
      digitalWrite(greenpin, LOW);
      digitalWrite(bluepin, LOW); //off
    }
  }

  BLYNK_WRITE(15)
  {
    // Change state on button press
    if ( param.asInt() == 1 ) {
      mode = 1 ;
    } else {
      mode = 0 ;
    }
  }


  void runModeOne() {

    digitalWrite(redpin, HIGH);
    delay(delayTime);
    digitalWrite(redpin, LOW);
    delay(delayTime);
    digitalWrite(greenpin, HIGH);
    delay(delayTime);
    digitalWrite(greenpin, LOW);
    delay(delayTime);
    digitalWrite(bluepin, HIGH);
    delay(delayTime);
    digitalWrite(bluepin, LOW);
    delay(delayTime);
  }


  void loop()
  {
    Blynk.run();

    switch ( mode ) {
      case 1:
        runModeOne();
        break;

defaul: {
          // Do nothing
        }
    }
  }

OK… the lines

analogWrite(bluepin, brightnessval * (param.asInt/1023))();

are wrong … the double parenthesis () should be after param.asInt()

i.e.
analogWrite(greenpin, brightnessval * (param.asInt()/1023)) ;

I assume you will be using V13 as the brightness slider with a range of 0 - 1023 to assign a value to brightnessval?
Your V4 timer switches lights on? In which case the value sent would need to be 1023 (full on), not 1.

I would like to help, but I still don’t understand the physical set up and the end goal :confused:

Yes I am using the v13 pin for the slider and the v4 timer is set to 1 because the light needs to be dimmed for the night. I will try what you suggested and let you know.

@Pavel

I am working on a project for my bedroom. The first 4 buttons control an RGB led strip which is connected to GPIO 12 13 and 14.
The first button which is Red is set up with virtual pin 1, green virtual pin 2 and blue is virtual pin 3 the white button is connected to virtual pin 4 which is set up to light up the strip all together

The leds under the buttons are supposed to turn on when the respectiv physical led is turned on so widget led red with red strip led and so on. The widget leds are currently not working because when I tried to write the code it gives me the error login timeout and I am going to try what @Roffey suggested. The sliders are working properly but not the master brightness slider so my question is how can I write the code so when I adjust the slider the leds dimm or brighten according to the slider.

The last button is not working either it is suppose to turn all the leds completely off.

Btw I am using the esp8266

I tried what yo sggested the code is ploding now bt the bttons are not working now nothing. Am I doing something wrong?

/**************************************************************
   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
     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 ESP8266 chip.

   You need to install this for ESP8266 development:
     https://github.com/esp8266/Arduino

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

   Change WiFi ssid, pass, and Blynk auth token to run :)

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

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define redpin 14
#define greenpin 12
#define bluepin 13
#define redvirt 5
#define grnvirt 6
#define bluevirt 7
#define witvirt 8
#define yellvirt 9
#define cyanvirt 10
#define vilvirt  14
#define delayTime 100
#define brightnessval V16
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "8374e7ea0b4541f085b0d0f59861b2df";

int mode ;

void setup()
{

  mode = 0 ; // Start in a known state

  Serial.begin(9600);
  Blynk.begin(auth, "Clayton Camilleri Wi-Fi", "clayton13");

  pinMode(redpin, OUTPUT);
  pinMode(bluepin, OUTPUT);
  pinMode(greenpin, OUTPUT);
  digitalWrite(redpin, LOW);
  digitalWrite(greenpin, LOW);
  digitalWrite(bluepin, LOW);
}




  BLYNK_WRITE(0) // At global scope (not inside of the function)
  {
    if ( param.asInt() == 1 )
    {
     analogWrite(redpin, brightnessval * (param.asInt()/1023)) ; 

    }
    else
    {
      analogWrite(redpin, brightnessval * (param.asInt()/1023)) ; 

    }
  }

  BLYNK_WRITE(1) // At global scope (not inside of the function)
  {
    if ( param.asInt() == 1 )
    {
      analogWrite(greenpin, brightnessval * (param.asInt()/1023)) ; 

    }
    else
    {
     analogWrite(greenpin, brightnessval * (param.asInt()/1023)) ; 

    }
  }

  BLYNK_WRITE(2) // At global scope (not inside of the function)
  {
    if ( param.asInt() == 1 )
    {
      analogWrite(bluepin, brightnessval * (param.asInt()/1023)) ; 
    }
    else
    {
      analogWrite(bluepin, brightnessval * (param.asInt()/1023)) ; 
    }
  }

  BLYNK_WRITE(3) // At global scope (not inside of the function)

  {
    analogWrite(redpin, brightnessval  *  (param.asInt()/1023)) ; 
    analogWrite(greenpin, brightnessval  *  (param.asInt()/1023)) ; 
    analogWrite(bluepin, brightnessval  *  (param.asInt()/1023)) ; 
  }


  BLYNK_WRITE(V4) // At global scope (not inside of the function)
  {
    if ( param.asInt() == 1 )
    {
      analogWrite(redpin, 1);
      analogWrite(greenpin, 1);
      analogWrite(bluepin, 1);
    }
    else
    {
      analogWrite(redpin, 0);
      analogWrite(greenpin, 0);
      analogWrite(bluepin, 0);
    }
  }

  BLYNK_WRITE(V11) // At global scope (not inside of the function)
  {
    if ( param.asInt() == 1 )
    {
      analogWrite(redpin, brightnessval  *  (param.asInt()/1023)) ; 
      analogWrite(greenpin, brightnessval  *  (param.asInt()/1023)) ; 
      analogWrite(bluepin, brightnessval  *  (param.asInt()/1023)) ; 
    }
    else
    {
      analogWrite(redpin, brightnessval  *  (param.asInt()/1023)) ; 
      analogWrite(greenpin, brightnessval  *  (param.asInt()/1023)) ; 
      analogWrite(bluepin, brightnessval  *  (param.asInt()/1023)) ; 
    }
  }


  BLYNK_WRITE(V12) // At global scope (not inside of the function)
  {
    if ( param.asInt() == 1 )
    {
      digitalWrite(redpin, LOW);
      digitalWrite(greenpin, LOW);
      digitalWrite(bluepin, LOW);
    }
    else
    {
      digitalWrite(redpin, LOW);
      digitalWrite(greenpin, LOW);
      digitalWrite(bluepin, LOW); //off
    }
  }

  BLYNK_WRITE(15)
  {
    // Change state on button press
    if ( param.asInt() == 1 ) {
      mode = 1 ;
    } else {
      mode = 0 ;
    }
  }


  void runModeOne() {

    digitalWrite(redpin, HIGH);
    delay(delayTime);
    digitalWrite(redpin, LOW);
    delay(delayTime);
    digitalWrite(greenpin, HIGH);
    delay(delayTime);
    digitalWrite(greenpin, LOW);
    delay(delayTime);
    digitalWrite(bluepin, HIGH);
    delay(delayTime);
    digitalWrite(bluepin, LOW);
    delay(delayTime);
  }


  void loop()
  {
    Blynk.run();

    switch ( mode ) {
      case 1:
        runModeOne();
        break;

defaul: {
          // Do nothing
        }
    }
  }

ok… you are getting there step by step …
The button V0 has two states … 0 and 1. So the param value will be 0 or 1. If you work out what will be sent as a pwm setting, you will see why it doesn’t work.

BLYNK_WRITE(0) // At global scope (not inside of the function)
  {
    if ( param.asInt() == 1 )
    {
     analogWrite(redpin, brightnessval * (param.asInt()/1023)) ; 

    }
    else
    {
      analogWrite(redpin, brightnessval * (param.asInt()/1023)) ; 

    }
  }

Try this for the buttons … obviously change the pin names

if ( param.asInt() == 1 )
    {
     analogWrite(redpin, brightnessval ) ;    // for switching on at brightness level

    }
    else
    {
      analogWrite(redpin, 0) ;                       // for switching off 

    }

The control panel still shows V13 with a 0 - 255 range. This should be 0 - 1023, as I said earlier.
You haven’t change V12 to analogWrites, and both values are 0. should be 1023 or 0 after changing and using analogWrite.
This also applies to your runModeOne().

Note… I say about changing all digitalWrites to analogWrites because the analogWrites put the ports into a timer pwm state. For the most stable esp8266 running, I would not want to switch between pin modes.(Pwm and digital on/off).

At least you are progressing and hopefully learning along the way, as we all do. (Except for those clever enough to just rattle this stuff off, of course!)

I tried what you suggested for the buttons but now they are dimmly lit and adjusting the slider no difference what so ever and I changed the code for the widget leds and they work but a little slow and I still get an error login time-out and only when I use the widget led. The V12 button worked after changing to analogWrite.

The code

/**************************************************************
   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
     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 ESP8266 chip.

   You need to install this for ESP8266 development:
     https://github.com/esp8266/Arduino

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

   Change WiFi ssid, pass, and Blynk auth token to run :)

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

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define redpin 14
#define greenpin 12
#define bluepin 13
/*
  #define redvirt 5
  #define grnvirt 6
  #define bluevirt 7
  #define witvirt 8
  #define yellvirt 9
  #define cyanvirt 10
  #define vilvirt  14
*/
#define delayTime 500


int brightnessval  = 16;
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "8374e7ea0b45xxxxxxxxxxxxxxxxxx";


WidgetLED redvirt(5);
WidgetLED greenvirt(6);
WidgetLED bluevirt(7);
WidgetLED witvirt(8);


int mode ;

void setup()
{

  mode = 0 ; // Start in a known state

  Serial.begin(9600);
  Blynk.begin(auth, "Clxxxxxxxxxxxxxi", "claxxxxxx3");

  pinMode(redpin, OUTPUT);
  pinMode(bluepin, OUTPUT);
  pinMode(greenpin, OUTPUT);
  analogWrite(redpin, 0);
  analogWrite(greenpin, 0);
  analogWrite(bluepin, 0);
}




BLYNK_WRITE(V0) // At global scope (not inside of the function)
{
  if ( param.asInt() == 1 )
  {
    analogWrite(redpin, brightnessval) ;
    redvirt.on();

  }
  else
  {
    analogWrite(redpin, 0) ;
    redvirt.off();

  }
}

BLYNK_WRITE(V1) // At global scope (not inside of the function)
{
  if ( param.asInt() == 1 )
  {
    analogWrite(greenpin, brightnessval);
    greenvirt.on();

  }
  else
  {
    analogWrite(greenpin, 0 );
    greenvirt.off();

  }
}

BLYNK_WRITE(V2) // At global scope (not inside of the function)
{
  if ( param.asInt() == 1 )
  {
    analogWrite(bluepin, brightnessval );
    bluevirt.on();

  }
  else
  {
    analogWrite(bluepin, 0);
    bluevirt.off();
  }
}

BLYNK_WRITE(V3) // At global scope (not inside of the function)

{
  analogWrite(redpin, brightnessval );
  analogWrite(greenpin, brightnessval ) ;
  analogWrite(bluepin, brightnessval ) ;
  greenvirt.on();
  bluevirt.on();
  redvirt.on();
}


BLYNK_WRITE(V4) // At global scope (not inside of the function)
{
  if ( param.asInt() == 1 )
  {
    analogWrite(redpin, 1);
    analogWrite(greenpin, 1);
    analogWrite(bluepin, 1);
    greenvirt.on();
    bluevirt.on();
    redvirt.on();
  }
  else
  {
    analogWrite(redpin, 0);
    analogWrite(greenpin, 0);
    analogWrite(bluepin, 0);
    greenvirt.off();
    bluevirt.off();
    redvirt.off();
  }
}

BLYNK_WRITE(V11) // At global scope (not inside of the function)
{
  if ( param.asInt() == 1 )
  {
    analogWrite(redpin, brightnessval  ) ;
    analogWrite(greenpin, brightnessval  ) ;
    analogWrite(bluepin, brightnessval ) ;
    greenvirt.on();
    bluevirt.on();
    redvirt.on();
  }
  else
  {
    analogWrite(redpin, 0 ) ;
    analogWrite(greenpin, 0 ) ;
    analogWrite(bluepin, 0 ) ;
    greenvirt.off();
    bluevirt.off();
    redvirt.off();
  }
}


BLYNK_WRITE(V12) // At global scope (not inside of the function)
{
  if ( param.asInt() == 1 )
  {
    analogWrite(redpin, 0);
    analogWrite(greenpin, 0);
    analogWrite(bluepin, 0);
    greenvirt.off();
    bluevirt.off();
    redvirt.off();
  }
  else
  {
    analogWrite(redpin, 0);
    analogWrite(greenpin, 0);
    analogWrite(bluepin, 0); //off
    greenvirt.off();
    bluevirt.off();
    redvirt.off();
  }
}

BLYNK_WRITE(V15)
{
  // Change state on button press
  if ( param.asInt() == 1 ) {
    mode = 1 ;
  } else {
    mode = 0 ;
  }
}


void runModeOne() {

  analogWrite(redpin, 1023);
  redvirt.on();
  delay(delayTime);
  analogWrite(redpin, 0);
  redvirt.off();
  delay(delayTime);
  analogWrite(greenpin, 1023);
  greenvirt.on();
  delay(delayTime);
  analogWrite(greenpin, 0);
  greenvirt.off();
  delay(delayTime);
  analogWrite(bluepin, 1023);
  bluevirt.on();
  delay(delayTime);
  analogWrite(bluepin, 0);
  bluevirt.off();
  delay(delayTime);
}


void loop()
{
  Blynk.run();

  switch ( mode ) {
    case 1:
      runModeOne();
      break;

defaul: {
        // Do nothing
      }
  }
}

Ok… try this for varying the leds and brightness.
Take out the pwm use of the sliders, and allocate as in the prog.
Note that as from my first reply the presently set colour value of each led is stored each time it is changed so that it can be re used where needed.
It is quite possible that timeouts are occurring if data is being sent too fast to the cloud server. i,e. sliders being swept and virtual leds being switched at the same time.

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define redpin 14
#define greenpin 12
#define bluepin 13
#define delayTime 500

int brightnessval  = 0;  //lights off
int redCol;
int greenCol;
int blueCol;
int whiteCol
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "8374e7ea0b45xxxxxxxxxxxxxxxxxx";


WidgetLED redvirt(5);
WidgetLED greenvirt(6);
WidgetLED bluevirt(7);
WidgetLED witvirt(8);


int mode ;

void setup()
{

  mode = 0 ; // Start in a known state

  Serial.begin(9600);
  Blynk.begin(auth, "Clxxxxxxxxxxxxxi", "claxxxxxx3");

  pinMode(redpin, OUTPUT);
  pinMode(bluepin, OUTPUT);
  pinMode(greenpin, OUTPUT);
  analogWrite(redpin, 0);
  analogWrite(greenpin, 0);
  analogWrite(bluepin, 0);
}

// V20 =red, V21= Green,V22 = Blue,V23 = White V24=Brightness

// V20 - V24 are sliders assigned to virtual pins 0-1023 range.


BLYNK_WRITE(V20) //red
 {
 redCol = param.asInt();
 analogWrite(redpin,redCol*(brightnessval/1023)); 
 }
BLYNK_WRITE(V21) //green
 {
 greenCol = param.asInt();
 analogWrite(greenpin,greenCol*(brightnessval/1023)); 
 }
BLYNK_WRITE(V22) //blue
 {
 blueCol = param.asInt();
 analogWrite(bluepin,blueCol*(brightnessval/1023)); 
 }
BLYNK_WRITE(V23) //white
 {
 whiteCol = param.asInt();
 redCol = whiteCol;
 blueCol= whiteCol;
 greenCol= whiteCol;
 analogWrite(redpin,redCol*(brightnessval/1023)); 
 analogWrite(bluepin,blueCol*(brightnessval/1023));
 analogWrite(greenpin,greenCol*(brightnessval/1023));
 }

BLYNK_WRITE(V24)
  { 
  brightnessval =param.asInt();
  analogWrite(redpin,redCol*(brightnessval/1023)); 
  analogWrite(bluepin,blueCol*(brightnessval/1023));
  analogWrite(greenpin,greenCol*(brightnessval/1023));
  }








BLYNK_WRITE(V0) // At global scope (not inside of the function)
{
  if ( param.asInt() == 1 )
  {
    analogWrite(redpin, brightnessval) ;
    redvirt.on();

  }
  else
  {
    analogWrite(redpin, 0) ;
    redvirt.off();

  }
}

BLYNK_WRITE(V1) // At global scope (not inside of the function)
{
  if ( param.asInt() == 1 )
  {
    analogWrite(greenpin, brightnessval);
    greenvirt.on();

  }
  else
  {
    analogWrite(greenpin, 0 );
    greenvirt.off();

  }
}

BLYNK_WRITE(V2) // At global scope (not inside of the function)
{
  if ( param.asInt() == 1 )
  {
    analogWrite(bluepin, brightnessval );
    bluevirt.on();

  }
  else
  {
    analogWrite(bluepin, 0);
    bluevirt.off();
  }
}



BLYNK_WRITE(V4) // At global scope (not inside of the function)
{
  if ( param.asInt() == 1 )
  {
    analogWrite(redpin, 1023); // or change with brightness val?
    analogWrite(greenpin, 1023);
    analogWrite(bluepin, 1023);
    greenvirt.on();
    bluevirt.on();
    redvirt.on();
  }
  else
  {
    analogWrite(redpin, 0);
    analogWrite(greenpin, 0);
    analogWrite(bluepin, 0);
    greenvirt.off();
    bluevirt.off();
    redvirt.off();
  }
}

BLYNK_WRITE(V11) // At global scope (not inside of the function)
{
  if ( param.asInt() == 1 )
  {
    analogWrite(redpin, brightnessval  ) ;
    analogWrite(greenpin, brightnessval  ) ;
    analogWrite(bluepin, brightnessval ) ;
    greenvirt.on();
    bluevirt.on();
    redvirt.on();
  }
  else
  {
    analogWrite(redpin, 0 ) ;
    analogWrite(greenpin, 0 ) ;
    analogWrite(bluepin, 0 ) ;
    greenvirt.off();
    bluevirt.off();
    redvirt.off();
  }
}


BLYNK_WRITE(V12) // At global scope (not inside of the function)
{
  if ( param.asInt() == 1 )
  {
    analogWrite(redpin, 0);
    analogWrite(greenpin, 0);
    analogWrite(bluepin, 0);
    greenvirt.off();
    bluevirt.off();
    redvirt.off();
  }
  else
  {
    analogWrite(redpin, 0);
    analogWrite(greenpin, 0);
    analogWrite(bluepin, 0); //off
    greenvirt.off();
    bluevirt.off();
    redvirt.off();
  }
}








BLYNK_WRITE(V15)
{
  // Change state on button press
  if ( param.asInt() == 1 ) {
    mode = 1 ;
  } else {
    mode = 0 ;
  }
}


void runModeOne() {

  analogWrite(redpin, 1023);
  redvirt.on();
  delay(delayTime);
  analogWrite(redpin, 0);
  redvirt.off();
  delay(delayTime);
  analogWrite(greenpin, 1023);
  greenvirt.on();
  delay(delayTime);
  analogWrite(greenpin, 0);
  greenvirt.off();
  delay(delayTime);
  analogWrite(bluepin, 1023);
  bluevirt.on();
  delay(delayTime);
  analogWrite(bluepin, 0);
  bluevirt.off();
  delay(delayTime);
}


void loop()
{
  Blynk.run();

  switch ( mode ) {
    case 1:
      runModeOne();
      break;

defaul: {
        // Do nothing
      }
  }
}
1 Like

Thank you @Roffey I Iearned a lot in this project sorry for me constintly posting my problems and thanks for editing my code.

Thankyou
Clayton

That’s what it’s all about really … Learning and progressing. Fortunately there are lots of people who will help along the way, and it is always good to be just one of those when you can be.
Good luck with your project.

1 Like