Widgets for RGB Strip

It depends how you structure your code and set-up your button widget, but it would go something like this…

“Ooo! Look, somebody has pressed the On/Off button, and it’s now Off - we’d better write zeros to each of the digital pins so that each colour channel is off.”

“Now they’ve pressed that On/Off switch again, so it’s now On. We’d better write the values from each of the slider widgets to the corresponding digital pins so that each colour channel is lit-up at the correct level.”

“Now somebody has moved that Red slider. We’d better check if the On/Off switch is On, if it is then we’ll change the value that written to the digital pin that controls the Red channel. If the switch is Off then we’ll just save that new Red channel value and use it the next time the switch is turned On.”

“Now somebody has moved the Blue slider, I’m getting bored with this. why don’t we do something totally random so that the programmer has a nervous breakdown?”

Does this help?

Pete.

2 Likes

Adding a flag and Boolean check will act as a simple ON/OFF method (not controlling existing setting, just preventing further adjustment)… basically as stated… this is all about learning Arduino C++ code, not really Blynk specific stuff.

Note: defining global variables used like PowSwitch still needs to be done at beginning of code

BLYNK_WRITE(V0)  // This functionRuns every time LED slider widget is adjusted
{
  if (PowSwich == 1) { // Only make adjustments if PowSwitch is ON
  analogWrite(PWMpin, param.asInt());  // Send slider value (storred in param.asINT() )to real LED.
  }
}

BLYNK_WRITE(V1)  // This function runs every time button widget is pressed
{
PowSwich = param.asInt();  // Set flag ON or OFF based on Button Widget set as switch
}

Ahahaha ok that helps a lot!
I understand, but I think that’s maybe too complicated for me. For now at least! Wouldn’t the method Gunner posted below not be simpler, and work as well? I just wrote a quick code for three sliders at the moment:


#define BLYNK_PRINT Serial


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

char auth[] = "**************";

char ssid[] = "****";
char pass[] = "*********";

void setup()
{
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
}

 BLYNK_WRITE (V20)
{
   analogWrite(D5,param.asInt());
   loop;
}

BLYNK_WRITE (V21)
{
   analogWrite(D6,param.asInt());
   loop;
}

BLYNK_WRITE (V22)
{
   analogWrite(D7,param.asInt());
   loop;
}


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

It compiled, but is this right? I can’t test it at the moment, but I’ll do so later. The process you described sounds perfect, but like I said before maybe too complicated. If I can’t get the on/off switch coded, I can just as easily slide all three down and then have it turn off like that, although it wouldn’t save previous slider settings. One last question - if the above code is correct for three sliders, then would I be able to keep the zeRGBa widget I have at the moment? Would it also synchronise with the sliders?

Thanks

That was already done in the code you found from me… just no ON/OFF power button… but could be added.


Found a more complete code posting

Yeah, well you’re code still isn’t compiling… You said it’s for a larger project. Since I don’t understand your code 100% do you mind posting it here with all the variables defined so it works for my project? And with the variables I need to add clearly highlighted so I can copy paste it, change the variables, and check if it compiles then? If you don’t have time I understand, I can try using the code I wrote myself. If it doesn’t work I guess I’m back to plain zeRGBa then…

Here’s the error message you’re code is giving by the way:

Code_2.0:19: error: expected constructor, destructor, or type conversion before ‘(’ token
pinMode (RedP, OUTPUT); // Set RED on RGB LED.
^
Code_2.0:20: error: expected constructor, destructor, or type conversion before ‘(’ token
pinMode (GrnP, OUTPUT); // Set GREEN on RGB LED.
^
Code_2.0:21: error: expected constructor, destructor, or type conversion before ‘(’ token
pinMode (BluP, OUTPUT); // Set BLUE on RGB LED.
^
exit status 1
expected constructor, destructor, or type conversion before ‘(’ token

Using this code:



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

char auth[] = "******";

char ssid[] = "*****";
char pass[] = "*****";

#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```

You still need to have your own void setup() and void loop(), Blynk connection commands, etc… and add these three lines into the setup, along with all the normally needed stuff.

Don’t forget to match up all the virtual pins used in my example with their respective widgets in the App.

Also, be aware that this code is from an Arduino… so I am using the 0-255 ranges that you will need to adjust to the 0-1023 ranges of an ESP, both in the Widgets and the code.

Take it slow, make use of the Arduino site for reference and you will catch on… heck I still am learning how to code beyond beginner levels.