Photon/Blynk Virtual LED Problem

Hello,
I want to write a Blynk app using a Particle Photon. I’m new to Blynk and I need help with getting a virtual LED to go on when a virtual button is pressed.
My Blynk app consists of 2 Virtual buttons, fan and led (V0 and V1 respectively) and on the Photon, D6 and D7 respectively . It also has 2 virtual LED’s, led1 and led2 (V4 and V3 respectively). My objective is that when Virtual button V0 is pressed, the following happens, it turns on small fan connected to pin D6 on the Photon, via a transistor and the Virtual LED , led1, turns on. Same when Virtual button V1 is pressed, the Photon on board LED turns on (D7) and Virual LED led2 turns on. When I flash the photon with my code, the an turns on and the Photon on board LED turns on but none of the virtual LED’s.
I have tried to get my code to do so but in vain. Following is the code , can anyone help?

// This #include statement was automatically added by the Particle IDE.
#include “blynk/blynk.h”

char auth[] = “My authorisation code”;
int fan = D6;
int led = D7;
bool buttonStatus;
WidgetLED led1(V4);
WidgetLED led2(V3);

void setup()
{
Serial.begin(9600);
pinMode(fan, OUTPUT);
pinMode(led, OUTPUT);
Blynk.begin(auth);

//bool buttonStatus ;
while (Blynk.connect() == false) {

}
}

BLYNK_WRITE(V0){
if (buttonStatus == HIGH) {

  digitalWrite(fan, HIGH);
  led1.off();

}
else

{ 
    digitalWrite(fan, LOW);
    led1.off();
}

}
BLYNK_WRITE(V1){
if (buttonStatus == HIGH) {
digitalWrite(led, HIGH);

led2.on();
}
else

{ 
    digitalWrite(led, LOW);
   led2.off();
}

}

void loop()
{
Blynk.run();

}

@Enzo I don’t see any way that you are changing buttonStatus in your sketch.

The normal procedure for virtual pins is:

BLYNK_WRITE(V0) { 
  int SomeVariable = param.asInt();
  if (SomeVariable == 1) {
    // do something
  }
  else{
    // do something different
  }
}

Hello Costa,

thank you for your suggestion.
I changed the code as follows and still having the same problem. What am I doing wrong?


// This #include statement was automatically added by the Particle IDE.
#include “blynk/blynk.h”

char auth[] = “16efab0214aa484d84d40e48ffb453f8”;
int fan = D6;
int led = D7;
int buttonStatusfan;
int buttonStatusled;
WidgetLED led1(V4);
WidgetLED led2(V3);

void setup()
{
Serial.begin(9600);
pinMode(fan, OUTPUT);
pinMode(led, OUTPUT);
Blynk.begin(auth);

//bool buttonStatus ;
while (Blynk.connect() == false) {

}
}

BLYNK_WRITE(V0){
int buttonStatusfan = param.asInt();
if (buttonStatusfan = 1) {

  digitalWrite(fan, HIGH);
 
  
  led1.on();

}
else

{ 
    digitalWrite(fan, LOW);
    led1.off();
}

}

BLYNK_WRITE(V1){
int buttonStatusled = param.asInt();
if (buttonStatusled = 1) {
digitalWrite(led, HIGH);
led2.on();
}
else

{ 
    digitalWrite(led, LOW);
   led2.off();
}

}

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


== not = for the if statements, = means set a variable, == means compare a variable.

Thank you Costa for your guidance. I solved the problem and also added an LCD display. Final code below:

#include “blynk/blynk.h”

char auth[] = “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”;
int fan = D6;
int led = D7;
int analogPin = A0;
int val;
int fanon = D3;

void setup()
{
Serial.begin(9600);
pinMode(fan, OUTPUT);
pinMode(led, OUTPUT);
pinMode(fanon, INPUT);
//IntervalTimer myTimer;
Blynk.begin(auth);

}

BLYNK_WRITE(V0){

int virtualbuttonclosed = param.asInt();
if (virtualbuttonclosed == 1)

{
digitalWrite(fan,HIGH);
Blynk.virtualWrite(V4,255);
Blynk.virtualWrite(V2,"FAN ON ");

}

else
{
digitalWrite(fan,LOW);
Blynk.virtualWrite(V4,0);
Blynk.virtualWrite(V2,“FAN OFF”);
}

}

BLYNK_WRITE(V1){

int virtualbuttonclosed = param.asInt();
if (virtualbuttonclosed == 1)
// if (V0 == 1)
{
digitalWrite(led,HIGH);
Blynk.virtualWrite(V3,255);
Blynk.virtualWrite(V5,"LED ON ");

}

else
{
digitalWrite(led,LOW);
Blynk.virtualWrite(V3,0);
Blynk.virtualWrite(V5,"LED OFF ");
}

}

void loop()
{
Blynk.run();

}

Hi Enzo.
I am using your example.
Could you clarify what you are doing with A0 and D3.
Another question I have is: how do I use the LCD widget on Blynk. Did you set it up as Simple or Advanced? Virtual pin V2 and V5? Did you use 2 LCD widgets?