Blynk 2.0 virtual pin HIGH/LOW invert state

Hi, I’m using Blynk 2.0 and NodeMcu to control a relay with a led strip connected, but I can’t invert the status of the button (V1). Very simple and fast thing in the Blynk 1.0 version where you can decide the value of the inverted button, that is 1 off 0 on.

#define pulsante 5
BLYNK_WRITE(V1) {  digitalWrite(pulsante, param.asInt());}


int stato = 0 ;
void Pulsanti() {

 if (digitalRead(D1) == LOW  ){
 Blynk.virtualWrite(V1, LOW); 
}
 if (digitalRead(D1) == HIGH   ){
 Blynk.virtualWrite(V1, HIGH); 
 }

}

@seby please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Pete.

1 Like

Hi Pete done thanks.

The logical NOT (the exclamation mark character “!”) is probably the easiest way…

#define pulsante 5
BLYNK_WRITE(V1) {  digitalWrite(pulsante, !param.asInt());}

I’m not sure where the rest of the code you’ve posted comes in to the mix.

Pete.

1 Like

Here is the complete code where with theV1 button the led strip connected to the relay lights up, instead through the V2 the brightness of the strip through PWM and transistor TIP 41C NPN Everything works perfectly through Blynk 1.0

#ifndef Pins_Arduino_h #define Pins_Arduino_h #define PIN_WIRE_SDA (4) #define PIN_WIRE_SCL (5)
static const uint8_t SDA = PIN_WIRE_SDA; static const uint8_t SCL = PIN_WIRE_SCL;

static const uint8_t D0   = 16;  static const uint8_t D1   = 5;  static const uint8_t D2   = 4;
static const uint8_t D3   = 0;   static const uint8_t D4   = 2;  static const uint8_t D5   = 14;
static const uint8_t D6   = 12;  static const uint8_t D7   = 13; static const uint8_t D8   = 15;
static const uint8_t RX   = 3;   static const uint8_t TX   = 1;
#include "../generic/common.h"
#endif /* Pins_Arduino_h */

#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "***-"
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>

char ssid[] = "**"; char pass[] = "***"; char auth[] = "**"; //TEST

#include <Ticker.h>
Ticker timer; Ticker timer1; 

void flip() {
  int state = digitalRead(LED_BUILTIN);  // get the current state of GPIO1 pin
  digitalWrite(LED_BUILTIN, !state); }    // set pin to the opposite state

#define pulsante  5

BLYNK_WRITE(V1) {  digitalWrite(pulsante, param.asInt());}

BLYNK_WRITE(V2) { analogWrite(D2, param.asInt());}

int stato = 0 ;
void Pulsanti() {

 if (digitalRead(D1) == LOW && stato == 0 ){
 Blynk.virtualWrite(V1, HIGH); 
 analogWrite (D2,255);
 Blynk.virtualWrite (V2,255);
 stato = 1;
}
 if (digitalRead(D1) == HIGH && stato == 1  ){
 Blynk.virtualWrite(V1, LOW); 
 analogWrite (D2,0);
 Blynk.virtualWrite (V2,0);
 stato = 0;}

}
 void setup() { 
  Serial.begin(115200);  
  digitalWrite (D1,HIGH);
  analogWrite(D2,LOW);
  pinMode(D1,OUTPUT); // 
  pinMode(D2,OUTPUT); //   
  pinMode(LED_BUILTIN, OUTPUT);//D0
  digitalWrite(LED_BUILTIN, LOW);//D0
  Serial.println("Booting");
  WiFi.mode(WIFI_STA); 
  WiFi.begin(ssid, pass);
  
  while (WiFi.waitForConnectResult() != WL_CONNECTED) {
  Serial.println("Connesseione fallita ! Riavvio...");
  delay(5000);
  ESP.restart();}

  Blynk.begin(auth, ssid, pass);
  
  timer.attach(1, flip);
  timer1.attach(1, Pulsanti);

  Blynk.virtualWrite(V1,HIGH);
  Blynk.virtualWrite(V2,LOW);
  
//ArduinoOTA.setPort(8266); 
  ArduinoOTA.setHostname("I***");
  ArduinoOTA.setPassword((const char *)"**");
  ArduinoOTA.begin();
  Serial.println("Ready");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  Blynk.run();
  ArduinoOTA.handle();
  while (WiFi.waitForConnectResult() != WL_CONNECTED) {
  Serial.println("Connesseione fallita ! Riavvio...");
  delay(5000);
  ESP.restart();}

    }

So does that mean you didn’t like my solution?

Pete.

1 Like

I just tested it and it works great: slight_smile:

The stuff that yoire doing with writing the V1 value to GPIO5 (Pin D1 on the NodeMCU) is very clunky.

First of all, mixing GPIO numbers and D numbers makes the code very difficult to follow.
It also seems to be a work-around for simply using global variables or passing parameters to a function, but if you’re happy and it works for you then that’s all that matters.

Pete.

2 Likes