Hello,
I have a (adafruit) neopixel ledstrip which I connect to my ESP8266. I want to control the strip (colors and paterns) with my phone so I use Blynk.
I already can change the color of the srtip using BLYNK_WRITE(v1). I can also run the chase by blynk-connecting a button to v2 but it will only run 1 time when I push the button.
How I can repeat this “Chase” as long as the button (switch) is on?
Code:
#include <Adafruit_NeoPixel.h>
#include <ESP8266_SoftSer.h>
#include <BlynkSimpleShieldEsp8266_SoftSer.h>
#define PIN 6 // pin ledstrip
#define NUMPIXELS 60 // Number of pixels in strip
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
char ssid[] = "SSID";
char password[] = "PASSWORD";
char token[] = "Token";// You should get Auth Token in the Blynk App.
// Set NanoESP Serial object
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(11, 12); // RX, TX
ESP8266 wifi(EspSerial);
int Red;
int Green;
int Blue;
void setup(){
Serial.begin(19200);
EspSerial.begin(19200);
Blynk.begin(token, wifi, ssid, password);
strip.begin();
strip.show();
}
BLYNK_WRITE(V1){ // change color in blynk
int R = param[0].asInt();
int G = param[1].asInt();
int B = param[2].asInt();
Red = R;
Green = G;
Blue = B;
Serial.println(R);
Serial.println(G);
Serial.println(B);
for(int i=0;i<NUMPIXELS;i++){
strip.setPixelColor(i, strip.Color(R,G,B)); // background color leds
strip.show(); // This sends the updated pixel color to the hardware.
}
}
BLYNK_WRITE(V2){ // Set chase on or off
//chase(strip.Color(255,0,0)); // Red
//chase(strip.Color(0,255,0)); // Green
while (V2){
chase(strip.Color(0,0,255)); // Blue
// This runs chase 1 time
}
}
void loop()
{
Blynk.run();
}
void chase(uint32_t c) {
for(uint16_t i=0; i<strip.numPixels()+3; i++) {
strip.setPixelColor(i , c); // Draw new pixel
strip.setPixelColor(i-3, Red,Green,Blue); // set color back to background color
strip.show();
delay(25);
}
}
I found the need to use the Blynk.syncVirtual() along with if()commands, while within the function loop. I think sticking your while() loop in the // Do Stuff Here area should work, as long as it doesn’t hold up things long enough to cause a timeout.
BLYNK_WRITE(V0) // Run this function while V0 button pressed.
{
int pinValue = param.asInt(); // Get status of V0.
if (pinValue == 1) { // If status of V0 is 1 then do stuff in if().
// Do Stuff Here
Blynk.run(); // Run rest of show in-between waiting for this loop to repeat or quit.
int pinValue = 0; // Set V0 status to 0 to quit, unless button is still pushed (as per below)
Blynk.syncVirtual(V2); // ...Then force BLYNK_WRITE(V0) function check of button status to determine if repeating or done.
}
}
Thanks Gunner,
I used your tips to further solve my project.
Here you will find my code:
#include <Adafruit_NeoPixel.h>
#include <ESP8266_SoftSer.h>
#include <BlynkSimpleShieldEsp8266_SoftSer.h>
#define PIN 6 // pin ledstrip
#define NUMPIXELS 60 // Number of pixels in strip
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
char ssid[] = "SSID";
char password[] = "PASSWORD";
char token[] = "Token"; // You should get Token in the Blynk App.
// Set NanoESP Serial object
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(11, 12); // RX, TX
ESP8266 wifi(EspSerial);
// colors
int Red;
int Red2;
int Green;
int Green2;
int Blue;
int Blue2;
int Length; // Length of chase
void setup(){
Serial.begin(19200);
EspSerial.begin(19200);
Blynk.begin(token, wifi, ssid, password);
strip.begin();
strip.show();
}
BLYNK_WRITE(V1){ // change background color in blynk
int R = param[0].asInt();
int G = param[1].asInt();
int B = param[2].asInt();
Red = R;
Green = G;
Blue = B;
Serial.println(R);
Serial.println(G);
Serial.println(B);
for(int i=0;i<NUMPIXELS;i++){
strip.setPixelColor(i, strip.Color(R,G,B)); // background color leds
strip.show(); // This sends the updated pixel color to the hardware.
}
}
BLYNK_WRITE(V2){ // change color chase in blynk
int R2 = param[0].asInt();
int G2 = param[1].asInt();
int B2 = param[2].asInt();
Red2 = R2;
Green2 = G2;
Blue2 = B2;
Serial.println(R2);
Serial.println(G2);
Serial.println(B2);
}
BLYNK_WRITE(V3){ // change length of chase in blynk
Length = param.asInt();
}
BLYNK_WRITE(V0) // set chase on or off
{
int pinValue = param.asInt(); // Get status of V0.
if (pinValue == 1) {
chase(strip.Color(Red2,Green2,Blue2));
Blynk.run(); // Run rest of show in-between waiting for this loop to repeat or quit.
int pinValue = 0; // Set V0 status to 0 to quit, unless button is still pushed (as per below)
Blynk.syncVirtual(V0); // ...Then force BLYNK_WRITE(V0) function check of button status to determine if repeating or done.
Blynk.syncVirtual(V1); // check color background
Blynk.syncVirtual(V2); // check color chase
Blynk.syncVirtual(V3); //check length of chase
}
}
void loop()
{
Blynk.run();
}
void chase(uint32_t c) {
for(uint16_t i=0; i<strip.numPixels()+Length; i++) {
strip.setPixelColor(i , c); // Draw new pixel
strip.setPixelColor(i-Length, Red,Green,Blue); // set color back to background color
strip.show();
delay(25);
}
}