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
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.
@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();
}