Basic functions that can be used in any project

Hello

I have 5 interesting projects with blynk (exterior lighting control, auxiliary heater thermostat, RGB led strip, desktop led lighting and doorway gate control), and have purchased and used almost 15000 blynk energy. But all my projects were made by copying codes from other projects and examples and it turned out that I do not know how to implement some really basic things since in my every project they were solved in a diferent way…

I am kindly asking if someone could correct my examples and put exact code that then be used in any of the project.

Case1: Relay board is connected to gpio5. Since each pin could be used only once, you need to trigger it by virtual pins. To create virtual pin I use

    BLYNK_WRITE(V1)
{
  int buttonState = param.asInt();
  if (param.asInt()==1) 
  {
    digitalWrite(5, HIGH);
  } 
else
  {
    digitalWrite(5, LOW);
  }
}

Is this correct? What if I would like that if digital pin is triggered by another virtual pin, it will also reflect in changed button state on V1?

Case2: I would like to turn relay on by triggering V1 on switch and to have it turned off automatically after some predefined time (eg 30s) .
I have read this

// thirtysecondtimer.ino by Costas 12 Dec 2016
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>      // library for SimpleTimer       
SimpleTimer timer;            // define a timer for use by SimpleTimer library
int Countdown;                // Global variable used in Slider widget and runEveryMinute()
bool ONstatus  = false;       // variable to switch device ON and OFF
char auth[] = "YourAuthToken";
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";
BLYNK_WRITE(V0){   // add a slider to your project on V0 range 0 to 30 (minutes)
  Countdown = param.asInt();  // set variable as Slider value
}
void runEveryMinute(){ // runs every 60s, will do noting when Slider is at zero
  
  if((Countdown > 0)&& (ONstatus == true)){
    Countdown--;    //  reduce Countdown by 1 minute every 60s
    Serial.print(F("Device will switch OFF in "));
    Serial.print(Countdown);
    Serial.println(F(" minute(s)"));    
  }  
  
  if((Countdown > 0) && (ONstatus == false)){
    Serial.println(F("Device was switched ON"));
    ONstatus = true;   // to ensure device is only turned ON once
    // code here to turn your device ON
    digitalWrite(14, HIGH); // send ON signal to relay          

but I still do not know how to use slider only to define time, but trigger on by pressing the button. Or in some cases even not include slider in application but define preset time in code.

Case3: LED control
I feel stupid, but I still did not manage how to make it work. I would just like that LED widget will show the actual state of digital pin (e.g. gpio5), no matter by which virtual pin it was triggered.
Blynk.virtualWrite(V7,5)

Yes it is correct.
If you change with another virtual pin you need to call virtualWrite within the other virtual pin function. So if V2 was your second controller it would be:

BLYNK_WRITE(V2)
{
  int buttonState = param.asInt();
  if (param.asInt()==1) 
  {
      digitalWrite(5, HIGH);
      Blynk.virtualWrite(V1, 1);  // or HIGH instead of 1
  } 
else
  {
      digitalWrite(5, LOW);
      Blynk.virtualWrite(V1, 0);  // or LOW instead of 0
  }
}

The 5 you have in virtualWrite has nothing to do with GPIO numbers. It is a setting between 0 and 255 for the intensity of an LED. For virtual LED’s we use 0 (off) and 255 (on). It depends on your project how you would control the virtual LED.

It can be done similar to Case1 or if you also have a physical LED by using digitalRead() at intervals etc.

Can you please help me also with timer?

For Case2 it means learning how SimpleTimer works.

There was a good example posted a few weeks ago using the timeout feature.
Study this thread in detail for inspiration and work through the code [SOLVED] Virtual button pressed for X seconds

PushData sketch, available from Sketch Builder link at the top right of this page, is also a very basic example of how SimpleTimer works.