Help with Virtual Pins

As I said in an earlier post I’m a noob.

I’m using a NodeMCU with Blynk on a Android phone.
I’m setting up a virtual button and trying to also use a Virtual LED to show when the button is in a high state.

The problem I’m having is the virtual LED only gets a quarter line on the outside. I does not fill in. It’s like it’s only reading a partial state between low and high. I’m sure it’s in my coding; but I can’t find the issue. Please forgive me if I didn’t format my code right.

#define BLYNK_PRINT Serial


#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#define N_DIMMERS 3
#define led_pin D0
int dimmer_pin[] = {14, 5, 15};


char auth[] = "**";

char ssid[] = "**";
char pass[] = "**";
char host[] = "ESP-OTA";


// Select your pin with physical button
const int btnPin = D2;
int btnState = LOW;

WidgetLED led3(V20);


BlynkTimer timer;

BLYNK_CONNECTED() {
  Blynk.syncAll();
}

void LedWidget() {
  if  (digitalRead(btnPin) == HIGH) {  // Sets LED widget ON or OFF depending on state of btnpin
    led3.on();

  } else {
    led3.off ();

  }
}
void updateButtonWidgetIfNeeded() {
  int newBtnState = digitalRead(btnPin);
  if (btnState != newBtnState) {
    btnState = newBtnState;
    Blynk.virtualWrite(V20, btnState);
  }
}

BLYNK_WRITE(V21) {
  digitalWrite(btnPin, param.asInt());
  updateButtonWidgetIfNeeded();
}

void ButtonWidget() {
  if  (digitalRead(btnPin) == HIGH) {  // Sets LED widget ON or OFF depending on state of btnpin

    Blynk.virtualWrite(V20, HIGH);
  } else {

    Blynk.virtualWrite(V20, LOW);
  }
}



void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  pinMode(led_pin, OUTPUT);
  digitalWrite(led_pin, LOW);
  pinMode(btnPin, OUTPUT);
  timer.setInterval(1000L, updateButtonWidgetIfNeeded);
  Serial.println("Booting");

  WiFi.mode(WIFI_STA);



  Blynk.begin(auth, ssid, pass);
  // WiFi.begin(ssid, password);



  while (WiFi.waitForConnectResult() != WL_CONNECTED) {

    Blynk.begin(auth, ssid, pass);
    // WiFi.begin(ssid, password);

    Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
    Serial.println("Retrying connection...");

  }

  /* switch off led */

  digitalWrite(led_pin, HIGH);



  /* configure dimmers, and OTA server events */

  analogWriteRange(1000);

  analogWrite(led_pin, 990);



  for (int i = 0; i < N_DIMMERS; i++) {

    pinMode(dimmer_pin[i], OUTPUT);

    analogWrite(dimmer_pin[i], 50);

  }



  ArduinoOTA.setHostname(host);

  ArduinoOTA.onStart([]() { // switch off all the PWMs during upgrade

    for (int i = 0; i < N_DIMMERS; i++) {

      analogWrite(dimmer_pin[i], 0);

    }

    analogWrite(led_pin, 0);

  });



  ArduinoOTA.onEnd([]() { // do a fancy thing with our board led at end

    for (int i = 0; i < 30; i++) {

      analogWrite(led_pin, (i * 100) % 1001);

      delay(50);

    }

  });



  ArduinoOTA.onError([](ota_error_t error) {

    (void)error;

    ESP.restart();

  });



  /* setup the OTA server */

  ArduinoOTA.begin();

  Serial.println("Ready");


}

BLYNK_WRITE(V20) {
  digitalWrite(btnPin, param.asInt());  // Sets btnPin HIGH or LOW depending on state of Button Widget.
}


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

Sorry, but you still haven’t formatted your code properly for viewing on this forum… use the correct backtick keys

Blynk - FTFC

The directions on how was in the text you removed when posting this topic (fine to remove them… but please read first :stuck_out_tongue_winking_eye: )

Was also in the Welcome Topic that all new users should thoroughly read before posting…

As for your Virtual LED… just like real LEDs it can be ON, OFF or a variant in between.

When setting the value with a Blynk.virtualWrite() command you are akin to setting a PWM value with a range of 0-255. So HIGH = 1 = Low intensity (not OFF, but just barely ON… thus only the edge “lights” up).

Try this…

WidgetLED led3(V20);

Blynk.virtualWrite(V20, 255);  // Full intensity

Widget LEDs also have their own distinct commands, so led3.on(); will do the same full intensity thing as the command above

And finally, when using the distinct commands… presetting the intensity with a led3.setvalue(127); (in Setup or elsewhere in code) will turn ON a vLED, but at the mid-level intensity instead of the default full intensity.

1 Like

PS… more fun with Virtual LEDs using Blynk.setProperty(vPin, “property”, value)

Thank you Gunner,

WidgetLED led3(V20);

Blynk.virtualWrite(V20, 255); // Full intensity

Did not work. It caused the led and button to both flash rapidly.

I’ll keep working on it. I sent the sketch to a friend and it works on his iPhone.

Thanks for the help Gunner I have it solved. The vLED I was trying to light was Set the same as the button and they need to be different in the sketch and Blynk. Doh!!!

It’s a steep learning curve.

1 Like