How to I get Blynk to initialise codes in my voidloop for other components to start working

Hi, I am new to Blynk and am experiencing some difficulty admist my project. I wanted to use Blynk to send a HIGH signal to start my running my set codes in Arduino (replacing a physical push button). While using the virtual pin BLYNK_WRITE and Blynk.run() function, I am able to received the HIGH signal from BLYNK and light up an LED. However, I am unable to use the HIGH signal as a condition to start my loop. How can I use Blynk to initialise my void loop()?

In my Voidloop there are components like stepper motor, load cell sensor, IR sensor that needs initialising from Blynk for them to begin. And I am using the Arduino Uno WiFi Rev 2 Board.
Thank you!

Thank you for your help.


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Defining IR Sensor PIN
#define sensorIR A0
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Defining Blynk 
#define BLYNK_PRINT Serial
#include <SPI.h>
#include <BlynkSimpleWiFiNINA.h>
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Including WIFI Library
#include <WiFiNINA.h>
//#include "arduino_secrets.h" 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Including Stepper Library
#include <Stepper.h>
#include <Stepper.h>
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Defining Pins
#define PIN_Bynk A1
#define PIN_Ack A2

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Setting Variable for WIFI

int keyIndex = 0;                 // your network key index number (needed only for WEP)
int status = WL_IDLE_STATUS;
WiFiServer server (80);
char server1[] = "script.google.com"; // name address for Google
WiFiSSLClient client;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Settings for Stepper Motor
const float STEPS_PER_REV = 32;
const float GEAR_RED = 64;
const float STEPS_PER_OUT_REV = STEPS_PER_REV * GEAR_RED;
int StepsRequired;
Stepper steppermotor(STEPS_PER_REV, 4, 6, 5, 7);

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Setting Variable for IR Sensor
float sensorValue,cm;
float previousVal;
float val;

String collect;     //Variable for if medication has been collected
String water;
String medicine;
String food;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Variables for Blynk
int CurrentState1 =0;
//int NewState1 =1;
int virtualbuttonstate = LOW; // for timer on blynk
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~TO STORE MEDICATION BUTTON VALUE
int CurrentState2 = 0;
int NewState2 = 1;
int medicationbutton = LOW;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Settings for Buzzer
int piezoPin = A5;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~WIFI Setup
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
// check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }
// attempt to connect to WiFi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
    delay(10000);
  }
  Serial.println("Connected to WiFi.\n");

  //~~~~~~~~~~~~~~~~Buttons for simu latching
  pinMode(12, INPUT_PULLUP);
  pinMode(10,INPUT_PULLUP); //acknowledgement button
  pinMode(A0,OUTPUT);// LED

  Blynk.begin(auth,ssid,pass);
}

void updateGAS() 
{
   Serial.println("Starting Connection to Server.");
   if (client.connect(server1, 443))
   {
      Serial.println("Connected to server.");
      // Making HTTP Request
      
      client.println("Host: script.google.com");
      client.println("Connection: close");
      client.println(); // end HTTP header
      Serial.println("List updated\n");
    }
}

void Stepper_Motor()
{
  steppermotor.setSpeed(1);    
  StepsRequired  =  4;
  steppermotor.step(StepsRequired);
  delay(2000);

   StepsRequired  =  STEPS_PER_OUT_REV / 4; 
   steppermotor.setSpeed(1000);   
   steppermotor.step(StepsRequired);
   delay(1000);
}
void Buzzer()
{
    tone(piezoPin, 950, 500);
    delay(900);
}

BLYNK_WRITE(V1)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable   
  if(param.asInt() == 1)
  {
    Serial.print("V1 Slider value is: ");
    Serial.println(pinValue);
    digitalWrite(12, HIGH);
    
  }
  else
  {
    digitalWrite(12,LOW);
  }
}
void loop() 
{
    Serial.println("NOT IN ANY LOOP");
    Blynk.run();
    //int NewState1 = HIGH; 
    int NewState1 = digitalRead(12);// Simu with Physical Button
    //NewState1 = 1;
    
    if(NewState1 == LOW)//signal from blynk
    {
         digitalWrite(A0,HIGH);
        Serial.println("Blynk Sending HIGH");
        Buzzer();
         digitalWrite(A0,LOW);
        delay(100);
        Serial.println("1");
        
        
        
        int NewState2 = digitalRead(10); //Digital read is on low because not pressed // Simu with Physical Button
        delay(100);
        if(NewState2 == LOW)
        {
            Serial.println("Starting of Main code"); 
            Stepper_Motor();
            Serial.println("Motor spun");
            Serial.println("2");
           
        }
    }
    
if(digitalRead(8) == LOW)// 

  {
    medicine = "Dispensed";
    updateGAS();
    delay(100);
  }
 
 }

First of all, you can’t have all that stuff in your void loop when using Blynk, and you need to get rid of the delays too…

http://help.blynk.cc/en/articles/2091699-keep-your-void-loop-clean

You appear to have declared some global variable for this…

but you aren’t updating these in the BLYNK_WRITE(vPin) function so that they can be used in if comparisons within your code.

Pete.