Virtual Pin can't read data

Hi.
i am using Arduino Uno and 2 ultrasonic sensor and i use virtual pins to read data sensor. I have problems with virtual pins. I use 2 widget gauges. gauge1 for virtual pin 4 and gauge2 for virtual pin 8. Unfortunately only virtual 4 is reading data. Here is my source code.

#define TRIGGER  9
#define ECHO     10
#define TRIGGER1 7
#define ECHO1    6 

#define BLYNK_PRINT DebugSerial
#include <SoftwareSerial.h>
#include <SPI.h>
#include <Ethernet.h>
SoftwareSerial DebugSerial(10, 11); // RX, TX
    
#include <BlynkSimpleStream.h>


char auth[] = "my auth token";

BlynkTimer timer;

void notifyUptime()
{
  long uptime = millis() / 60000L;

  Blynk.notify(String("Running for ") + uptime + " minutes.");
}


BLYNK_WRITE(V1)
{
  int pinValue1 = param.asInt(); // assigning incoming value from pin V1 to a variable

}

BLYNK_WRITE(V2)
{
  int pinValue2 = param.asInt(); // assigning incoming value from pin V2 to a variable

}

BLYNK_WRITE(V3)
{
  int pinValue3 = param.asInt(); // assigning incoming value from pin V3 to a variable

}


BLYNK_WRITE(V4)
{
  int pinValue4 = param.asInt(); // assigning incoming value from pin V4 to a variable

}


BLYNK_WRITE(V5)
{
  int pinValue5 = param.asInt(); // assigning incoming value from pin V5 to a variable

}

BLYNK_WRITE(V6)
{
  int pinValue6 = param.asInt(); // assigning incoming value from pin V6 to a variable

}
BLYNK_WRITE(V7)
{
  int pinValue7 = param.asInt(); // assigning incoming value from pin V7 to a variable

}


BLYNK_WRITE(V8)
{
  int pinValue8 = param.asInt(); // assigning incoming value from pin V8 to a variable

}


void setup()
{
  DebugSerial.begin(9600);
  Serial.begin(9600);
  Blynk.begin(Serial, auth);
  pinMode(TRIGGER, OUTPUT);
  pinMode(ECHO, INPUT);
  pinMode(TRIGGER1, OUTPUT);
  pinMode(ECHO1, INPUT);

   //Notify immediately on startup
   Blynk.notify("Device started");

  // Setup a function to be called every minute
  timer.setInterval(60000L, notifyUptime);
}

void loop()
{
  long duration, distance, duration1, distance1;
  digitalWrite(TRIGGER, LOW);  
  delayMicroseconds(2); 
  digitalWrite(TRIGGER1, LOW);  
  delayMicroseconds(2);
  
  digitalWrite(TRIGGER, HIGH);
  delayMicroseconds(10); 
  digitalWrite(TRIGGER1, HIGH);
  delayMicroseconds(10); 
  
  digitalWrite(TRIGGER, LOW);
  digitalWrite(TRIGGER1, LOW);
  duration = pulseIn(ECHO, HIGH);
  duration1 = pulseIn(ECHO1, HIGH);
  
  distance = (duration/2) / 29.1;
  Blynk.virtualWrite(V4, distance);
  distance1 = (duration1 /2) / 29.1;
  Blynk.virtualWrite(V8, distance1);

  //--------------first sensor-------------
  if(distance <= 1000)
  {
    Blynk.virtualWrite(V1, 255);
  }
  else
  {
    Blynk.virtualWrite(V1, 0);
  }
  if(distance <= 800)
  {
    Blynk.virtualWrite(V2, 255);
  }
  else
  {
    Blynk.virtualWrite(V2, 0);
  }
  if(distance <= 100)
  {
    Blynk.virtualWrite(V3, 255);
  }
  else
  {
    Blynk.virtualWrite(V3, 0);
  }
  
  if(distance <= 80)
  {
    Blynk.notify("Overflow! Please clean up the trash can");
  }
  
  //---------------second sensor-------------------
  if(distance1 <= 1000)
  {
    Blynk.virtualWrite(V5, 255);
  }
  else
  {
    Blynk.virtualWrite(V5, 0);
  }
  if(distance1 <= 800)
  {
    Blynk.virtualWrite(V6, 255);
  }
  else
  {
    Blynk.virtualWrite(V6, 0);
  }
  if(distance1 <= 100)
  {
    Blynk.virtualWrite(V7, 255);
  }
  else
  {
    Blynk.virtualWrite(V7, 0);
  }
  if(distance1 <= 4)
  {
    Blynk.email("my e-mail @gmail.com", "Subject: IoT Trash Can", "Overflow! Please clean up the trash can.");
  }
  
  delay(200);
  Blynk.run();
  timer.run();
}

i really need help.

@Natasha. You loop() is heavily loaded. Rewrite the program to have all reads triggerred by timers. Let loop() just contain blynk.run and timer.run. There should be no Blynk.virtualWrites in loop().

thank you and i do as u told but virtual pin 8 remain the same

#define BLYNK_PRINT DebugSerial
#define TRIGGER   9
#define ECHO      10
#define TRIGGER1  7
#define ECHO1     6

#include <SoftwareSerial.h>
SoftwareSerial DebugSerial(10, 11); // RX, TX
    
#include <BlynkSimpleStream.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "my auth token";

// This function will be called every time Slider Widget
// in Blynk app writes values to the Virtual Pin V1
BLYNK_WRITE(V4)
{
  int pinValue4 = param.asInt(); // assigning incoming value from pin V1 to a variable

  // process received value
}

BLYNK_WRITE(V8)
{
  int pinValue8 = param.asInt(); // assigning incoming value from pin V1 to a variable

  // process received value
}


void setup()
{
  // Debug console
  DebugSerial.begin(9600);
  Serial.begin(9600);
  Blynk.begin(Serial, auth);
  pinMode(TRIGGER, OUTPUT);
  pinMode(TRIGGER1, OUTPUT);
  pinMode(ECHO, INPUT);
  pinMode(ECHO1, INPUT);
  
}

void loop()
{
  long duration, distance, duration1, distance1;
  digitalWrite(TRIGGER, LOW);  
  delayMicroseconds(2); 
  digitalWrite(TRIGGER1, LOW);  
  delayMicroseconds(2);
  
  digitalWrite(TRIGGER, HIGH);
  delayMicroseconds(10); 
  digitalWrite(TRIGGER1, HIGH);
  delayMicroseconds(10);
  
  digitalWrite(TRIGGER, LOW);
  digitalWrite(TRIGGER1, LOW);
  
  duration = pulseIn(ECHO, HIGH);
  distance = (duration/2) / 29.1;
  Blynk.virtualWrite(V4, distance);
  
  duration1 = pulseIn(ECHO1, HIGH);
  distance1 = (duration1/2) / 29.1;
  Blynk.virtualWrite(V8, distance1);
  
  delay(200);
  Blynk.run();
}

did i miss something?

No you didn’t :stuck_out_tongue_winking_eye: As per the document I linked to above… With Blynk, you need to keep the void loop() more like this… and use Blynk Timer not delays.

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

Probably becasue your Blynk function is doing nothing but assigning the incoming value to a non-global variable that is never used inside the function and since it is not global, it can’t be used outside this function (not that it is anyhow).

https://www.arduino.cc/reference/en/language/variables/variable-scope--qualifiers/scope/

thank you very much for the info. i’ll try my hardest