[SOLVED] Problem with "if" (understanding proper use of 'if' in BLYNK_WRITE() function)

Hello.

Im new with Blynk, and with arduino. i want to build a security system for my garage.
I use ESP8266 e12, and its working well with blynk test files, but i cant do a working “if” module.
If the mosion sensor swithed on, its sent to me a notification, but i think is overflood, because of to much notification.
The microwave motion sensor send 5v every secound again, if motion is detected.
I want to use simpletimer and “if” but “if” doesnt want to working on any spot in the code.
I tryed to paste it in to the repeatMe, in to the menu selection, or in to the voidloop, but nothing happend.
On iphone the V2 is the button, and V15 value display, but nothing happend.
Where is my fault?
Thank you!

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>//ez is a timerhez teszt
SimpleTimer timer;        //ez is a timerhez teszt

char auth[] = "a2e450bd5***";

// Your WiFi credentials.
char ssid[] = "VeX's iPhone";
char pass[] = "***";

void repeatMe() {

    Serial.print("Uptime (s): ");       //kiírja mióta megy a modul

   Serial.println(millis() / 1000);    //kirja mióta megy a modul


if (V2 == 1){             
        Blynk.virtualWrite(V15, "Bekapcs");    
}

}


// Menu és működik innentől

BLYNK_WRITE(V1) {
  switch (param.asInt())
  {
    case 1: // Item 1

      Serial.println("Item 1 selected");

        Blynk.virtualWrite(V5, "Mozog");     

      break;

    case 2: // Item 2

      Serial.println("Item 2 selected");

        Blynk.virtualWrite(V5, "Robban");

      break;

    case 3: // Item 3

      Serial.println("Item 3 selected");

        Blynk.virtualWrite(V5, "Kikapcs");

      break;

    default:

      Serial.println("Unknown item selected");
  }

}
// Menu és működik idáig


void notifyOnButtonPress()

{

  // Invert state, since button is "Active LOW"

  int isButtonPressed = !digitalRead(4);

  if (isButtonPressed) {

    Serial.println("Button is pressed.");


    // Note:

    //   We allow 1 notification per 15 seconds for now.

    Blynk.notify("Mozgás az 1.");

  }

}



void setup() {
  Serial.begin(9600);           // Debug console

  Blynk.begin(auth, ssid, pass);

//Ez indítja az ismételt folyamatot

    timer.setInterval(1000, repeatMe);

  // Setup notification button on pin 2

  pinMode(4, INPUT);

 // Attach pin 2 interrupt to our handler

  attachInterrupt(digitalPinToInterrupt(4), notifyOnButtonPress, CHANGE);

}

void loop()

{

  Blynk.run();

  timer.run();

}

I don’t really see what you did wrong but here is an example where i used if.

BLYNK_WRITE(V1)
{
if (String(“xxxxxxxxxx”) == param.asStr()) {
terminal.println(“Password accepted”) ;
Blynk.notify(“Warning, someone succesfully accessed camera controls”);
digitalWrite (4, LOW);
} else {
terminal.print(“Password denied”);
Blynk.notify(“Warning, someone tried to access camera controls”);
Blynk.email(“xxxxxxxxxxxxxx”, “Failed camera login”, “Warning, someone tried to access camera controls”);
terminal.write(param.getBuffer(), param.getLength());
terminal.println();
digitalWrite (4, HIGH);
}

I am not quite understanding what your code is doing… you seem to have a single timer for UpTime, and some form of case switch for whatever? But I haven’t really identified the actual motion sensor scanning loop, let alone the where and why for an “if loop”.

I think you need to find an example sketch for whatever sensor you are using and get it fully functional with just basic Arduino code. Then once you have that figured out, you can start merging it in with whatever Blynk functions you need… simple notifications I guess?

You cant understand the functions, becaus not compleate, the menu is just test, now write V5 somthing what i see in iphone, so i know its working. It will be change, but there want to use “if” but doesnt worked :frowning:
Later i want that, if i change from the menu, chase 2, and if V2 is HIGH the digital output 13 is high.
But doesnt worked, so i tryed it step by step.

The motion sensor is give 5v if motion is detected and i tryed it and worked, but later in test i just add manually the simple 5 v to the port, and it sent a push notification every time, and only once allowed by 15 sec, and it sent every 2-3 sec so overflooded. I want to solve this, with simpletimer, and if, but i cant solve it :frowning:

The main question fom me, why not working this piece of code:
V2 is button on iphone Blynk app on virtual V2.
V15 is value display.
If i put the "Blynk.virtualWrite(V15, “Bekapcs”); " out of “if” its appear on phone, but with this code need to work, if i think well: if i push the button, write “Bekapcs” but nothing happend.
Why?

if (V2 == 1){             
        Blynk.virtualWrite(V15, "Bekapcs");    
}

Well, you clearly have a basic grasp of timer and basic Blynk commands… unfortunately the rest is down to general programming knowledge.

Depending in the sensor type, whatever libraries it may or may not require, and the application you need it to work in, there could be dozens of ways to make it work, with or without Blynk integrations… But this forum is not a code factory. We will help as best able with your understanding of Blynk commands and related issues, but the “learning to program” requirement is really up to you.

You first have to “populate” V2 (which will need to be a different variable name than that) with a value from the virtual pin V2

BLYNK_WRITE(V2)  // This function gets called whenever something happens to the Widget attached to this vPin
{   
  int value = param.asInt(); // Get value as integer
    if (value == 1) {             
        Blynk.virtualWrite(V15, "Bekapcs");    
    }
}
1 Like

I will try it at afternoon. Thank you so much!

Its working, thank you so much!