Hello everyone I am a beginner so I don’t have much idea about how thing works with this blynk I need help in my project.
Hardware used :-
1.Nodemcu esp8266 12e
2.WS2812B led strip.
I am using blynk app on my smartphone xiaomi redmi y2
blynk library vesion 0.6.1
my problem is my animations dont run in a loop it runs just once and then it stops at with latest led data
or it keeps running and whenever i try to change animation it doesn’t change it and my esp8266 disconnects and i need to press reset button on my board to run it again
my programs has lots of bugs too please help me clean my code.
given below is the design of my app
i will now explain features what i am trying to add
when switch is OFF then nothing should work
but when i turn on switch depending on the mode which i have selected
if its static then based on the current rgb value in zeRGBa led’s should turn on
and if its in animated mode then default animation should start
if i have selected animation then nothing related to static to should work
if i have selected static then nothing related to animation should work
and every led should turn on depending on the current brightness
and as i change brightness in app brightness of led should change too
for example in static mode when i change brightness i can see change applied and don’t need to select color again but when i change brightness in animation mode i need to restart animation to see the change applied.
Given below is my code
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Adafruit_NeoPixel.h>
char auth[] = "L8dB9Ar7zsxgTRxxzYvJSZgxmfsYIE3D";
char ssid[] = "seiryū";
char pass[] = "password";
#define LED_PIN 5 // Which pin on the Arduino is connected to the NeoPixels?
#define LED_COUNT 25 // How many NeoPixels are attached to the Arduino?
int brightness = 255;
int choice = 1;
int R,G,B,Mode,state;
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800); // Declare our NeoPixel strip object:
// setup() function -- runs once at startup --------------------------------
void setup() {
// Debug console
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
strip.show(); // Turn OFF all pixels ASAP
}
BLYNK_WRITE(V0){
R = param[0].asInt();
G = param[1].asInt();
B = param[2].asInt();
if(Mode == 0 && state == 1)
Static(strip.Color(R, G, B), brightness);
}
BLYNK_WRITE(V1){
Mode = param.asInt();
switch(Mode){
case 1:{
Static(strip.Color(R, G, B), brightness);}
break;
case 2:{
animation(choice);}
break;
default:{
onoff();}
break;
}
}
BLYNK_WRITE(V2){
choice = param.asInt();
if(Mode == 2 && state == 1)
animation(choice);
}
BLYNK_WRITE(V3){
brightness = param.asInt();
if(Mode == 1 && state == 1)
Static(strip.Color(R, G, B), brightness);
else if(Mode == 2 && state == 1)
animation(choice);
}
BLYNK_WRITE(V4){
state = param.asInt();
if(state == 0)
onoff();
else if(state == 1 && Mode == 1)
Static(strip.Color(R, G, B), brightness);
else if(state == 1 && Mode == 2)
animation(choice);
}
// loop() function -- runs repeatedly as long as board is on ---------------
void loop() {
Blynk.run();
}
// Some functions of our own for creating animated effects -----------------
void Static(uint32_t rgb, int brightness){
strip.setBrightness(brightness);
for(int i = 0; i < strip.numPixels(); i++)
{strip.setPixelColor(i, rgb);
}strip.show();
}
// #Animation 1
void colorWipe(uint32_t color, int wait, int brightness) {
strip.setBrightness(brightness);
for(int i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, color);
strip.show();
delay(wait);
}
}
// #Animation 2,3,4
void theaterChase(uint32_t color, int wait, int brightness) {
strip.setBrightness(brightness);
for(int a=0; a<10; a++) { // Repeat 10 times...
for(int b=0; b<3; b++) { // 'b' counts from 0 to 2...
strip.clear(); // Set all pixels in RAM to 0 (off)
// 'c' counts up from 'b' to end of strip in steps of 3...
for(int c=b; c<strip.numPixels(); c += 3) {
strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
}
strip.show(); // Update strip with new contents
delay(wait); // Pause for a moment
}
}
}
// #Animation 5
void rainbow(int wait, int brightness) {
strip.setBrightness(brightness);
for(long firstPixelHue = 0; firstPixelHue < 5*65536; firstPixelHue += 256) {
for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
}
strip.show(); // Update strip with new contents
delay(wait); // Pause for a moment
}
}
// #Animation 5
void theaterChaseRainbow(int wait, int brightness) {
strip.setBrightness(brightness);
int firstPixelHue = 0; // First pixel starts at red (hue 0)
for(int a=0; a<30; a++) { // Repeat 30 times...
for(int b=0; b<3; b++) { // 'b' counts from 0 to 2...
strip.clear(); // Set all pixels in RAM to 0 (off)
for(int c=b; c<strip.numPixels(); c += 3) {
int hue = firstPixelHue + c * 65536L / strip.numPixels();
uint32_t color = strip.gamma32(strip.ColorHSV(hue)); // hue -> RGB
strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
}
strip.show(); // Update strip with new contents
delay(wait); // Pause for a moment
firstPixelHue += 65536 / 90; // One cycle of color wheel over 90 frames
}
}
}
// #ON/OFF
void onoff(){
for(int i=0; i<strip.numPixels(); i++)
{
strip.setPixelColor(i, strip.Color(0, 0, 0));
}strip.show();
}
// # For Animation mode
void animation(int Data){
switch(Data){
case 2:{
colorWipe(strip.Color(255, 0, 0), 50, brightness); // Red
colorWipe(strip.Color( 0, 255, 0), 50, brightness); // Green
colorWipe(strip.Color( 0, 0, 255), 50, brightness); // Blue
}
break;
case 3:{
theaterChase(strip.Color(255, 255, 255), 50, brightness);}
break;
case 4:{
theaterChase(strip.Color(255, 0, 0), 50, brightness);}
break;
case 5:{
theaterChase(strip.Color( 0, 0, 255), 50, brightness);}
break;
case 6:{
rainbow(10, brightness); // Flowing rainbow cycle along the whole strip
theaterChaseRainbow(50, brightness);} // Rainbow-enhanced theaterChase variant
break;
default:{
onoff();}
break;
}
}