How to add ON/OFF button in blynk

Hello Everyone,

I need a help to add a ON/OFF button in my blynk project. Im trying to control my LED strips with blynk . Im trying to control the brightness with the slider, Animation with button, Color with zeRGB and i also want to add ON/OFF button. Please help me to figure it out.
Thank you in advance.

Below is my code for the project

#include <Blynk.h>

#include <FastLED.h>
#include <Adafruit_NeoPixel.h>

#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp8266.h>
#define FASTLED_ESP8266_RAW_PIN_ORDER
#include “FastLED.h”
#define NUM_LEDS1 120
#define LED_TYPE WS2812
#define COLOR_ORDER GRB //CHANGED GRB TO RGB IF STILL SAME CHANGE BACK TO GRB
CRGB leds1[NUM_LEDS1];
char auth = “******************";
char ssid[] = "";
char pass[] = "
”;
#define PIN1 D2

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUM_LEDS1,PIN1, NEO_GRB + NEO_KHZ800);

int data=255;
int r,g,b;
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
pixels.begin();
FastLED.addLeds<LED_TYPE, PIN1, COLOR_ORDER>(leds1, NUM_LEDS1).setCorrection( TypicalLEDStrip );
}

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

BLYNK_WRITE(V2)
{
r = param[0].asInt();
g = param[1].asInt();
b = param[2].asInt();
if(data==0)
static1(r,g,b);
}

BLYNK_WRITE(V1)
{
data = param.asInt();
static1(r, g, b,data);
}
void static1(int r, int g, int b,int brightness)
{
FastLED.setBrightness(brightness);
for (int i = 0; i < NUM_LEDS1; i++ )
{
leds1[i] = CRGB(r, g, b);
}
FastLED.show();
}

BLYNK_WRITE(V3)
{
data = param.asInt();
Serial.println(data);
if(data==0)
{
static1(r,g,b);
}
else if(data==1)
{
animation1();
}

}
void static1(int r, int g, int b)
{
for(int i=0;i<=NUM_LEDS1;i++)
{
pixels.setPixelColor(i, pixels.Color(r,g,b));
pixels.show();
}
}
void animation1()
{
for(int i=0;i<NUM_LEDS1;i++)
{
pixels.setPixelColor(i, pixels.Color(255,0,0));
pixels.show();
delay(100);
}
for(int i=NUM_LEDS1;i>=0;i–)
{
pixels.setPixelColor(i, pixels.Color(0,255,0));
pixels.show();
delay(100);
}
for(int i=0;i<NUM_LEDS1;i++)
{
pixels.setPixelColor(i, pixels.Color(0,255,255));
pixels.show();
delay(100);
}
for(int i=NUM_LEDS1;i>=0;i–)
{
pixels.setPixelColor(i, pixels.Color(255,255,0));
pixels.show();
delay(100);
}
}

Your topic has been moved here as this forum section is more appropriate than where it was originally posted.

The easier you make it to read and copy the code the more likely it is that you will get help

Please follow the advice given in the link below when posting code

1 Like

we need a sticky “How to post code”

From experience it did very little good on the old forum

I don’t think that it has been implemented yet but moves are afoot to direct new users to How to get the best out of this forum when they first log on. Being a cynic I suspect that most will ignore the advice however it is presented.

I assume too that they will also continue to post in the wrong forum section and their posts will need to be moved. I have lost track of how many topics I have moved but luckily it is easy to do

i’d like to reiterate that we need a sticky entitled “</> - How to post your code”, not one that describes how (of course it should)

i think few having a problem posting code would look under a post entitled “how to get to get the best out of this forum”

i admire your endurance

And get rid of this “</>” symbol, it doesn’t even resemble code tags.

The problem is that they don’t know that they have a problem in the first place !

I actually don’t think that such a sticky would be a bad idea, just that nobody would take any notice of it any more than they did previously with the prominently displayed sticky topic Read this before posting a programming question ...