Virtual write and Blynkwrite at same time

When I both have a function that runs with the timer to update a value and a blynkwrite to controll a servo. The servo doesnt do what it should, you should be able to do this at the same time.

#include "BlynkEdgent.h"
#define echoPin 18
#define trigPin 19
float distance;
long duration;
Servo servo;



void level() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034/2;
  Blynk.virtualWrite(V1, distance);
}
BLYNK_WRITE(V0)
{
  int angle = param.asInt();
  Serial.println(angle); 
  if(angle==1) {
    servo.write(90);
  }
  else {
    servo.write(0);
  }
  
}

BlynkTimer timer;
void setup()
{
  Serial.begin(115200);
  pinMode(echoPin, INPUT);
  pinMode(trigPin, OUTPUT);
  timer.setInterval(500L, level);
  servo.attach(4);
  BlynkEdgent.begin();
  
  delay(100);
}

void loop() {
  BlynkEdgent.run();
  timer.run();
}```

@Philip_Ardvall Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Copy and paste these if you can’t find the correct symbol on your keyboard.

Pete.

Your sketch appears to be missing some important lines of code. I guess these may have been removed for privacy purposes, but it’s best to replace sensitive information with “REDACTED” rather than deleting these lines entirely.

You don’t have a #define USE_[your board type]_BOARD line either, so we’ll need info about the type of board you are using, and also the Settings.h file from your sketch.

When you talks about “a blynkwrite” are you talking about a BLYNK_WRITE(vPin) command, or a Blynk.virtualWrite(vPin, value) command?

The more information about exactly what your issue is, and the nature of the symptoms you are experiencing, the easier it will be for you to obtain assistance from the community group.

Pete.

As you can see I have a function for reading a value and send it (Blynk.virtualWrite), then I have a function for controlling a servo (BLYNK_WRITE(vPin)). I use a esp32, settings below:

/*
 * Board configuration (see examples below).
 */

#if defined(USE_WROVER_BOARD)

  #define BOARD_BUTTON_PIN            15
  #define BOARD_BUTTON_ACTIVE_LOW     true

  #define BOARD_LED_PIN_R             0
  #define BOARD_LED_PIN_G             2
  #define BOARD_LED_PIN_B             4
  #define BOARD_LED_INVERSE           false
  #define BOARD_LED_BRIGHTNESS        128

#elif defined(USE_TTGO_T7)

  // This board does not have a built-in button
  // Connect a button to gpio0 <> GND
  #define BOARD_BUTTON_PIN            0
  #define BOARD_BUTTON_ACTIVE_LOW     true

  #define BOARD_LED_PIN               19
  #define BOARD_LED_INVERSE           false
  #define BOARD_LED_BRIGHTNESS        64

#else

  #warning "Custom board configuration is used"

  #define BOARD_BUTTON_PIN            0                     // Pin where user button is attached
  #define BOARD_BUTTON_ACTIVE_LOW     true                  // true if button is "active-low"

  #define BOARD_LED_PIN               4                     // Set LED pin - if you have a single-color LED attached
  //#define BOARD_LED_PIN_R           15                    // Set R,G,B pins - if your LED is PWM RGB
  //#define BOARD_LED_PIN_G           12
  //#define BOARD_LED_PIN_B           13
  //#define BOARD_LED_PIN_WS2812      4                     // Set if your LED is WS2812 RGB
  #define BOARD_LED_INVERSE           false                 // true if LED is common anode, false if common cathode
  #define BOARD_LED_BRIGHTNESS        64                    // 0..255 brightness control

#endif


/*
 * Advanced options
 */

#define BUTTON_HOLD_TIME_INDICATION   3000
#define BUTTON_HOLD_TIME_ACTION       10000

#define BOARD_PWM_MAX                 1023

#define BOARD_LEDC_CHANNEL_1     1
#define BOARD_LEDC_CHANNEL_2     2
#define BOARD_LEDC_CHANNEL_3     3
#define BOARD_LEDC_TIMER_BITS    10
#define BOARD_LEDC_BASE_FREQ     12000

#define CONFIG_AP_URL                 "blynk.setup"
#define CONFIG_DEFAULT_SERVER         "blynk.cloud"
#define CONFIG_DEFAULT_PORT           443

#define WIFI_NET_CONNECT_TIMEOUT      30000
#define WIFI_CLOUD_CONNECT_TIMEOUT    30000
#define WIFI_AP_IP                    IPAddress(192, 168, 4, 1)
#define WIFI_AP_Subnet                IPAddress(255, 255, 255, 0)
//#define WIFI_CAPTIVE_PORTAL_ENABLE

//#define USE_TICKER
//#define USE_TIMER_ONE
//#define USE_TIMER_THREE
//#define USE_TIMER_FIVE
#define USE_PTHREAD

#define BLYNK_NO_DEFAULT_BANNER

#if defined(APP_DEBUG)
  #define DEBUG_PRINT(...) BLYNK_LOG1(__VA_ARGS__)
#else
  #define DEBUG_PRINT(...)
#endif```

Yes, I can see that.

So far, the only information you have provided is…

despite me saying…

You haven’t provided your full .ino sketch yet, as what you’ve posted clearly won’t work as it stands.

So, it’s some sort of ESP32 development board that has am onboard button attached to GPIO0 which pulls that pin LOW when its pressed, and an onboard LED connected to GPIO2 and that LED lights when GPIO4 is pulled HIGH. Is that correct?

Pete.

#include <Servo.h>


#define BLYNK_TEMPLATE_ID “REDACTED”
#define BLYNK_DEVICE_NAME “REDACTED”
#define BLYNK_AUTH_TOKEN “REDACTED”

#define BLYNK_FIRMWARE_VERSION "0.1.0"
#define BLYNK_PRINT Serial
#include "BlynkEdgent.h"
Servo servo;
float distance;
long duration;
#define echo 18
#define trig 19
void level() {
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);

  duration = pulseIn(echo, HIGH);
  distance = duration * 0.034/2;
  Blynk.virtualWrite(V1, distance);
}
BLYNK_WRITE(V0)
{
  int angle = param.asInt();
  Serial.println(angle); 
  if(angle==1) {
    servo.write(90);
  }
  else {
    servo.write(0);
  }
  
}
BlynkTimer timer;
void setup()
{
  Serial.begin(9600);
  BlynkEdgent.begin();
  servo.attach(4);
  delay(2000); 
timer.setInterval(500L,level);
}

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

These need to the very first lines of your sketch.
I’ll wait for more information.

Pete.

done, without the blynktimer the servo works and when I implement it I get this.
In file included from /Users/user/Documents/Servo_control_blynk2.0/Servo_control_blynk2.0.ino:8:0:
sketch/BlynkEdgent.h:111:12: note: ‘BlynkTimer timer’ previously declared here
BlynkTimer timer;
^
Seems like I declared it before but cant find it.

Found it 111 line, shoud BlynkTimer timer; be in ino or there

Early versions of the Edgent example sketch had a BlynkTimer object called ‘timer’ declared within the Edgent .h files.
That has been fixed in later versions of the Edgent example sketch.

You should ensure that you have the latest Blynk library installed, and you could then either use the later example sketch, or simply give your BlynkTimer object a different name, such as ‘timer1’

Pete.