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

Help with virtual pin read (I want to replace a physical button)

Hello. I have tried searching the internet for help but I am not knowledgeable at all in Arduino stuff, never mind in Blynk stuff! Here’s my issue:
I want to be able to tell Arduino UNO to run the void loop when the pinValue=1 i.e. when I push the virtual button on my phone. In order to do this, I tried writing in BLYNK_WRITE(V1) that when pinValue=1, it sets the value of a==1, and then runs the loop function. Inside my loop function, if a=1 then it does some stuff. I am connected through USB.
Here is the code I wrote for this:

#define BLYNK_PRINT SwSerial
#include <SoftwareSerial.h>
SoftwareSerial SwSerial(10, 11); // RX, TX
#include <BlynkSimpleStream.h>
#include <Servo.h>
#include <DFRobot_HX711.h>


int pinValue;
int servopin = 9;
int angle = 0; 

int button = 3;
float poids = 0;
int i=0;
int a=0;

char auth[] = "HXoG4hYsPCq5GtVQ15kGnCuw9Q-Kkg2J";

DFRobot_HX711 scale(A2, A3);
Servo servo; 

// This function will be called every time Slider Widget
// in Blynk app writes values to the Virtual Pin V1

BLYNK_WRITE(V1)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  Serial.print("V1 Slider value is: ");
  Serial.println(pinValue);
  if(param.asInt() == 1) {
    a==1;
    loop();
    
  }
  // process received value
}

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

  pinMode(button, INPUT_PULLUP);
  servo.attach(servopin); 
}

void loop() 
{
  Blynk.run();
  poids = scale.readWeight();
  if ((a==1) and (poids<20)){
    servo.write(angle);
    if(angle == 0)
      angle = 180;
    else
    if(angle == 180)
      angle = 0;
    else
   
  poids = scale.readWeight();
  Serial.print(poids);
  Serial.println(" g");
  Serial.println(" Gamelle vide. Je vais la remplir!");
  
 
  delay(1000);
  }
  else if (poids>20) {  
    while (i<1){
      Serial.println(" Gamelle pleine");
    i++;
    }
  }
  }

My problem is in the BLYNK_WRITE(V1) part. I want my code to run the loop function when pinValue=1. But it doesn’t do anything. My project is connected, and everything seems to be OK, its just that when I push the button on the app, nothing happens. The code is supposed to make a servo turn when a load cell reads a weight of less than 20g, but this is not relevant to my problem, as this code runs perfectly with a digital button. So all I want to do is replace the physical button by a virtual one, but I can’t get it right.

Could someone help me figure out what’s wrong? Thank you so much in advance.

Okay, your lack of understanding of C++ code structure is letting you down.

The void loop() is a special function which runs constantly.
What you need to do in your case is to put the code from your void loop (except Bynk.run() which needs to remain where it is) into a new function that you’ve created.
If this code is just meant to run once on each press of the widget attached to V1 then the code could simply go in the BLYNK_WRITE(V1) function instead.
Note that you can’t use delays with Blynk, so if you want the code that is currently in the void loop to execute once every second when the V1 widget is on then you should use a timer, and a flag.

Start by reading this:

then the "Overriding a Timer (auto/manual mode)` section of this topic:

for information about how to use a flag.

Pete.