On/Off Button Doesn't work with Blynk and Neopixel

I’m using the Wemos D1 Mini with the RGB Shield.

I’m using zeRGBa to change colors and also a button in the app that should turn on and off the LED.

But it is not working properly.

Source code:

#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Adafruit_NeoPixel.h>
#define PIN D2
#define NUMPIXELS 1
int pinData;
BLYNK_WRITE(V2){
  pinData = param.asInt(); 
}

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(1, PIN, NEO_GRB + NEO_KHZ800);

char auth[] = "9a09ace50cd04492815c302e0f3bf3ba"; //insert here your token generated by Blynk

const char* ssid = "netvirtua_296Ap81";
const char* pass = "3070657610";


BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncAll();

  // Alternatively, you could override server state using:
  //Blynk.virtualWrite(V2, ledState);
}


void setup() {
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth, ssid, pass); //insert here your SSID and password
  pixels.begin(); // This initializes the NeoPixel library.
  pinMode(PIN,OUTPUT);
}

BLYNK_WRITE(V3) // Widget WRITEs to Virtual Pin V2
{   
int R = param[0].asInt();
int G = param[1].asInt();
int B = param[2].asInt();
  Serial.println(R);
  Serial.println(G);
  Serial.println(B);

for(int i=0;i<NUMPIXELS;i++){
    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(R,G,B)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
}

}

void loop() {

  Blynk.run();
  digitalWrite(PIN,pinData);

  }

After pasting code, please format it like this for proper viewing

Blynk - FTFC

Remove this line… it gets overridden every time a new data stream gets processed anyhow… besides that pin is data control, not power control of the NeoPixel.

Just add in another BLYNK_WRITE() function for your ON/OFF Button Widget and use it to set a flag and add in a flag check to your colour control…

int FLAG // Setting global variable pre setup

BLYNK_WRITE(Vx) // FLAG control
{
  FLAG = param.asInt();
}



BLYNK_WRITE(V3) // Widget WRITEs to Virtual Pin V2
{
  int R = param[0].asInt();
  int G = param[1].asInt();
  int B = param[2].asInt();

  if (FLAG == 0) { // If "power" button is LOW/0, then override all RGB variables to 0
    int R = 0;
    int B = 0;
    int G = 0;
  }

  Serial.println(R);
  Serial.println(G);
  Serial.println(B);

  for (int i = 0; i < NUMPIXELS; i++) {
    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(R, G, B)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
  }
}

Also, consider setting your variables as global, like I did for the FLAG variable… then you can use the same variables outside of their function.

https://www.arduino.cc/en/Reference/scope

@Gunner Thank you for your help.
But I did, the adjustments I mentioned but now I came across the following problem:

When the button status (V4) is OFF, the LED will only “turn off” when changing the color in the ZeRGBA.

Code Below:

#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Adafruit_NeoPixel.h>
#define PIN D2
#define NUMPIXELS 1

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(1, PIN, NEO_GRB + NEO_KHZ800);

char auth[] = "9a09ace50cd04492815c302e0f3bf3ba"; //insert here your token generated by Blynk

const char* ssid = "netvirtua_296Ap81";
const char* pass = "3070657610";



int FLAG; // Setting global variable pre setup

int R;
int G;
int B;


BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncAll();

  // Alternatively, you could override server state using:
  //Blynk.virtualWrite(V2, ledState);
}
void setup() {
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth, ssid, pass); //insert here your SSID and password
  pixels.begin(); // This initializes the NeoPixel library.
}

BLYNK_WRITE(V4) // FLAG control
{
  FLAG = param.asInt();
}

BLYNK_WRITE(V3) // Widget WRITEs to Virtual Pin V2
{   
  R = param[0].asInt();
  G = param[1].asInt();
  B = param[2].asInt();
  
   if (FLAG == 0) { // If "power" button is LOW/0, then override all RGB variables to 0
    R = 0;
    B = 0;
    G = 0;
   }
  Serial.println(R);
  Serial.println(G);
  Serial.println(B);

for(int i=0;i<NUMPIXELS;i++){
    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(R,G,B)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
}

}

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

Yes… that is because each BLYNK_WRITE() function is independently controlled by its associated Widgets change of state.

But each time you press V4 (Button) you can also force a “refresh” of V3 (ZeRGBa) with Blynk.syncVirtual()

http://docs.blynk.cc/#blynk-firmware-virtual-pins-control-blynksyncvirtualvpin

BLYNK_WRITE(V4) // FLAG control
{
  FLAG = param.asInt();
  Blynk.syncVirtual(V3);  // Force refresh of ZeRGBa function
}
1 Like

@Gunner Thank you so much for your help

Problem Solved!