BLYNK
BLYNK.IO       📲 GETTING STARTED       📗 DOCS       👉 SKETCH BUILDER

Virtual pin in If statement

Hello. Can anyone help me with this problem. I have a light that I want to turn on when a sensor is triggered, but also if a button from the app is pushed. Also to see in the app is the light is on or off and if it is on to get a notification. The button is V0, and V2 is the sensor. Now the light is turning on if the sensor is triggerd but if I push the button from the app the light goes fast on-off.
This is what I have so far.

#define BLYNK_TEMPLATE_ID "***"
#define BLYNK_DEVICE_NAME "***"
#define BLYNK_AUTH_TOKEN "***"
#define BLYNK_FIRMWARE_VERSION        "0.1.0"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "***";
char pass[] = "***";

BlynkTimer timer;

int LightS=35;
int LightR=2;

BLYNK_WRITE(V0)
{
  int LightV = param.asInt();
  digitalWrite(LightR,LightV);
}

void sendSensor()
{
  int LightV=analogRead(LightS);
  Blynk.virtualWrite(V2,LightV);
  
  if (V0 == 0) {
    if (LightV < 1) {
    digitalWrite(LightR,HIGH);
    Blynk.virtualWrite(V0,1); 
    } else {
    digitalWrite(LightR,LOW);
    Blynk.virtualWrite(V0,0);
    }
  }
}
void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  pinMode(2,OUTPUT);
  timer.setInterval(1000L, sendSensor);
  delay(100);
}

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

How did you configure your datastream ?

It’s a pity you didn’t take my advice…

Pete.

Why are you using LightV as a local variable in two different functions?

If you want to achieve what you’ve described then you should be using two different global variables.

Pete.

This are the settings.

I don’t know how else to do it.

What is the additional information that would you like to know?

I’d suggest you learn about C++ variable scope.

When you created this topic you were asked to do certain things, and to provide certain information.
It appears you deleted that guidance information without reading it, despite me already asking you not to.

Create a new “need help…” topic and look at what you’re prompted to do/provide, then abort the process before actually creating the topic.

Pete.

The hardware is esp32, Smartphone OS is Android 10 and I’m using the Blynk server.

You are asked to provide your Blynk app version and Blynk library version, neither of which you seem to think are important enough to share.

Pete.

What is the relevance of this data for my problem?

The point I’m making is that you started by hijacking someone else’s topic, and I asked you to create your own topic and provide ALL of the requested information.
That requested minimum set of data is extremely useful, and prevents us having to ask all sorts of follow-up information to get to the bottom of the issue.
You clearly don’t know what information is or isn’t relevant, because you’ve come here looking for assistance with an issue you don’t understand.
If you can’t be bothered to do us the courtesy of providing that basic Information, even after being asked multiple times to provide it, the why should we donate out time and expertise to try to assist you?

Pete.

1 Like

First of all, I did not hijacking someone else’s topic, I found a topic that had a problem similar to mine and there is rule of thumb, if on a forum you find a post similar to what you are looking for, don’t create a new one.
Second, what is the relevance of knowing the Blynk app version if I never said that I’m using the app?
Third, I don’t thing that an answer like “learn about …” is a productive answer.
And fourth how many topics are providing ALL the information from the first post?

You found a topic which was three years old (before the current version of Blynk was launched) and which had lots of issues caused by poor code structure, syntax errors and lack of programming skills.
Whilst your code may also have suffered from some of these issues, that’s where the similarities stopped.

It gives us valuable information about whether you are using the version of Blynk which was being used in the post you hijacked, or the new version of Blynk. It also gives us structured data in an “at a glance” format that speeds up the process of allowing us to quickly understand your platform and versions to eliminate any potential conflicts. You would be surprised how often this information is useful in getting to the root cause of an issue.

This is a forum for Blynk related issues, not a C++ 101 teaching forum. There are plenty of resources on the internet to teach people the basics of C++ programming, including variable scope. We prefer to keep this forum focussed on Blynk.

Thankfully, quite a few. But, when you’ve been specifically asked to create a new topic of a particular type, because of the questions it will prompt you to answer, then don’t do that and repeatedly refuse to provide that information even when specifically asked to do so, then we tend to withdraw out input.
The small number of active contributors to this forum donate their time to help people who want to be helped. When people don’t work with us we tend to move on to help other people instead.

This is all I’m saying in this topic, unless I need to step-in for moderating reasons, so I’ll, wish you good luck with your project.

Pete.

Fortunately I managed to figure it out without the help of some one how is here more to critique then to help.

Here is the code:

#define BLYNK_TEMPLATE_ID "***"
#define BLYNK_DEVICE_NAME "***"
#define BLYNK_AUTH_TOKEN "***"
#define BLYNK_FIRMWARE_VERSION        "0.1.0"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "***";
char pass[] = "***";

BlynkTimer timer;

int LightS = 35;
int LightR = 21; 
int LightStat;
int LightV;

BLYNK_WRITE(V0) {
  LightStat=param.asInt();
}

void sendSensor()
{
  LightV=analogRead(LightS);
  Blynk.virtualWrite(V2,LightV);
  if (LightStat == 1) {
  digitalWrite(LightR,HIGH);
  Blynk.virtualWrite(V0,1);    
  } else {
    if (LightV < 1) {
    digitalWrite(LightR,HIGH);
    Blynk.virtualWrite(V0,1); 
    } else {
    digitalWrite(LightR,LOW);
    Blynk.virtualWrite(V0,0);
    } 
  }
}
void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  pinMode(21,OUTPUT);
  timer.setInterval(1000L, sendSensor);
  delay(100);
}

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