BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Led Widget controlled by a Button Widget


#1

Hello There,
I wanted to control a Led Widget with a Button Widget over Virtual Pins. To turn on the Led, there’s no problem. But if I want to turn off the Led nothing happens.
In the code the “led2.on” command is colored orange but the “led2.off” isn’t…
What do I do wrong?
Here’s the code for the Led:

WidgetLED led2(V2);
BlynkTimer timer;

BLYNK_WRITE(V4){
  int pinValue = param.asInt();

  if(pinValue == 255){
    led2.on();
  }
  if(pinValue == 0){
    led2.off();
  }
}

#2

Hello,

Proper way to format posted code as per the instructions… (I already fixed your post)

As for your question… your code works fine… although you do not need the button to be 255 just to toggle the LED ON as you are using a different process then sending an intensity value.

As for the “colouring” double check your spelling and syntax in the actual code.

image

06 01


#3

Thanks for the very fast reply.
I took your advice and checked the code like 70 times but I just cand find the error…
I’ll post the whole code so maybe you see the error…
Ps.: Thx for the tutorial to post a proper code ;D

The BLYNK_WRITE(V0) and (V1) are for a Joystick. This part should not bother the Led…

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxx";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "yyy";
char pass[] = "zzz";




BLYNK_WRITE(V0)
{
  int pinValue = param.asInt();

if(pinValue < 250){
  digitalWrite(D3, HIGH);
  }
  else{ digitalWrite(D3, LOW);
  }
if(pinValue > 750){
  digitalWrite(D2, HIGH);
}
else{digitalWrite(D2, LOW);
}
}
BLYNK_WRITE(V1)

{

  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  // You can also use:
  // String i = param.asStr();
  // double d = param.asDouble();
  Serial.print("V1 Slider value is: ");
  Serial.println(pinValue);
  if(pinValue < 250){
    digitalWrite(D6, HIGH);
  }
  else{ 
    digitalWrite(D6, LOW);
  }
  if(pinValue > 750){
    digitalWrite(D5, HIGH);
  }
  else{
    digitalWrite(D5, LOW);
  } 
}
WidgetLED led2(V2);
BlynkTimer timer;

BLYNK_WRITE(V4){
  int pinValue = param.asInt();
  if(pinValue == 1){
    led2.on();
  }
  if(pinValue == 0){
    led2.off();
  }
}
void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
}

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

#4

I glanced at it, but don’t see any glaring issues. But a few pointers…

You code is scattered all over :stuck_out_tongue_winking_eye:

You are declaring your LED mid code, not up in pre setup()

You are declaring a BlynkTimer, but not using any timers anywhere??

All of your variables are local, and using the same name pinValue… probably not a big issue, but better if declared globally so you can share data outside of a function (if necessary).

https://www.arduino.cc/reference/en/language/variables/variable-scope--qualifiers/scope/

And using specific, purpose descriptive, variable names might make it easier to follow along in your code.


#5

Yeah sorry, I have this code for like a year where I write all my new ideas… but I will “glance” it and try again thanks!


#6

So now I have optimized it but it won’t work now either… Somewhere has to be an error… The Color of the led2.off() is still not orange wich is probably the reason it won’t work… Do you see this error? Could you show me your whole code for the problem so I can compare it to mine and maybe find the error please :slight_smile:
The code:

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxx";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "yyy";
char pass[] = "zzz";

WidgetLED led2(V2);

BLYNK_WRITE(V0)
{
  int pinValueV0 = param.asInt();

  if(pinValueV0 < 250){
    digitalWrite(D3, HIGH);
  }
  else{ 
    digitalWrite(D3, LOW);
  }
  if(pinValueV0 > 750){
  digitalWrite(D2, HIGH);
  }
  else{
    digitalWrite(D2, LOW);
  }
}
BLYNK_WRITE(V1)
{
  int pinValueV1 = param.asInt();
  
  if(pinValueV1 < 250){
    digitalWrite(D6, HIGH);
  }
  else{ 
    digitalWrite(D6, LOW);
  }
  if(pinValueV1 > 750){
    digitalWrite(D5, HIGH);
  }
  else{
    digitalWrite(D5, LOW);
  } 
}


BLYNK_WRITE(V4){
  int pinValueV4 = param.asInt();
  
  if(pinValueV4 == 1){
    led2.on();
  }
  if(pinValueV4 == 0){
    led2.off();
  }
}
void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
}

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

#7

image

You see? the on command is orange but the off is not…


#8

Nvm now it worked altrough the black Color… Thx for all!