BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

[SOLVED] Using BLYNK_WRITE() with loop()

A virtual switch button is assigned to V22 and a virtual LED is assigned to V0.
Here’s the code I’m using:

#include <SoftwareSerial.h>
SoftwareSerial DebugSerial(2, 3); // RX, TX

#define BLYNK_PRINT DebugSerial
#include <BlynkSimpleStream.h>

char auth[] = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";


WidgetLED led(V0);

void setup()
{
  DebugSerial.begin(9600);
  Serial.begin(9600);
  Blynk.begin(Serial, auth);
}

int buttonState;
BLYNK_WRITE(V22)
{
  buttonState = param.asInt();
}

void loop()
{
  Blynk.run();
  if (buttonState)
  {
    led.on();
  }
  else
  {
    led.off();
  }
}

I expected BLYNK_WRITE() to assign the HIGH/LOW state of the button to the buttonState variable and then use it to turn the LED on or off in the loop() function. Unfortunately, it the LED always turns out to be turned off, i.e. the button has no effect.

Note that the code works if I move the if-else condition to the BLYNK_WRITE() function, but I want to use buttonState in the loop() function. It’s because this code is just a short and simplified version of the original code.

Do you have a particular reason for needing the if() else in the void loop()??

As I have experienced, it is best to keep the void loop() very bare. Besides, I believe the BLYNK_WRITE() function will automatically run whenever you press the virtual button anyhow, thus running your if() else as needed, as long as it is placed in the BLYNK_WRITE() function.

2 Likes
2 Likes

digitialRead(), digitalWrite() and analogWrite() functions don’t work inside BLYNK_WRITE. That’s why I wrote it inside loop().

However, the code worked when I used the SimpleTimer library later. I just had to write the if-else statements inside a timer function. The problem is solved. Thank you.

@zadspecial Glad you found a solution.

However, you might be interested to know that this is NOT true. A BLYNK_WRITE() Function is a loop that gets called whenever the associated linked item (i.e. a button) gets triggered.

For example, the following was a test to see if I could continuously, and rapidly, blink an LED, while a button is pressed/switched on, and still run the rest of the program in the meantime.

BLYNK_WRITE(V1) // Run this function when V1 button pressed.
{
     int pinValue = param.asInt();  // Get status of V1.
    if (pinValue == 1) {  // If status of V1 is 1 then do stuff in if().
      digitalWrite(3, HIGH); // Turn on LED.
      delay(20); // Wait 20 millis but remember you are holding up the show.
      digitalWrite(3, LOW);  // Turn off LED.
      Blynk.run(); // Run rest of show in-between waiting for this loop to repeat or quit.
      int pinValue = 0;  // Set V1 status to 0 to quit, unless button is still pushed (as per below)
      Blynk.syncVirtual(V1); // ...Then force BLYNK_WRITE(V1) function check of button status to determine if repeating or done.
  }
}
5 Likes

Hello @GunnerTechTools can you show all the example code? I am very interested. Thanks.

Please take note of time/date stamps before reopening old topics. this is almost a year old.

And sorry, that last post WAS the entire example code… it was small… hence the term “example” :stuck_out_tongue_winking_eye: